I have a dataframe that looks something like this:
mydf <- data.frame(
x = 1:3,
y = c('apples; pears', 'oranges; bananas; grapes', 'apples')
)
mydf
x y
1 1 apples; pears
2 2 oranges; bananas; grapes
3 3 apples
I would like the count of fruits in a new variable z. Desired outcome:
mydf
x y z
1 1 apples; pears 2
2 2 oranges; bananas; grapes 3
3 3 apples 1
Tried:
mydf %>% mutate(z = str_split(y, ';') %>% length) # gives '3' for all fields
How can I get the count of strings within a string by splitting on some character, in this case ';'?