I have a character column with a different amount of values per row. This is just a small example:
GoodForMeal %>% head(5)
# A tibble: 5 x 1
GoodForMeal
<chr>
1 dessert': False, 'latenight': False, 'lunch': True, 'dinner': True
2 dessert': False, 'latenight': False, 'lunch': True, 'dinner': True
3 <NA>
4 dessert': False, 'latenight': False, 'lunch': True, 'dinner': True
5 dessert': False, 'latenight': False, 'lunch': True, 'dinner': True
Here is a dput()
of the first row of the column:
structure(list(GoodForMeal = "dessert': False, 'latenight': False, 'lunch': True, 'dinner': True, 'breakfast': False, 'brunch': False}"), .Names = "GoodForMeal", row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))
I want to assign the values before the colon as column names and the values after the colon as the values of the respective column.
Example:
desert latenight lunch diner
1 False False True True
2 False False True True
3 NA NA NA NA
4 False False True True
5 False False True True
I tried it with the tidyr
packadge and the separate
and the spread
function:
separate(GoodForMeal, c("key", "value"), sep = ":", extra = "merge") %>% spread(key, value)
The problem is the r is not splitting all the values before the colon but just the first value.
So the result looks like this:
GoodForMeal %>% str()
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4464 obs. of 2 variables:
$ dessert': chr " False, 'latenight': False, 'lunch': True, 'dinner': False, 'breakfast': False, 'brunch': False}" " False, 'latenight': False, 'lunch': True, 'dinner': True, 'breakfast': False, 'brunch': False}" " False, 'latenight': False, 'lunch': False, 'dinner': False, 'breakfast': False, 'brunch': False}" " False, 'latenight': False, 'lunch': True, 'dinner': True, 'breakfast': False, 'brunch': False}" ...
$ <NA> : chr NA NA NA NA ...
Any Idea how to split the values so that it´s looking like in the example? THX