Iskandar The Pupsi
Iskandar The Pupsi

Reputation: 323

Frequency of elements of vector x, as occuring in vector y

I have a vector x and want to get the frequency of occurences of elements of vector x in vector y. in the end I need a dataframe with the first column showing the elements of x and the second column showing the frequency of these as they occur in vector y:

element_x frequency_in_y
hello            2
my               1
world            0

d

I have tried something clumsy as that but that doesn't do the trick.

freq_x <- as_tibble(x) %>% mutate(Freq=str_count(x, y))

Update:

x <- c("hello", "my", "world")
y <- c("hello there", "this is my life", "hello", "bla")

Upvotes: 0

Views: 69

Answers (1)

maydin
maydin

Reputation: 3755

One option with base R,

out <- sapply(seq(x), function(i) sum(grepl(x[i],y)))

data.frame(element_x=x, frequency_in_y=out)

gives,

  element_x frequency_in_y
1     hello              2
2        my              1
3     world              0

EDIT:

For getting more than one occurances of a word you can run,

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

Upvotes: 2

Related Questions