首页 > 解决方案 > Is there a R function to compare the frequency of occurrences of row elements between two dataframes?

问题描述

Say I have two dataframes

df1
Var1=c("a", "b", "c")
Freq= c(1, 3, 8)

df2
Var1= c("a", "b", "c")
Freq= c(5, 3, 8)

so I want my output to be y & z as their frequencies match between the two dataframes.

标签: rdataframecomparefrequency

解决方案


使用 dplyr:

> df1
  Var1 Freq
1    x    1
2    y    3
3    z    8
> df2
  Var1 Freq
1    x    5
2    y    3
3    z    8
> df1 %>% inner_join(df2, by = c('Var1' = 'Var1', 'Freq' = 'Freq' )) %>% pull(1)
[1] "y" "z"

推荐阅读