0

I try to do a subset from an original data base but I don't understand why I have some different results with lines that seem the same. Let's take an example :

     a <- data.frame(c("1091Z","4773Z","4725Z","4781Z","4789Z","4725Z","4779Z","4789Z",
            "1071D","4789Z"))
     colnames(a) <- "name"
     b<- subset(a, name == c("1071D","4789Z")) #3 obs. of 1 variables
     d<- subset(a, name == c("4789Z","1071D")) #1 obs. of 1 variables
     c<- subset(a, name == "4789Z" |name =="1071D") #4 obs. of 1 variables

The results are the same if I use filter in stead of subset.

Augustin A
  • 55
  • 4
  • 7
    You are looking for `%in%`, do `name %in% c("1071D", "4789Z")`. `==` is pairwise, 1st on left vs 1st on right, 2nd on left vs 2nd on right, ... with `%in%` it is 1st on left vs anywhere on right, 2nd on left vs anywhere on right... – Gregor Thomas Aug 06 '18 at 14:20
  • 2
    I would do some Googling on the R behavior called "recycling", look up the `%in%` operator and run `a$name == c("1071D","4789Z")` at the console. That will all get you started. – joran Aug 06 '18 at 14:21
  • 1
    The "real" question is: [What is the difference between `==` and `%in%`?"](https://stackoverflow.com/questions/15358006/difference-between-in-and) – zx8754 Aug 06 '18 at 14:57
  • Possible duplicate https://stackoverflow.com/questions/15358006/difference-between-in-and – zx8754 Aug 06 '18 at 14:59

0 Answers0