str_extract()和summarise()给我没有行

pkpto39

这应该非常简单,因为我只是想对自己所看到的内容进行验证。

我试图用来将str_extract()感兴趣的区域从数据框中的列中拉出,然后计算每个单词出现的频率。我遇到了一个问题,但是当我执行此操作时,我生成的数据帧已NA列在其中一行中。这让我感到困惑,因为我不知道是什么原因导致的,或者这是否表示我的代码中有错误。我不确定如何解决此问题。

此外,请注意,单词中的最后一项是“桌子很轻”,在此示例中包含两个感兴趣的单词。我这样做是有意的,因为我想确保将其计算两次。

library(tidyverse)

df <- data.frame(words =c("paper book", "food press", "computer monitor", "my fancy speakers",
                 "my two dogs", "the old couch", "the new couch", "loud speakers", 
                 "wasted paper", "put the dishes away", "set the table", "put it on the table", 
                 "lets go to church", "turn out the lights", "why are the lights on",
                 "the table is light"))

keep <- c("dogs|paper|table|light|couch")

new_df <- df %>% 
  mutate(Subject = str_extract(words, keep), n = n()) %>% 
  group_by(Subject)%>%
  summarise(`Word Count` = length(Subject))

这就是我现在要得到的

 Subject `Word Count`
  <chr>          <int>
1 couch              2
2 dogs               1
3 light              2
4 paper              2
5 table              3
6 NA                 6

所以我的问题是-是什么导致Subject中的NA行?还有其他所有记录吗?

罗纳克·沙

NA那些地方有中没有字的值出现keep出现在该行因此没有什么可提取物。

library(dplyr)
library(stringr)

df %>%  mutate(Subject = str_extract(words, keep))

#                   words Subject
#1             paper book   paper
#2             food press    <NA>
#3       computer monitor    <NA>
#4      my fancy speakers    <NA>
#5            my two dogs    dogs
#6          the old couch   couch
#7          the new couch   couch
#8          loud speakers    <NA>
#9           wasted paper   paper
#10   put the dishes away    <NA>
#11         set the table   table
#12   put it on the table   table
#13     lets go to church    <NA>
#14   turn out the lights   light
#15 why are the lights on   light
#16    the table is light   table

例如,对于第二行,'food press'其中没有任何值,"dogs|paper|table|light|couch"因此它返回NA

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章