我想构建一个可以在文本向量中搜索单词的函数。但有时矢量将包含10个以上的元素(ps:矢量的长度可以变化)。所以我想根据函数的长度命名这些列" x"。名称应为" str1"," str2"," str3" ...等等。我该怎么做?感谢您的帮助:)
> myfun <- function(x){
>
> word5<- c("my",9,6,5,3,4,3)
>
qq <- function(x){
> str_count(x, pattern = word5)
> }
>
> Freq <- sapply(x, FUN = qq)
>
output1 <- data.frame(word5,Freq)
> output1
>
> }
>
> u <- c("my god","my love my", "my my my")
>myfun(u)
结果应该是这样的
> word5 str1 str2 str3
> 1 my 1 2 3
> 2 9 0 0 0
> 3 6 0 0 0
> 4 5 0 0 0
> 5 3 0 0 0
> 6 4 0 0 0
> 7 3 0 0 0
答案 0 :(得分:1)
require(stringr)
myfun <- function(u) {
n <- seq_along(u)
word5 <- c("my", "god", 6, 5, 3, 4, 3)
qq <- function(x) {
str_count(x, pattern = word5)
}
Freq <- sapply(u, FUN = qq)
output1 <- data.frame(word5, Freq)
colnames(output1)[-1] <- paste0("str", n)
output1
}
u <- c("my god","my love my", "my my my")
myfun(u)
# word5 str1 str2 str3
# 1 my 1 2 3
# 2 god 1 0 0
# 3 6 0 0 0
# 4 5 0 0 0
# 5 3 0 0 0
# 6 4 0 0 0
# 7 3 0 0 0
答案 1 :(得分:0)
这有用吗?
library(tidyverse)
word_searcher <- function(vec_to_search, pattern) {
word_count <- stringr::str_count(vec_to_search, pattern)
setNames(word_count, paste0("str_", seq_along(word_count))) %>%
enframe() %>%
spread(name, value) %>%
mutate(word5 = pattern) %>%
select(word5, everything())
}
u <- c("my god","my love my", "my my my", "my", "pop", "pip my")
word_searcher(u, "my")
# # A tibble: 1 x 7
# word5 str_1 str_2 str_3 str_4 str_5 str_6
# <chr> <int> <int> <int> <int> <int> <int>
# 1 my 1 2 3 1 0 1
我们得到计数,将计数的名称设置为向量长度的函数,然后简单地将其转换为具有正确列排序的数据框。
您可以使用该函数传递要在循环中获得的句子和模式的向量,然后bind_rows
将所有内容组合在一起。
答案 2 :(得分:0)
u <- c("my god","my love my", "my my my")
word5<- c("my","god",6,5,3,4)
library(stringr)
ans <- rbind.data.frame(t(sapply(word5, function(x) str_count(u, x))))
V1 V2 V3
my 1 2 3
god 1 0 0
6 0 0 0
5 0 0 0
3 0 0 0
4 0 0 0