0

How can I select all the rows of those IDs, that have at least one B as Item?

df<-cbind(c(1,1,1,1,1,2,2,2,2,3,3),c("A","A","B","A","A","B","A","A","B","A","A"))
colnames(df)<-cbind("ID","ITEM")
df
flozygy
  • 83
  • 2
  • 8

2 Answers2

1

Not sure if this is what you mean, but:

df[df[, 2]=="B", ]
NewUser
  • 336
  • 2
  • 10
  • thx, but please read my specification:I select all the rows of those IDs, that have at least one B as Item, for this example it means: give me all rows for ID 1 and ID 2 (but no rows for ID 3, because ID 3 has no B in any Item). your query just results in all rows with a B. – flozygy Jul 27 '18 at 12:41
0

Its easy if you construct a data frame rather than working with a list.

library(dplyr)
df<-data.frame(ID = c(1,1,1,1,1,2,2,2,2,3,3),ITEM=c("A","A","B","A","A","B","A","A","B","A","A"));

df %>% filter(ITEM == "B");
MSW Data
  • 441
  • 3
  • 8
  • thx, but please read my specification: select all the rows of those IDs, that have at least one B as Item, for this example it means: give me all rows for ID 1 and ID 2 (but no rows for ID 3, because ID 3 has no B in any Item). your query just results in all rows with a B. – flozygy Jul 27 '18 at 12:55
  • right answer is given by Ronak Shah – flozygy Jul 27 '18 at 12:55