0

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'.

sfetan
  • 109
  • 7

3 Answers3

3

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.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

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
r2evans
  • 141,215
  • 6
  • 77
  • 149
1

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"
akrun
  • 874,273
  • 37
  • 540
  • 662