dplyr concat columns stored in variable (mutate and non standard evaluation)

[亡魂溺海] 提交于 2019-12-12 15:37:38

问题


I would like to concatenate an arbitrary number of columns in a dataframe based on a variable cols_to_concat

df <- dplyr::data_frame(a = letters[1:3], b = letters[4:6], c = letters[7:9])
cols_to_concat = c("a", "b", "c")

To achieve the desired result with this specific value of cols_to_concat I could do this:

df %>% 
  dplyr::mutate(concat = paste0(a, b, c))

But I need to generalise this, using syntax a bit like this

# (DOES NOT WORK)
df %>% 
  dplyr::mutate(concat = paste0(cols))

I'd like to use the new NSE approach of dplyr 0.7.0, if this is appropriate, but can't figure out the correct syntax.


回答1:


You can try syms from rlang:

library(dplyr)
packageVersion('dplyr')
#[1] ‘0.7.0’
df <- dplyr::data_frame(a = letters[1:3], b = letters[4:6], c = letters[7:9])
cols_to_concat = c("a", "b", "c")

library(rlang)
cols_quo <- syms(cols_to_concat)
df %>% mutate(concat = paste0(!!!cols_quo))

# or
df %>% mutate(concat = paste0(!!!syms(cols_to_concat)))

# # A tibble: 3 x 4
#       a     b     c concat
#   <chr> <chr> <chr>  <chr>
# 1     a     d     g    adg
# 2     b     e     h    beh
# 3     c     f     i    cfi



回答2:


You can perform this operation using only the tidyverse if you'd like to stick to those packages and principles. You can do it by using either mutate() or unite_(), which comes from the tidyr package.

Using mutate()

library(dplyr)
df <- data_frame(a = letters[1:3], b = letters[4:6], c = letters[7:9])
cols_to_concat <- c("a", "b", "c")

df %>% mutate(new_col = do.call(paste0, .[cols_to_concat]))

# A tibble: 3 × 4
      a     b     c new_col
  <chr> <chr> <chr>   <chr>
1     a     d     g     adg
2     b     e     h     beh
3     c     f     i     cfi

Using unite_()

library(tidyr)
df %>% unite_(col='new_col', cols_to_concat, sep="", remove=FALSE)

# A tibble: 3 × 4
  new_col     a     b     c
*   <chr> <chr> <chr> <chr>
1     adg     a     d     g
2     beh     b     e     h
3     cfi     c     f     i



回答3:


You can do the following:

library(dplyr)

df <- dplyr::data_frame(a = letters[1:3], b = letters[4:6], c = letters[7:9])

cols_to_concat = lapply(list("a", "b", "c"), as.name)

q <- quos(paste0(!!! cols_to_concat))

df %>% 
  dplyr::mutate(concat = !!! q)


来源:https://stackoverflow.com/questions/44613279/dplyr-concat-columns-stored-in-variable-mutate-and-non-standard-evaluation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!