用R中的plyr包重命名输出列 [英] renaming the output column with the plyr package in R

查看:81
本文介绍了用R中的plyr包重命名输出列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hadley使我开始使用 plyr 程序包,我发现自己一直在使用它来做分组依据"之类的东西.但是我发现自己必须始终对结果列进行重命名,因为它们默认为V1,V2等.

Hadley turned me on to the plyr package and I find myself using it all the time to do 'group by' sort of stuff. But I find myself having to always rename the resulting columns since they default to V1, V2, etc.

这是一个例子:

mydata<-data.frame(matrix(rnorm(144, mean=2, sd=2),72,2),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(mydata) <- c("x_value", "acres",  "state")
groupAcres <- ddply(mydata, c("state"), function(df)c(sum(df$acres)))
colnames(groupAcres) <- c("state","stateAcres")

有没有办法让ddply为我命名结果列,以便我省略最后一行?

Is there a way to make ddply name the resulting column for me so I can omit that last line?

推荐答案

这似乎可行:

> groupAcres <- ddply(mydata, c("state"), function(df) c(myName=sum(df$acres)))
> groupAcres
  state   myName
1     A 56.87973
2     B 57.84451
3     C 52.82415

这篇关于用R中的plyr包重命名输出列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆