Reputation: 287
I have a one dataframe with 3719 (actual data) rows and another with 3721 (from coding) rows. I got 2 extra observations.
I have tried with setdiff but it is giving zero rows
dplyr::setdiff(d1,d2)
o/p: [1] col1 col2 col3 col4
[5] col5 col6
<0 rows> (or 0-length row.names)
I have tried vise versa also,i.e,
dplyr::setdiff(d2,d1)
o/p: [1] col1 col2 col3 col4
[5] col5 col6
<0 rows> (or 0-length row.names)
How to identify those 2 extra observation in R?
Upvotes: 0
Views: 69
Reputation: 5456
Anti-join would be the "tidy" option:
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
Upvotes: 2
Reputation: 2770
Option 1 You can use the %in% operator
#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) , ]
Option 2 - use merge with all=T
a$flaga <- 1
b$flagb <- 1
d <- merge( a[ ,c("id","flaga")] , b[ ,c("id","flagb")], by= "id" , all=T)
d[ is.na(d$flaga) | is.na(d$flagb) , "id" ]
Upvotes: 0