如何为列名创建别名?

时间:2019-04-12 02:53:26

标签: r

这是我以前从未见过的很棒的东西。您可以在一个命令中进行汇总,从而等效于mutategroup bysummarize

aggregate(state.x77,
          list(Region = state.region,
               Cold = state.x77[,"Frost"] > 130), mean)

我想以公式格式复制它。我快到了。

state <- as.data.frame(state.x77)
state$states <- rownames(state.x77)
state$region <- state.region
aggregate(Population ~ region + (state[,"Frost"] > 130), state, mean)

我想给新列起一个名字。我尝试了AS "Cold",但是没有用。

2 个答案:

答案 0 :(得分:2)

您还可以在公式界面中使用cbind重命名输入或分组变量:

aggregate(Population ~ region + cbind(cold = Frost > 130), data=state, mean)
#         region  cold Population
#1     Northeast FALSE  8802.8000
#2         South FALSE  4208.1250
#3 North Central FALSE  7233.8333
#4          West FALSE  4582.5714
#5     Northeast  TRUE  1360.5000
#6 North Central  TRUE  2372.1667
#7          West  TRUE   970.1667

~的两侧工作,并在LHS的一个cbind中使用多个变量:

aggregate(cbind(popn=Population,incm=Income) ~ cbind(cold = Frost > 130), data=state, mean)
#   cold     popn     incm
#1 FALSE 5494.853 4345.412
#2  TRUE 1593.500 4627.875

否则,如果您需要应对这种可能性,只需在RHS上添加多个cbind语句:

aggregate(cbind(popn=Population,incm=Income) ~ cbind(cold = Frost > 130) + 
                                               cbind(st = states), data=state, mean)
#    cold             st  popn incm
#1  FALSE        Alabama  3615 3624
#2   TRUE         Alaska   365 6315
#3  FALSE        Arizona  2212 4530
#4  FALSE       Arkansas  2110 3378
#...

答案 1 :(得分:1)

我们可以使用transform

aggregate(Population ~ region + Cold, transform(state, Cold = Frost > 130),   mean)
#        region  Cold Population
#1     Northeast FALSE  8802.8000
#2         South FALSE  4208.1250
#3 North Central FALSE  7233.8333
#4          West FALSE  4582.5714
#5     Northeast  TRUE  1360.5000
#6 North Central  TRUE  2372.1667
#7          West  TRUE   970.1667