Sum column based on conditions in another column in a data frameDrop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameDrop data frame columns by nameHow to drop columns by name in a data frameChanging column names of a data frameExtracting specific columns from a data frameSum of hybrid data frames depending on multiple conditions in RSelect rows from a DataFrame based on values in a column in pandas
Is there any way to land a rover on the Moon without using any thrusters?
getting syntax error in simple bash script
My research paper filed as a patent in China by my Chinese supervisor without me as inventor
What explanation do proponents of a Scotland-NI bridge give for it breaking Brexit impasse?
Why does the speed of sound decrease at high altitudes although the air density decreases?
Can I conceal an antihero's insanity - and should I?
Does an oscilloscope subtract voltages as phasors?
How are aircraft depainted?
Is the Dodge action perceptible to other characters?
How do certain apps show new notifications when internet access is restricted to them?
What organs or modifications would be needed for an animal not to require sleep?
How major are these paintwork & rust problems?
Thematic, genred concepts in Ancient Greek?
What is the mathematical notation for rounding a given number to the nearest integer?
Ambiguity in notation resolved by +
If I want an interpretable model, are there methods other than Linear Regression?
Is a suit against a University Dorm for changing policies on a whim likely to succeed (USA)?
What does a Light weapon mean mechanically?
What was redacted in the Yellowhammer report? (Point 15)
Is there a real-world mythological counterpart to WoW's "kill your gods for power" theme?
Does my opponent need to prove his creature has morph?
Should I leave the first authorship of our paper to the student who did the project whereas I solved it?
What hard drive connector is this?
What is the CR of a Metallic Dragon that used Change Shape?
Sum column based on conditions in another column in a data frame
Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameDrop data frame columns by nameHow to drop columns by name in a data frameChanging column names of a data frameExtracting specific columns from a data frameSum of hybrid data frames depending on multiple conditions in RSelect rows from a DataFrame based on values in a column in pandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a data frame, which contains two columns, Time
and Response
df = cbind.data.frame(
Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
)
I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].
The expected data frame will be
dfe = cbind.data.frame(
time.range = c(1, 2, 3, 4, 5),
Response = c(3, 5, 19.4, 6, 24)
)
r dataframe
add a comment
|
I have a data frame, which contains two columns, Time
and Response
df = cbind.data.frame(
Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
)
I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].
The expected data frame will be
dfe = cbind.data.frame(
time.range = c(1, 2, 3, 4, 5),
Response = c(3, 5, 19.4, 6, 24)
)
r dataframe
add a comment
|
I have a data frame, which contains two columns, Time
and Response
df = cbind.data.frame(
Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
)
I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].
The expected data frame will be
dfe = cbind.data.frame(
time.range = c(1, 2, 3, 4, 5),
Response = c(3, 5, 19.4, 6, 24)
)
r dataframe
I have a data frame, which contains two columns, Time
and Response
df = cbind.data.frame(
Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
)
I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].
The expected data frame will be
dfe = cbind.data.frame(
time.range = c(1, 2, 3, 4, 5),
Response = c(3, 5, 19.4, 6, 24)
)
r dataframe
r dataframe
asked Mar 28 at 10:29
WangWang
2161 silver badge9 bronze badges
2161 silver badge9 bronze badges
add a comment
|
add a comment
|
2 Answers
2
active
oldest
votes
We can use floor
to group it for every minute
library(dplyr)
df %>%
group_by(minute = floor(Time)) %>%
summarise(Response = sum(Response))
# minute Response
# <dbl> <dbl>
#1 1 3
#2 2 5
#3 3 20.4
#4 4 6
#5 5 24
Using aggregate
in base R
aggregate(Response~floor(Time), df, sum)
Also with tapply
tapply(df$Response, floor(df$Time), sum)
And for completion data.table
option
library(data.table)
setDT(df)[,sum(Response), by = floor(Time)]
add a comment
|
We can use rowsum
from base R
rowsum(df$Response, as.integer(df$Time))
# [,1]
#1 3.0
#2 5.0
#3 20.4
#4 6.0
#5 24.0
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55395339%2fsum-column-based-on-conditions-in-another-column-in-a-data-frame%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
We can use floor
to group it for every minute
library(dplyr)
df %>%
group_by(minute = floor(Time)) %>%
summarise(Response = sum(Response))
# minute Response
# <dbl> <dbl>
#1 1 3
#2 2 5
#3 3 20.4
#4 4 6
#5 5 24
Using aggregate
in base R
aggregate(Response~floor(Time), df, sum)
Also with tapply
tapply(df$Response, floor(df$Time), sum)
And for completion data.table
option
library(data.table)
setDT(df)[,sum(Response), by = floor(Time)]
add a comment
|
We can use floor
to group it for every minute
library(dplyr)
df %>%
group_by(minute = floor(Time)) %>%
summarise(Response = sum(Response))
# minute Response
# <dbl> <dbl>
#1 1 3
#2 2 5
#3 3 20.4
#4 4 6
#5 5 24
Using aggregate
in base R
aggregate(Response~floor(Time), df, sum)
Also with tapply
tapply(df$Response, floor(df$Time), sum)
And for completion data.table
option
library(data.table)
setDT(df)[,sum(Response), by = floor(Time)]
add a comment
|
We can use floor
to group it for every minute
library(dplyr)
df %>%
group_by(minute = floor(Time)) %>%
summarise(Response = sum(Response))
# minute Response
# <dbl> <dbl>
#1 1 3
#2 2 5
#3 3 20.4
#4 4 6
#5 5 24
Using aggregate
in base R
aggregate(Response~floor(Time), df, sum)
Also with tapply
tapply(df$Response, floor(df$Time), sum)
And for completion data.table
option
library(data.table)
setDT(df)[,sum(Response), by = floor(Time)]
We can use floor
to group it for every minute
library(dplyr)
df %>%
group_by(minute = floor(Time)) %>%
summarise(Response = sum(Response))
# minute Response
# <dbl> <dbl>
#1 1 3
#2 2 5
#3 3 20.4
#4 4 6
#5 5 24
Using aggregate
in base R
aggregate(Response~floor(Time), df, sum)
Also with tapply
tapply(df$Response, floor(df$Time), sum)
And for completion data.table
option
library(data.table)
setDT(df)[,sum(Response), by = floor(Time)]
edited Mar 28 at 10:42
answered Mar 28 at 10:32
Ronak ShahRonak Shah
82k13 gold badges50 silver badges87 bronze badges
82k13 gold badges50 silver badges87 bronze badges
add a comment
|
add a comment
|
We can use rowsum
from base R
rowsum(df$Response, as.integer(df$Time))
# [,1]
#1 3.0
#2 5.0
#3 20.4
#4 6.0
#5 24.0
add a comment
|
We can use rowsum
from base R
rowsum(df$Response, as.integer(df$Time))
# [,1]
#1 3.0
#2 5.0
#3 20.4
#4 6.0
#5 24.0
add a comment
|
We can use rowsum
from base R
rowsum(df$Response, as.integer(df$Time))
# [,1]
#1 3.0
#2 5.0
#3 20.4
#4 6.0
#5 24.0
We can use rowsum
from base R
rowsum(df$Response, as.integer(df$Time))
# [,1]
#1 3.0
#2 5.0
#3 20.4
#4 6.0
#5 24.0
answered Mar 28 at 17:02
akrunakrun
466k15 gold badges261 silver badges343 bronze badges
466k15 gold badges261 silver badges343 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55395339%2fsum-column-based-on-conditions-in-another-column-in-a-data-frame%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown