假设我有两个数据帧
df1
Var1=c("a", "b", "c")
Freq= c(1, 3, 8)
df2
Var1= c("a", "b", "c")
Freq= c(5, 3, 8)
所以我希望我的输出是y&z,因为它们的频率在两个数据帧之间匹配。
也许可以试试这个:
#Code
df1$Var1[df1$Freq==df2$Freq]
输出:
[1] "y" "z"
使用的一些数据:
#Data 1
df1 <- structure(list(Var1 = c("x", "y", "z"), Freq = c(1L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-3L))
#Data 2
df2 <- structure(list(Var1 = c("x", "y", "z"), Freq = c(5L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-3L))
使用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"
我们可以使用with
with(df1, Var1[Freq == df2$Freq])
#[1] "y" "z"
数据
df1 <- structure(list(Var1 = c("x", "y", "z"), Freq = c(1L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-3L))
df2 <- structure(list(Var1 = c("x", "y", "z"), Freq = c(5L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-3L))