如何在 mutate 中使用 singularize 函数?
How to use the singularize function in mutate?
我有一个文本语料库,作为预处理的一部分,我需要将所有单词单数化。
假设我们有一个语料库:
corpus <- c("house", "friends", "cats", "dogs") %>% tibble(word = .)
如果我直接应用单数化函数 (SemNetCleaner) 它会起作用,但是我需要使用慢速 for 循环将它应用到我的单词列的每一行:
#install.packages("SemNetCleaner")
library(SemNetCleaner)
corpus[2,1] %>% unlist() %>% singularize()
word
"friend"
但是,如果我在 mutate 中使用它,它只会像 paste() 函数那样绑定所有条目:
corpus %>% mutate(singular = singularize(word))
# A tibble: 4 x 2
word singular
<chr> <chr>
1 house house friends cats dog
2 friends house friends cats dog
3 cats house friends cats dog
4 dogs house friends cats dog
使用 rowwise()
corpus %>% rowwise() %>% mutate(singular = singularize(word))
# A tibble: 4 x 2
# Rowwise:
word singular
<chr> <chr>
1 house house
2 friends friend
3 cats cat
4 dogs dog
我有一个文本语料库,作为预处理的一部分,我需要将所有单词单数化。
假设我们有一个语料库:
corpus <- c("house", "friends", "cats", "dogs") %>% tibble(word = .)
如果我直接应用单数化函数 (SemNetCleaner) 它会起作用,但是我需要使用慢速 for 循环将它应用到我的单词列的每一行:
#install.packages("SemNetCleaner")
library(SemNetCleaner)
corpus[2,1] %>% unlist() %>% singularize()
word
"friend"
但是,如果我在 mutate 中使用它,它只会像 paste() 函数那样绑定所有条目:
corpus %>% mutate(singular = singularize(word))
# A tibble: 4 x 2
word singular
<chr> <chr>
1 house house friends cats dog
2 friends house friends cats dog
3 cats house friends cats dog
4 dogs house friends cats dog
使用 rowwise()
corpus %>% rowwise() %>% mutate(singular = singularize(word))
# A tibble: 4 x 2
# Rowwise:
word singular
<chr> <chr>
1 house house
2 friends friend
3 cats cat
4 dogs dog