计算作为 dplyr 链一部分的字符串“内”的字符串数量

Dou*_*Fir 3 r stringr

我有一个看起来像这样的数据框:

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
Run Code Online (Sandbox Code Playgroud)

我想要一个新变量 z 中的水果数量。期望的结果:

mydf
  x                        y z
1 1            apples; pears 2
2 2 oranges; bananas; grapes 3
3 3                   apples 1
Run Code Online (Sandbox Code Playgroud)

尝试过:

mydf %>% mutate(z = str_split(y, ';') %>% length) # gives '3' for all fields
Run Code Online (Sandbox Code Playgroud)

如何通过分割某个字符(在本例中为“;”)来获取字符串中的字符串计数?

akr*_*run 5

可以用以下方法完成str_count

library(dplyr)
library(stringr0
mydf %>%
    mutate(z = str_count(y, '\\w+'))
Run Code Online (Sandbox Code Playgroud)

的输出str_split是 alist并且lengthlength我们需要的整个列表的lengths(返回length每个list元素的)

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
Run Code Online (Sandbox Code Playgroud)