[英]identify unique values of 2 data frames in R
I am working with 2 data frames.我正在使用 2 个数据框。 I want to a file that outputs rows that appear in data frame 1, but do not appear in data frame 2. Here is sample data:
我想要一个输出出现在数据框 1 中但不出现在数据框 2 中的行的文件。这是示例数据:
df1:
id visit
094-1 2
094-2 3
0813-1 11
0813-3 22
df2:
id visit
094-1 2
094-2 3
0819-2 8
This is what I want:这就是我要的:
df3:
id visit
0819-2 8
I tried this, but it is not working.我试过这个,但它不工作。 Can someone please help?
有人可以帮忙吗?
library(tidyverse)
df1 %in% df2 -> x
df2[!x,]-> df3
In dplyr, there is a function setdiff
for this:在 dplyr 中,有一个
setdiff
设置差异:
df1 = data.frame(id=c("094-1","094-2","0813-1","0813-3"),visit=c(2,3,11,22))
df2 = data.frame(id=c("094-1","094-2","0819-2"),visit=c(2,3,8))
dplyr::setdiff(df2,df1)
id visit
1 0819-2 8
Or:或者:
library(dplyr)
setdiff(df2,df1)
Using data.table
使用
data.table
library(data.table)
fsetdiff(setDT(df2), setDT(df1))
# id visit
#1: 0819-2 8
base r
solution using a similar approach to the code included in your question. base r
解决方案,使用与您问题中包含的代码类似的方法。 This solution uses the %in%
operator but reverses it when used in combination with the !
此解决方案使用
%in%
运算符,但在与!
operator.操作员。
Data:数据:
df1 <- data.frame(
id = c("094-1", "094-2", "0813-1", "0813-3"),
visit = c(2,3,11,22)
)
df2 <- data.frame(
id = c("094-1", "094-2", "0819-2"),
visit = c(2,3, 8)
)
Code:代码:
df3 <- df2[!df2$id %in% df1$id,]
Output: Output:
df3
#> id visit
#> 3 0819-2 8
Created on 2020-11-29 by the reprex package (v0.3.0)由代表 package (v0.3.0) 于 2020 年 11 月 29 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.