[R] Subtotals by id for a large number of columns XXXX

arun smartpink111 at yahoo.com
Wed Oct 30 14:27:31 CET 2013


Hi,
May be:

apply(d1[3:5],2, mysum)
  var1 var2 var3
1    2    0    1
2    2    2    3
3    1    1    1
4    0    1    0

#or
sapply(d1[3:5],mysum)

#or
library(plyr)
ddply(d1[,-2],.(id),colwise(sum))
A.K.



On Wednesday, October 30, 2013 8:30 AM, Dan Abner <dan.abner99 at gmail.com> wrote:
Hi Rui,

Thanks for responding. When I make this change, I get an error message:

> mysum<-function(x) tapply(x,d1$id,sum)
>
> d2<-apply(d1[2:4],mysum)
Error in match.fun(FUN) : argument "FUN" is missing, with no default
Thoughts?



On Wed, Oct 30, 2013 at 8:01 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:

> Hello,
>
> If I understand it correctly, just change mysum to the following.
>
>
> mysum<-function(x) tapply(x,d1$id,sum)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 30-10-2013 11:07, Dan Abner escreveu:
>
>> Hi everybody,
>>
>> I have data in the format of the example data below where essentially a
>> large number of indicator variables (coded [0,1]) reflect traits of the
>> same id across multiple rows. I need to represent the data in a 1 row per
>> id format. I see this as being similar to converting from long to wide
>> format, however, there is no time component here: The multiple rows here
>> are all characteristics observed at the same measurement occasion. So,
>> really I just need an individual sum for each variable (for a large number
>> of variables) and for these to be all saved in the same row (along with
>> the
>> id variable and other demographics (e.g., "location").
>>
>> Here is the example df and the method I used first:
>>
>>
>> d1<-data.frame(id=c(1,1,1,2,2,**2,2,3,3,4),location=factor(c(**
>> rep(0,7),rep(1,3)),
>>   labels=c("A","B")),var1=as.**logical(round(runif(10))),
>>   var2=as.logical(round(runif(**10))),var3=as.logical(round(**
>> runif(10))))
>> d1
>> mysum<-function(x) aggregate(x,by=list(d1$id),**sum)
>> d2<-sapply(d1[2:4],mysum)
>> d2
>>
>> Any help is appreciated!!
>>
>> Thanks!
>>
>> Dan
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________**________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.

>>
>>

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list