Home > Net >  Finding a missing match in two columns of a data frame in R
Finding a missing match in two columns of a data frame in R

Time:09-21

I got:

a <- c('A','A','B','B','C','C','D','D')
b <- c(1,2,1,2,2,3,1,2)
frame1 <- data.frame(a,b)

And now I want to know which string in column 'a' got no number matching '1' in column 'b'. In this case it would be 'C'.

CodePudding user response:

Here is a base R option -

with(frame1, setdiff(unique(a), a[b == 1]))
#[1] "C"

Extract a values where b = 1 and use setdiff to return the a values not in them.

CodePudding user response:

base R

frame1[ave(frame1$b, frame1$a, FUN = function(z) !1 %in% z) > 0,]
#   a b
# 5 C 2
# 6 C 3

dplyr

library(dplyr)
frame1 %>%
  group_by(a) %>%
  filter(!1 %in% b)
# # A tibble: 2 x 2
# # Groups:   a [1]
#   a         b
#   <chr> <dbl>
# 1 C         2
# 2 C         3

CodePudding user response:

We can use data.table

library(data.table)
setDT(frame1)[, .SD[!any(b %in% 1)], a]
   a b
1: C 2
2: C 3

Or in base R

unique(subset(frame1, !a %in% a[b == 1])$a)
[1] "C"
  • Related