How to sum a variable by group but do not aggregate the data frame in R? [duplicate]
This question already has an answer here:
Count number of observations/rows per group and add result to data frame
9 answers
Calculate group mean (or other summary stats) and assign to original data
4 answers
although I have found a lot of ways to calculate the sum of a variable by group, all the approaches end up creating a new data set which aggregates the double cases.
To be more precise, if I have a data frame:
id year
1 2010
1 2015
1 2017
2 2011
2 2017
3 2015
and I want to count the number of times I have the same ID by the different years, there are a lot of ways (using aggregate, tapply, dplyr, sqldf etc) which use a "group by" kind of functionality that in the end will give something like:
id count
1 3
2 2
3 1
I haven't managed to find a way to calculate the same thing but keep my original data frame, in order to obtain:
id year count
1 2010 3
1 2015 3
1 2017 3
2 2011 2
2 2017 2
3 2015 1
and therefore do not aggregate my double cases.
Has somebody already figured out?
Thank you in advance
r sum grouping
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Count number of observations/rows per group and add result to data frame
9 answers
Calculate group mean (or other summary stats) and assign to original data
4 answers
although I have found a lot of ways to calculate the sum of a variable by group, all the approaches end up creating a new data set which aggregates the double cases.
To be more precise, if I have a data frame:
id year
1 2010
1 2015
1 2017
2 2011
2 2017
3 2015
and I want to count the number of times I have the same ID by the different years, there are a lot of ways (using aggregate, tapply, dplyr, sqldf etc) which use a "group by" kind of functionality that in the end will give something like:
id count
1 3
2 2
3 1
I haven't managed to find a way to calculate the same thing but keep my original data frame, in order to obtain:
id year count
1 2010 3
1 2015 3
1 2017 3
2 2011 2
2 2017 2
3 2015 1
and therefore do not aggregate my double cases.
Has somebody already figured out?
Thank you in advance
r sum grouping
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
dplyr --> mutate
,data.table --> :=
,baseR --> ave
– Sotos
Nov 14 '18 at 14:23
Indplyr
, you can usedf %>% add_count(id)
ordf %>% group_by(id) %>% mutate(count = n())
.
– tmfmnk
Nov 14 '18 at 14:25
1st userle
function (or others) to calculate your second table. 2nd merge the first and the second tables by the 'id' column to get the third table
– Bastien
Nov 14 '18 at 14:28
did u got the solution?
– sai saran
Nov 14 '18 at 14:29
@sai saran yes, thanks to tmfmnk i did the follow using dplyr : df %>% add_count(id)%>% group_by(id, year) Thank you all
– am.nik
Nov 14 '18 at 16:29
add a comment |
This question already has an answer here:
Count number of observations/rows per group and add result to data frame
9 answers
Calculate group mean (or other summary stats) and assign to original data
4 answers
although I have found a lot of ways to calculate the sum of a variable by group, all the approaches end up creating a new data set which aggregates the double cases.
To be more precise, if I have a data frame:
id year
1 2010
1 2015
1 2017
2 2011
2 2017
3 2015
and I want to count the number of times I have the same ID by the different years, there are a lot of ways (using aggregate, tapply, dplyr, sqldf etc) which use a "group by" kind of functionality that in the end will give something like:
id count
1 3
2 2
3 1
I haven't managed to find a way to calculate the same thing but keep my original data frame, in order to obtain:
id year count
1 2010 3
1 2015 3
1 2017 3
2 2011 2
2 2017 2
3 2015 1
and therefore do not aggregate my double cases.
Has somebody already figured out?
Thank you in advance
r sum grouping
This question already has an answer here:
Count number of observations/rows per group and add result to data frame
9 answers
Calculate group mean (or other summary stats) and assign to original data
4 answers
although I have found a lot of ways to calculate the sum of a variable by group, all the approaches end up creating a new data set which aggregates the double cases.
To be more precise, if I have a data frame:
id year
1 2010
1 2015
1 2017
2 2011
2 2017
3 2015
and I want to count the number of times I have the same ID by the different years, there are a lot of ways (using aggregate, tapply, dplyr, sqldf etc) which use a "group by" kind of functionality that in the end will give something like:
id count
1 3
2 2
3 1
I haven't managed to find a way to calculate the same thing but keep my original data frame, in order to obtain:
id year count
1 2010 3
1 2015 3
1 2017 3
2 2011 2
2 2017 2
3 2015 1
and therefore do not aggregate my double cases.
Has somebody already figured out?
Thank you in advance
This question already has an answer here:
Count number of observations/rows per group and add result to data frame
9 answers
Calculate group mean (or other summary stats) and assign to original data
4 answers
r sum grouping
r sum grouping
asked Nov 14 '18 at 14:21
am.nikam.nik
53
53
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
dplyr --> mutate
,data.table --> :=
,baseR --> ave
– Sotos
Nov 14 '18 at 14:23
Indplyr
, you can usedf %>% add_count(id)
ordf %>% group_by(id) %>% mutate(count = n())
.
– tmfmnk
Nov 14 '18 at 14:25
1st userle
function (or others) to calculate your second table. 2nd merge the first and the second tables by the 'id' column to get the third table
– Bastien
Nov 14 '18 at 14:28
did u got the solution?
– sai saran
Nov 14 '18 at 14:29
@sai saran yes, thanks to tmfmnk i did the follow using dplyr : df %>% add_count(id)%>% group_by(id, year) Thank you all
– am.nik
Nov 14 '18 at 16:29
add a comment |
1
dplyr --> mutate
,data.table --> :=
,baseR --> ave
– Sotos
Nov 14 '18 at 14:23
Indplyr
, you can usedf %>% add_count(id)
ordf %>% group_by(id) %>% mutate(count = n())
.
– tmfmnk
Nov 14 '18 at 14:25
1st userle
function (or others) to calculate your second table. 2nd merge the first and the second tables by the 'id' column to get the third table
– Bastien
Nov 14 '18 at 14:28
did u got the solution?
– sai saran
Nov 14 '18 at 14:29
@sai saran yes, thanks to tmfmnk i did the follow using dplyr : df %>% add_count(id)%>% group_by(id, year) Thank you all
– am.nik
Nov 14 '18 at 16:29
1
1
dplyr --> mutate
, data.table --> :=
, baseR --> ave
– Sotos
Nov 14 '18 at 14:23
dplyr --> mutate
, data.table --> :=
, baseR --> ave
– Sotos
Nov 14 '18 at 14:23
In
dplyr
, you can use df %>% add_count(id)
or df %>% group_by(id) %>% mutate(count = n())
.– tmfmnk
Nov 14 '18 at 14:25
In
dplyr
, you can use df %>% add_count(id)
or df %>% group_by(id) %>% mutate(count = n())
.– tmfmnk
Nov 14 '18 at 14:25
1st use
rle
function (or others) to calculate your second table. 2nd merge the first and the second tables by the 'id' column to get the third table– Bastien
Nov 14 '18 at 14:28
1st use
rle
function (or others) to calculate your second table. 2nd merge the first and the second tables by the 'id' column to get the third table– Bastien
Nov 14 '18 at 14:28
did u got the solution?
– sai saran
Nov 14 '18 at 14:29
did u got the solution?
– sai saran
Nov 14 '18 at 14:29
@sai saran yes, thanks to tmfmnk i did the follow using dplyr : df %>% add_count(id)%>% group_by(id, year) Thank you all
– am.nik
Nov 14 '18 at 16:29
@sai saran yes, thanks to tmfmnk i did the follow using dplyr : df %>% add_count(id)%>% group_by(id, year) Thank you all
– am.nik
Nov 14 '18 at 16:29
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
1
dplyr --> mutate
,data.table --> :=
,baseR --> ave
– Sotos
Nov 14 '18 at 14:23
In
dplyr
, you can usedf %>% add_count(id)
ordf %>% group_by(id) %>% mutate(count = n())
.– tmfmnk
Nov 14 '18 at 14:25
1st use
rle
function (or others) to calculate your second table. 2nd merge the first and the second tables by the 'id' column to get the third table– Bastien
Nov 14 '18 at 14:28
did u got the solution?
– sai saran
Nov 14 '18 at 14:29
@sai saran yes, thanks to tmfmnk i did the follow using dplyr : df %>% add_count(id)%>% group_by(id, year) Thank you all
– am.nik
Nov 14 '18 at 16:29