我怎样才能使用管道操作符来管道到替换功能colnames()<-
呢?
这是我正在尝试做的事情:
library(dplyr)
averages_df <-
group_by(mtcars, cyl) %>%
summarise(mean(disp), mean(hp))
colnames(averages_df) <- c("cyl", "disp_mean", "hp_mean")
averages_df
# Source: local data frame [3 x 3]
#
# cyl disp_mean hp_mean
# 1 4 105.1364 82.63636
# 2 6 183.3143 122.28571
# 3 8 353.1000 209.21429
但理想情况下,它会是这样的:
averages_df <-
group_by(mtcars, cyl) %>%
summarise(mean(disp), mean(hp)) %>%
add_colnames(c("cyl", "disp_mean", "hp_mean"))
有没有办法做到这一点,而无需每次都编写专业函数?
这里的答案是一个开始,但不完全是我的问题:Chaining算术运算符 in dplyr