Curious G.
Curious G.

Reputation: 877

Select rows based in rows of another data.frame

I have these following data.frames:

dt1

Id  Mother Weight  
1    elly     10
2    bina     20
3    sirce    30
4    tina     30
5    lina     40

and

dt2

Id   Mother  Weight  sex  
1    elly     10      M
2    bina     20      F
3    sirce    30      F

And I would like select rows from DT1 (ID) based in DT2 (ID), this way:

new.dt

Id   Mother  Weight  sex
4    tina     30     NA
5    lina     40     NA

Upvotes: 1

Views: 315

Answers (2)

user11450027
user11450027

Reputation: 66

transform(dt1[!dt1$Id %in% dt2$Id,], sex = NA)
#  Id Mother Weight sex
#4  4   tina     30  NA
#5  5   lina     40  NA

d = merge(dt1, dt2, all = TRUE)
d[is.na(d$sex),]
#  Id Mother Weight  sex
#4  4   tina     30 <NA>
#5  5   lina     40 <NA>

Upvotes: 1

akrun
akrun

Reputation: 886958

Here is one option with anti_join

library(dplyr)
anti_join(dt1 %>% 
           mutate(sex = NA), dt2, by = 'Id')
#   Id Mother Weight sex
#1  4   tina     30  NA
#2  5   lina     40  NA

data

dt1 <- structure(list(Id = 1:5, Mother = c("elly", "bina", "sirce", 
"tina", "lina"), Weight = c(10L, 20L, 30L, 30L, 40L)), 
   class = "data.frame", row.names = c(NA, 
-5L))


dt2 <- structure(list(Id = 1:3, Mother = c("elly", "bina", "sirce"), 
    Weight = c(10L, 20L, 30L), sex = c("M", "F", "F")), 
  class = "data.frame", row.names = c(NA, 
-3L))

Upvotes: 1

Related Questions