由 dplyr 和 magrittr 等软件包启用的管道隐喻非常有用,并且可以使您的代码在 R 中可读(一项艰巨的任务!)
如何制作一个以将数据框中的所有变量重命名为预定列表而结束的管道?
这是我尝试过的。首先,要测试的简单样本数据:
> library(dplyr)
> iris %>% head(n=3) %>% select(-Species) %>% t %>% as.data.frame -> test.data
> test.data
1 2 3
Sepal.Length 5.1 4.9 4.7
Sepal.Width 3.5 3.0 3.2
Petal.Length 1.4 1.4 1.3
Petal.Width 0.2 0.2 0.2
这不起作用:
> test.data %>% rename(a=1,b=2,c=3)
Error: Arguments to rename must be unquoted variable names. Arguments a, b, c are not.
我无法通过阅读rename
. 我的另一个尝试通过使用花括号来定义代码块来避免错误,但重命名实际上并没有发生:
> test.data %>% { names(.) <- c('a','b','c')}