Ryan R. Rosario
Ryan R. Rosario

Reputation: 5234

Expanding Delimited Data in a Column into Multiple Logical Columns

I have a column in a data.frame that consists of data from a series of checkboxes. The data, as I receive it, gets shoved into a column that looks like:

choice1|choice3|choice6

For reproduction: my.data <- data.frame(checkboxes="choice1|choice3|choice6", stringsAsFactors = FALSE)

There is a discrete number of such values separated by a pipe.

I would like to convert this data into a series of logical columns named after the possible choices as so:

choice1 choice2 choice3 choice4 choice5 choice6 
TRUE    FALSE   TRUE    FALSE   FALSE   TRUE

I tried separate() but it did not quite fit my use case. My use case seems like a mix of reshaping and separate().

How can I accomplish this in R?

Upvotes: 0

Views: 38

Answers (2)

Aur&#232;le
Aur&#232;le

Reputation: 12819

Another possibility, in the same spirit:

parsed_checkboxes <- strsplit(my.data$checkboxes, "|", fixed = TRUE)
as.data.frame(lapply(
  setNames(nm = paste0("choice", 1:6)),
  function(choice) vapply(parsed_checkboxes, "%in%", logical(1), x = choice)
))

##   choice1 choice2 choice3 choice4 choice5 choice6
## 1    TRUE   FALSE    TRUE   FALSE   FALSE    TRUE

Upvotes: 2

moodymudskipper
moodymudskipper

Reputation: 47320

You want to use a combination of strsplit and apply functions, here's a way to do that :

library("magrittr")

possible_choices <- c("choice1","choice2","choice3","choice4","choice5","choice6")
df1 <- data.frame(choices =c("choice1|choice3|choice6","choice1|choice2|choice3"),stringsAsFactors = FALSE)

# > df1
# choices
# 1 choice1|choice3|choice6
# 2 choice1|choice2|choice3

output <- df1$choices %>% 
  strsplit("\\|") %>% 
  lapply(. %>% sapply(`==`,possible_choices)) %>% 
  lapply(. %>% apply(1,any)) %>% 
  do.call(rbind,.) %>%
  as.data.frame %>%
  setNames(possible_choices)

# choice1 choice2 choice3 choice4 choice5 choice6
# 1    TRUE   FALSE    TRUE   FALSE   FALSE    TRUE
# 2    TRUE    TRUE    TRUE   FALSE   FALSE   FALSE

Upvotes: 1

Related Questions