Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何打印存在于一个数据帧中且在另一个数据帧中丢失的观测值?_R_Dplyr_Set Difference - Fatal编程技术网

R 如何打印存在于一个数据帧中且在另一个数据帧中丢失的观测值?

R 如何打印存在于一个数据帧中且在另一个数据帧中丢失的观测值?,r,dplyr,set-difference,R,Dplyr,Set Difference,我有一个包含3719(实际数据)行的数据帧和另一个包含3721(来自编码)行的数据帧。我有两个额外的观察结果 我尝试过使用setdiff,但它没有给出任何行 dplyr::setdiff(d1,d2) o/p: [1] col1 col2 col3 col4 [5] col5 col6 <0 rows> (or 0-length row.names) dplyr::setdiff(d1,d2) o/p:[1]第1列第2列第3

我有一个包含3719(实际数据)行的数据帧和另一个包含3721(来自编码)行的数据帧。我有两个额外的观察结果

我尝试过使用setdiff,但它没有给出任何行

dplyr::setdiff(d1,d2)

o/p: [1] col1 col2 col3 col4       
     [5] col5 col6                
<0 rows> (or 0-length row.names)
dplyr::setdiff(d1,d2) o/p:[1]第1列第2列第3列第4列 [5] col5 col6 (或长度为0的行名称) 我也试过vise versa,即

dplyr::setdiff(d2,d1)

o/p: [1] col1 col2 col3 col4       
     [5] col5 col6                
<0 rows> (or 0-length row.names)
dplyr::setdiff(d2,d1) o/p:[1]第1列第2列第3列第4列 [5] col5 col6 (或长度为0的行名称)
如何识别R中的这两个额外观察值?

选项1您可以使用%in%运算符

#Make Fake Data
a <- mtcars
b <- mtcars[ 3:nrow(mtcars) , ] 

a$id <- rownames( a )
b$id <- rownames( b )

#In A not B
a[ !(a$id %in% b$id) , ]
#In B not A
b[ !(b$id %in% a$id) , ]
#伪造数据

选项1您可以使用%in%运算符

#Make Fake Data
a <- mtcars
b <- mtcars[ 3:nrow(mtcars) , ] 

a$id <- rownames( a )
b$id <- rownames( b )

#In A not B
a[ !(a$id %in% b$id) , ]
#In B not A
b[ !(b$id %in% a$id) , ]
#伪造数据
反连接将是“整洁”选项:

库(tidyverse)
d1反连接将是“整洁”选项:

库(tidyverse)

d1“数据帧”是如何定义的?tibbles、tribbles、data.frames等。。。?请参阅此问题:您可以尝试
which(d1$col1[1:3719]!=d2$col1[1:3719])[1]
以获取
col1
中的第一个不匹配项如何定义“data.frames”?tibbles、tribbles、data.frames等。。。?请参阅此问题:您可以尝试
哪个(d1$col1[1:3719]!=d2$col1[1:3719])[1]
来获取
col1
中的第一个不匹配条目谢谢。了解了两个缺失的观测谢谢。了解了两个缺失的观测
library(tidyverse)

d1 <- tribble(~a, ~b,
              "a", 3,
              "f", 9,
              "g", 10)

d2 <- tribble(~a, ~b,
              "a", 333,
              "b", 999,
              "f", 444,
              "g", 111)

d2 %>%
  anti_join(d1, by = "a")

# A tibble: 1 x 2
# a         b
# <chr> <dbl>
#   1 b       999