使用匹配表达式的列创建数据框的新列(向量类型)

时间:2019-11-27 14:55:24

标签: r dataframe tidyverse purrr mutate

有一个包含n列的数据框,我想创建一个新列,其中包含与正则表达式匹配的某些列的向量(行方向)。例如:

df <- data.frame(id = c(1,2,3,4),
                 V1 = letters[1:4],
                 V2 = letters[2:5],
                 V3 = letters[3:6],
                 stringsAsFactors = F)

> print.data.frame(df)
  id V1 V2 V3
1  1  a  b  c
2  2  b  c  d
3  3  c  d  e
4  4  d  e  f

我想做的事情是这样的:

df %>%
  mutate(vectors = mapply(function(...) {c(...) %>% list},
                          select(., matches("V[0-9]"))))

为澄清起见,在此示例中,结果将与这样做相同:

df %>%
  mutate(vectors = mapply(function(x, y, z) {c(x,y,z) %>% list},
                          x = V1,
                          y = V2,
                          z = V3))

  id V1 V2 V3 vectors
1  1  a  b  c a, b, c
2  2  b  c  d b, c, d
3  3  c  d  e c, d, e
4  4  d  e  f d, e, f

是否可以使用tidyverse / purrr解决方案?

预先感谢

编辑:“向量”列必须不是单个字符串,而是向量。请在下面查看:

> str(df2)
'data.frame':   4 obs. of  5 variables:
 $ id     : num  1 2 3 4
 $ V1     : chr  "a" "b" "c" "d"
 $ V2     : chr  "b" "c" "d" "e"
 $ V3     : chr  "c" "d" "e" "f"
 $ vectors:List of 4
  ..$ a: chr  "a" "b" "c"
  ..$ b: chr  "b" "c" "d"
  ..$ c: chr  "c" "d" "e"
  ..$ d: chr  "d" "e" "f"

1 个答案:

答案 0 :(得分:-1)

感谢@aosmith

这有效:

df %>% mutate(vectors = select(., matches("V[0-9]")) %>% pmap(c)) %>% 
  mutate(vectors = lapply(vectors, function(x) unname(unlist(x))))