我想写一个函数来获取单字的个数(一个单词)。然而,我当前的函数并没有按照我想要的方式工作。
这是我的函数和示例数据集:
library(ngrams)
library(tidyverse)
#dataframe
df<-tribble(~text,
"This sentence",
"I am going to luch",
"This is a really nice and sunny day")
#function
get_unigrams <- function(text) {
unigram<- ngram(text, n = 1) %>% get.ngrams() %>% length()
return(unigram)
}然而,使用"mutate“函数的计算得到了一个非常奇怪的结果:
df %>% mutate(n=get_unigrams((text)))
# A tibble: 3 x 2
text n
<chr> <int>
1 This sentence 14
2 I am going to luch 14
3 This is a really nice and sunny day 14每个句子的长度是相等的。我认为这是因为所有三行文本被放在一起并被视为一个文本。
但是,我希望得到这样的结果:
# A tibble: 3 x 2
text n
<chr> <int>
1 This sentence 2
2 I am going to luch 5
3 This is a really nice and sunny day 8有人能帮我吗?
我在我的函数中看不到错误。
首先要感谢大家!
更新:
我找到了一个(临时)解决方案:
get_unigrams <- function(text) {
sapply(text, function(text){
unigram<- ngram(text, n = 1) %>% get.ngrams() %>% length()
return(unigram)
}
)
}但是,使用sapply-function的解决方案非常慢(因为它单独执行每一行)。我有一个超过100k行的数据帧。
有人能帮我提高速度吗?例如,使用矢量化函数?
发布于 2021-12-02 14:00:12
使用rowwise。有关更多信息,请查看?rowwise。
df %>% rowwise() %>%
mutate(n=get_unigrams(text))
text n
<chr> <int>
1 This sentence 2
2 I am going to luch 5
3 This is a really nice and sunny day 8另一种解决方案(使用基数R)是:
df$n <- apply(df, 1, get_unigrams)发布于 2021-12-02 16:06:49
另一种解决方案,基于stringr::str_count
library(tidyverse)
df<-tribble(~text,
"This sentence",
"I am going to luch",
"This is a really nice and sunny day")
df %>%
mutate(n = str_count(text, "\\w+"))
#> # A tibble: 3 × 2
#> text n
#> <chr> <int>
#> 1 This sentence 2
#> 2 I am going to luch 5
#> 3 This is a really nice and sunny day 8https://stackoverflow.com/questions/70200602
复制相似问题