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

David Carlson dcarlson at tamu.edu
Wed Oct 30 16:23:02 CET 2013


apply() is a different function from sapply() and has different
arguments.

-------------------------------------
David L Carlson
Deparment of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Dan Abner
Sent: Wednesday, October 30, 2013 7:24 AM
To: Rui Barradas
Cc: r-help at r-project.org
Subject: Re: [R] Subtotals by id for a large number of columns
XXXX

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