向量 x 的元素出现在向量 y 中的频率
Frequency of elements of vector x, as occuring in vector y
我有一个向量 x 并且想要获得向量 x 的元素在向量 y 中出现的频率。
最后我需要一个数据框,其中第一列显示 x 的元素,第二列显示它们在向量 y 中出现的频率:
element_x frequency_in_y
hello 2
my 1
world 0
d
我曾尝试过一些笨拙的方法,但没有成功。
freq_x <- as_tibble(x) %>% mutate(Freq=str_count(x, y))
更新:
x <- c("hello", "my", "world")
y <- c("hello there", "this is my life", "hello", "bla")
一个选项 base R
,
out <- sapply(seq(x), function(i) sum(grepl(x[i],y)))
data.frame(element_x=x, frequency_in_y=out)
给予,
element_x frequency_in_y
1 hello 2
2 my 1
3 world 0
编辑:
要获得多次出现的单词,您可以 运行,
x <- c("hello", "my", "world")
y <- c("hello there", "this is my my life", "hello", "bla") # 2 my including now.
library(tidyverse)
out <- colSums(sapply(x, function(i) str_count(y, i)))
data.frame(element_x=x, frequency_in_y=out)
element_x frequency_in_y
hello hello 2
my my 2
world world 0
我有一个向量 x 并且想要获得向量 x 的元素在向量 y 中出现的频率。 最后我需要一个数据框,其中第一列显示 x 的元素,第二列显示它们在向量 y 中出现的频率:
element_x frequency_in_y
hello 2
my 1
world 0
d
我曾尝试过一些笨拙的方法,但没有成功。
freq_x <- as_tibble(x) %>% mutate(Freq=str_count(x, y))
更新:
x <- c("hello", "my", "world")
y <- c("hello there", "this is my life", "hello", "bla")
一个选项 base R
,
out <- sapply(seq(x), function(i) sum(grepl(x[i],y)))
data.frame(element_x=x, frequency_in_y=out)
给予,
element_x frequency_in_y
1 hello 2
2 my 1
3 world 0
编辑:
要获得多次出现的单词,您可以 运行,
x <- c("hello", "my", "world")
y <- c("hello there", "this is my my life", "hello", "bla") # 2 my including now.
library(tidyverse)
out <- colSums(sapply(x, function(i) str_count(y, i)))
data.frame(element_x=x, frequency_in_y=out)
element_x frequency_in_y
hello hello 2
my my 2
world world 0