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 ';'?
It can be done with str_count
library(dplyr)
library(stringr0
mydf %>%
mutate(z = str_count(y, '\\w+'))
The output of str_split is a list and length is the length of the whole list, we need lengths (returns the length of each list element)
mydf %>%
mutate(z = str_split(y, ';') %>%
lengths)
x y z
1 1 apples; pears 2
2 2 oranges; bananas; grapes 3
3 3 apples 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With