R:dplyr::mutate使用由作为字符串传递的变量组合组成的表达式

R:dplyr::mutate使用由作为字符串传递的变量组合组成的表达式,r,dplyr,R,Dplyr,我想写一个函数,向数据帧添加一个新变量。新变量包含与参数中传递的一组变量(作为字符串向量)对应的值的串联。在base R中,我将写下如下内容: addConcatFields<-function(data,listOfVar) { data$uniqueId=data[,listOfVar[1]] for(elt in listOfVar[2:length(listOfVar)]) { data$uniqueId=paste(data$uniqueId,data[,elt],sep='_'

我想写一个函数,向数据帧添加一个新变量。新变量包含与参数中传递的一组变量(作为字符串向量)对应的值的串联。在base R中,我将写下如下内容:

addConcatFields<-function(data,listOfVar)
{
data$uniqueId=data[,listOfVar[1]]
for(elt in listOfVar[2:length(listOfVar)])
{
data$uniqueId=paste(data$uniqueId,data[,elt],sep='_')
}
return(data)
}

addConcatFields(iris,c('Petal.Width','Species'))

# gives:
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species   uniqueId
1          5.1         3.5          1.4         0.2  setosa 0.2_setosa
2          4.9         3.0          1.4         0.2  setosa 0.2_setosa
...

addConcatFields使用数据表,我做了如下操作

library(data.table)
iris <- data.table(iris)

iris[, uniqueId := do.call(function(...) paste(..., sep = "_"),.SD), .SDcols = c('Petal.Width','Species')]
库(data.table)

iris查看
tidyr
中的
unite
功能。它是
tidyverse
dplyr
包含在同一组包中的一部分

library(tidyr)
unite(iris,uniqueID,c(Petal.Width,Species))
#    Sepal.Length Sepal.Width Petal.Length       uniqueID
#1            5.1         3.5          1.4     0.2_setosa
#2            4.9         3.0          1.4     0.2_setosa
#3            4.7         3.2          1.3     0.2_setosa
#4            4.6         3.1          1.5     0.2_setosa
如果不想丢失连接的两列,只需包含
remove=F

unite(iris,uniqueID,c(Petal.Width,Species),remove = F)
#    Sepal.Length Sepal.Width Petal.Length       uniqueID Petal.Width    Species
#1            5.1         3.5          1.4     0.2_setosa         0.2     setosa
#2            4.9         3.0          1.4     0.2_setosa         0.2     setosa
#3            4.7         3.2          1.3     0.2_setosa         0.2     setosa
#4            4.6         3.1          1.5     0.2_setosa         0.2     setosa

添加到其他答案中,因为您说过要使用dplyr的
mutate

下面是一种在
mutate
中使用
粘贴的方法:

iris %>% mutate(uniqueId= paste(Petal.Width, Species, sep = '_'))
# gives the following result:
     Sepal.Length Sepal.Width Petal.Length Petal.Width Species uniqueId
 1          5.1         3.5          1.4         0.2 setosa  0.2_setosa
 2          4.9         3            1.4         0.2 setosa  0.2_setosa
 3          4.7         3.2          1.3         0.2 setosa  0.2_setosa
 4          4.6         3.1          1.5         0.2 setosa  0.2_setosa
 5          5           3.6          1.4         0.2 setosa  0.2_setosa
 6          5.4         3.9          1.7         0.4 setosa  0.4_setosa
 7          4.6         3.4          1.4         0.3 setosa  0.3_setosa
 8          5           3.4          1.5         0.2 setosa  0.2_setosa
 9          4.4         2.9          1.4         0.2 setosa  0.2_setosa
10          4.9         3.1          1.5         0.1 setosa  0.1_setosa
...
如果您的函数是自定义函数,则可以将其矢量化,然后使用它。 例如,这将导致与上述相同的结果:

concat_fields<-function(var1, var2) {
  return (paste(var1, var2, sep = '_'))
}
v_concat_fields <- Vectorize(concat_fields)
iris %>% mutate(v_concat_fields(Petal.Width, Species))

concat_fields解决这一问题的最佳方法是使用准引号-本文在解释基本原理方面非常有用

最好的选择不是将列名存储为字符串,而是将它们存储为带引号的字符串,因此:

varlist <- rlang::quos('Petal.Width', 'Species')

应该会给你想要的结果。

好的,考虑一下这里是另一个解决方案

使用match函数将字符串名称转换为列号

然后使用如下列编号(将示例中的数字向量替换为匹配结果):


df我的观点是,我想用字符串向量作为变量名来编写一个函数(就像在我的代码中一样)。老实说,我不太清楚你想做什么。我编辑了我的答案,以提供一个自定义函数的示例,但请不要犹豫,发表评论,以澄清您试图实现的目标。这是我第一次选择的方向,但我确实希望保留字符串作为输入
dplyr::select(iris, !!! varlist)
df <- tbl_df(df[c(3, 4, 7, 1, 9, 8, 5, 2, 6, 10)])