将使用“by”创建的摘要转换为data.frame [英] converting summary created using 'by' to data.frame
本文介绍了将使用“by”创建的摘要转换为data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
使用子集
和 by
函数提供我想要的输出:
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)
但是我要求它是我n一个数据框架,以便我可以使用XLconnect将这些结果与频率表一起导出。
However I require it to be in a data.frame so that I can export these results alongside frequency tables using XLconnect.
有没有办法将输出转换为data.frame或优雅的选择,可能使用plyr?
Is there an way to convert the output to a data.frame or an elegant alternative, possibly using plyr?
推荐答案
这是使用plyr的一种方法:
Here's one approach using plyr:
> ddply(df1, c("gender", "age"), function(x) summary(x$height))
gender age Min. 1st Qu. Median Mean 3rd Qu. Max.
1 female 25-34 142 148 154 154 160 166
2 female 55-64 145 151 157 157 163 169
3 male 16-24 141 147 153 153 159 165
4 male 35-44 143 149 155 155 161 167
5 male 45-54 144 150 156 156 162 168
6 male 65+ 146 152 158 158 164 170
这篇关于将使用“by”创建的摘要转换为data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文