Hi,
My df basically looks like this:
Id A B C total
3 5 0 1 6
3 4 3 4 11
3 2 1 2 5
4 5 4 3 12
4 3 2 4 9
4 1 1 1 3
I want to collapse the rows by Id and get:
Id A B C total
3 11 4 7 22
4 9 7 8 24
I was able to do so for one column with:
df.grouped<- aggregate(df$A~Id, data=df, FUN="sum")
I have many columns (A-Z), so I need some kind of loop. I tried:
df.grouped<- aggregate(df[4:51]~Id, data=df, FUN="sum")
names(df.grouped)<-paste(names(df)[4:51])
But got:
Error in model.frame.default(formula = df[4:51] ~ Id, data = df) :
invalid type (list) for variable 'df[4:51]'
As you can see, I also want the names in df.grouped to be the same as in df.
Any ideas?
Thanks,
Simon
You really should take a deep look at dplyr and tidyr. In this case group_by and summarise should do the work. In base r you can look at the by command.
--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thanks Amit
I tried but I got really confused and couldn't find a solution. Can you point me to a good tutorial on these packages?
Simon
תודה!
סיימון גבעוליפסיכולוג תעסוקתי052-3626345
2015-10-05 6:54 GMT+03:00 Yoni Sidi <yon...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.