解析数据框中的重载列-使用一个mutate语句创建多个列

时间:2019-01-29 07:56:04

标签: r dplyr tidyeval

我在一个数据帧中有一个overloaded重载列,其中包含1432312789之类的值。

df <- data_frame(id = 1:3, 
                 overloaded = c("145", "459", "2"))

我想将此列解析为9个新的布尔列。

df %>% 
  mutate(col_1 = str_detect(overloaded, "1"),
         col_2 = str_detect(overloaded, "2"),
         col_3 = str_detect(overloaded, "3"),
         col_4 = str_detect(overloaded, "4"),
         col_5 = str_detect(overloaded, "5"),
         col_6 = str_detect(overloaded, "6"),
         col_7 = str_detect(overloaded, "7"),
         col_8 = str_detect(overloaded, "8"),
         col_9 = str_detect(overloaded, "9"))

像这样

id   overloaded    col_1 col_2 col_3 col_4 col_5 col_6 col_7 col_8 col_9
 1      145        TRUE  FALSE FALSE TRUE  TRUE  FALSE FALSE FALSE FALSE
 2      459        FALSE FALSE FALSE TRUE  TRUE  FALSE FALSE FALSE TRUE 
 3      2          FALSE TRUE  FALSE FALSE FALSE FALSE FALSE FALSE FALSE

我如何写这个而不重复自己?

1 个答案:

答案 0 :(得分:1)

pat = as.character(1:9)
res = sapply(pat, FUN = str_detect, string = df$overloaded)
colnames(res) = paste("col", pat, sep = "_")
cbind(df, res)
#     id overloaded col_1 col_2 col_3 col_4 col_5 col_6 col_7 col_8 col_9
# 145  1        145  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
# 459  2        459 FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
# 2    3          2 FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE