df1=data.frame(c(2,1,2),c(1,2,3,4,5,6),seq(141,170)) #create data.frame
names(df1) = c("gender","age","height") #column names
df1$gender <- factor(df1$gender,
levels=c(1,2),
labels=c("female","male")) #gives levels and labels to gender
df1$age <- factor(df1$age,
levels=c(1,2,3,4,5,6),
labels=c("16-24","25-34","35-44","45-54","55-64","65+")) # gives levels and labels to age groups
I am looking to produce a summary of the height values subsetted by gender and then age.
Using the subset
and by
functions as provides the output I want:
females<-subset(df1,df1$gender==1) #subsetting by gender
males<-subset(df1,df1$gender==2)
foutput=by(females$height,females$age,summary) #producing summary subsetted by age
moutput=by(males$height,males$age,summary)
However I require it to be in a data.frame so that I can export these results alongside frequency tables using XLconnect.
Is there an way to convert the output to a data.frame or an elegant alternative, possibly using plyr?