[R] summarizing a dataset on a factor

Tom Wright tom at maladmin.com
Thu Mar 27 21:48:23 CET 2014


Hi all,
I've spent too long in matlab land recently and seem to have forgotten
my R skillz ;-)
I'm sure I'm missing a simple way to do this...

Given a data frame
id<-rep(1:5,2)
eye<-c(rep('l',5),rep('r',5))
age<-rep(round(runif(5,0,12)),2)
response<-round(runif(10,1,10)*10)/10
response2<-sample(c('High','Low'),10,replace=TRUE)

data<-data.frame(id,eye,age,response,response2)

I want to create a new dataset averaging the response variable from both
eyes for each test.

I know there are many ways to do this but... I would also like to keep
the value of the response2 variable for the left eye.

ending up with the dataset
id	age	response	response2
1	9	3.65	High
2	10	3.85	High
3	8	8.15	Low
4	4	4.4	Low
5	0	4.6	High

I thought something like
f<-function(x){#make my choices here}
aggregate(data,list(data$id),f)

but x only seems to contain the first column of data.

I could probably have done this manually in the time spent writing this
email.
any help appreciated, 
Tom



More information about the R-help mailing list