I am using the R programming language. I am interested in seeing whether the "c" statement can be used along with the "which" statement in R. For example, consider the following code (var1 and var2 are both "Factor" variables):
my_file
var1 var2
1 A AA
2 B CC
3 D CC
4 C AA
5 A BB
ouput <- my_file[which(my_file$var1 == c("A", "B", "C") & my_file$var2 !== c("AA", "CC")), ]
But this does not seem to be working.
I can run each of these conditions individually, e.g.
# not in
'%nin%' <- function(x,y)!('%in%'(x,y))
my_file <- data.frame(var1 = c("A","B","C","D","E"),
var2 = c("AA","CC","CC","AA","BB"))
my_file$var1 <- as.factor(my_file$var1)
my_file$var2 <- as.factor(my_file$var2)
my_file
#> var1 var2
#> 1 A AA
#> 2 B CC
#> 3 C CC
#> 4 D AA
#> 5 E BB
ouput <- my_file[which(my_file$var1 == c("A", "B", "C") & my_file$var2 != c("AA", "CC")), ]
#> Warning in `==.default`(my_file$var1, c("A", "B", "C")): longer object length is
#> not a multiple of shorter object length
#> Warning in is.na(e1) | is.na(e2): longer object length is not a multiple of
#> shorter object length
#> Warning in `!=.default`(my_file$var2, c("AA", "CC")): longer object length is
#> not a multiple of shorter object length
#> Warning in is.na(e1) | is.na(e2): longer object length is not a multiple of
#> shorter object length
triplet <- c("A", "B", "C")
doublet <- c("AA","CC")
my_file[which(my_file$var1 %in% triplet & my_file$var2 %nin% doublet),]
#> [1] var1 var2
#> <0 rows> (or 0-length row.names)
The difficulty lies in the lack of intersection between the two conditions. There are no rows that have"A", "B" or "C" that don't ALSO have "AA" or "CC".