我有以下问题:
library(tidyverse)
df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5), Sepal.Width = c(3.5,
3, 3.2, 3.1, 3.6), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4)), .Names = c("Sepal.Length",
"Sepal.Width", "Petal.Length"), row.names = c(NA, 5L), class = c("tbl_df",
"tbl", "data.frame"))
看起来像这样:
> df
# A tibble: 5 × 3
Sepal.Length Sepal.Width Petal.Length
* <dbl> <dbl> <dbl>
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
4 4.6 3.1 1.5
5 5.0 3.6 1.4
我想要做的是替换Sepal.Length
并Petal.Length
使用附加的字符串to_app <- ".xxx"
导致:
Sepal.Length.xxx Sepal.Width Petal.Length.xxx
5.1 3.5 1.4
4.9 3.0 1.4
4.7 3.2 1.3
4.6 3.1 1.5
5.0 3.6 1.4
我尝试此错误:
>df %>% rename(paste(Sepal.Length,to_app,sep="") = Petal.Length,paste(Sepal.Width,to_app,sep="") = Petal.Length)
Error: unexpected '=' in "df %>% rename(paste(Sepal.Length,to_app,sep="") ="
如果要使用dplyr的rename
函数,最好创建一个命名的vector / list并使用.dots
标准评估版中的参数调用它:
cols <- c("Sepal.Length", "Petal.Length")
to_app <- ".xxx"
cols <- setNames(cols, paste0(cols, to_app))
df %>% rename_(.dots = cols)
## A tibble: 5 × 3
# Sepal.Length.xxx Sepal.Width Petal.Length.xxx
#* <dbl> <dbl> <dbl>
#1 5.1 3.5 1.4
#2 4.9 3.0 1.4
#3 4.7 3.2 1.3
#4 4.6 3.1 1.5
#5 5.0 3.6 1.4
但是请注意,此方法可能会随着dplyr的下一个版本0.6.0更改(请参见例如http://blog.rstudio.org/2017/04/13/dplyr-0-6-0-coming-soon/和http://dplyr.tidyverse.org/articles/programming.html)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句