"By" function Frame Conversion (with Multiple Indices)
On Jan 3, 2013, at 9:00 PM, Ray DiGiacomo, Jr. wrote:
Hello,
I have the following dataset. Please note that there are missing
values on
records 4 and 5:
id,age,weight,height,gender
1,22,180,72,m
2,13,100,67,f
3,5,40,40,f
4,6,42,,f
5,12,98,66,
6,50,255,60,m
I'm using the "By" function like this:
list1 <- by(dataset[c("weight", "height")],
dataset[c("age", "gender")],
colMeans,
na.rm = TRUE)
I named the dataframe "dat"
aggregate( dat[,c("weight", "height")],
list( age=dat$age, gender=dat$gender), FUN=mean,
na.rm=TRUE)
age gender weight height
1 12 98 66
2 5 f 40 40
3 6 f 42 NaN
4 13 f 100 67
5 22 m 180 72
6 50 m 255 60
David. > > I then convert the list above to a frame like this: > > as.data.frame( do.call(rbind, list1) ) > > I get this output from the code above: > > weight height > 1 40 40 > 2 42 NaN > 3 100 67 > 4 180 72 > 5 255 60 > > I want to get the output above, but I also want two additional columns > named "age" and "gender" (with the age and gender values from the "By" > function output). > > How would I do this? > > Best Regards, > > Ray DiGiacomo, Jr. > Healthcare Predictive Analytics Specialist > President, Lion Data Systems LLC > President, The Orange County R User Group > Board Member, TDWI > rayd at liondatasystems.com > (m) 408-425-7851 > San Juan Capistrano, California USA > twitter.com/liondatasystems > linkedin.com/in/raydigiacomojr > youtube.com/user/liondatasystems/videos > liondatasystems.com/courses > > [[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. David Winsemius, MD Alameda, CA, USA