我如何使用管道运算符将管道替换为类似的函数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"))
有没有一种方法,而无需每次都编写特殊功能?
答案是一个开始,但不完全是我的问题:dplyr中的链算术运算符
summarise
-group_by(mtcars, cyl) %>% summarise(disp_mean=mean(disp), hp_mean=mean(hp))
尽管我看不到如何使用colnames
这么多的阻力。是否必须在dplyr中完成每件事?