Одно решение с участием purrr
, stringr
и dplyr
может быть:
bind_cols(DATASET,
map_dfc(.x = c("apps", "phone", "commute", "place"),
~ DATASET %>%
mutate(!!.x := ifelse(str_detect(INFO, .x),
str_extract_all(INFO, "\\d+"),
NA_character_)) %>%
select(.x)))
ID INFO apps phone commute place
1 1 You used works apps for 4 minutes today. 4 <NA> <NA> <NA>
2 2 You checked your phone 10 times today. <NA> 10 <NA> <NA>
3 3 Your commute time to work today was 4 minutes. <NA> <NA> 4 <NA>
4 4 You (or at least your phone) were at your work place for 15 minutes today <NA> 15 <NA> 15
Если в строке может быть более одного числа:
bind_cols(DATASET,
map_dfc(.x = c("apps", "phone", "commute", "place"),
~ DATASET %>%
mutate(!!.x := map_chr(ifelse(str_detect(INFO, .x),
str_extract_all(INFO, "\\d+"),
NA_character_),
toString)) %>%
select(.x)))