How to sum a variable by group but do not aggregate the data frame in R? [duplicate]












0
















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










share|improve this question













marked as duplicate by Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

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













  • 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













  • 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


















0
















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










share|improve this question













marked as duplicate by Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

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













  • 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













  • 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
















0












0








0









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










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:21









am.nikam.nik

53




53




marked as duplicate by Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

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 r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

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













  • 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













  • 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





    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











  • 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










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














0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues