Rickard
Rickard

Reputation: 3670

Parsing overloaded column in dataframe - creating multiple columns with one mutate statement

I have an overloaded column, overloadedin a dataframe containing values like 143 or 23 or 12789.

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

I want to parse this column into 9 new boolean columns.

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"))

like so

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

How can I write this without repeating myself?

Upvotes: 0

Views: 19

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145785

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

Upvotes: 1

Related Questions