Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of strings 'within' a string as part of a dplyr chain [duplicate]

Tags:

r

stringr

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 ';'?

like image 258
Doug Fir Avatar asked Oct 27 '25 04:10

Doug Fir


1 Answers

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
like image 184
akrun Avatar answered Oct 29 '25 20:10

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!