I have a dataframe that contains fields with strings, such as "fish, birds, animals", etc. I have collapsed them into a list, and iterate over them in order to create logical fields within same dataframe. Update: The question is now updated with a more elaborate example.
However, this is slow and does not feel optimal. It's not an operation I have to do more than once, so I wasn't that bothered, but think there might be a better way, using dplyr, perhaps.
This code does create new fields for every match of each element in my_list within the field items.
no <- seq(1:3)
items <- c('fish,cat,dog', 'horse,elephant,dog', 'hamster,pig')
df <- data.frame(no, items)
df$items <- as.character(df$items)
df
Will create the following data frame:
no items
1 1 fish,cat,dog
2 2 horse,elephant,dog
3 3 hamster,pig
Running this code will harvest the field items and expand it into logical fields
items <- paste(df$items, collapse = ",")
item_list <- unlist(unique(strsplit(items, ",")))
for (i in 1:length(item_list)) {
lt <- item_list[i]
df <- df %>% rowwise() %>% mutate(!!lt := grepl(lt, items))
}
data.frame(df)
Resulting in this data frame:
no items fish cat dog horse elephant hamster pig
1 1 fish,cat,dog TRUE TRUE TRUE FALSE FALSE FALSE FALSE
2 2 horse,elephant,dog FALSE FALSE TRUE TRUE TRUE FALSE FALSE
3 3 hamster,pig FALSE FALSE FALSE FALSE FALSE TRUE TRUE
Here's a step by step solution; Uwe's is probably much faster but I hope this is easier to understand: