Can the "c" statement be used along with the "which" statement?

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.

output <- my_file[which(my_file$var1 == "A" | my_file$var1 == "B" | my_file$var1 == "C"), ]
output1 <- output[which(output$var2 == "AA" | output$var2 == "CC" ), ]

But I would like to run them in a more "compact" form, e.g.:

ouput <- my_file[which(my_file$var1 == c("A", "B", "C") & my_file$var2 !== c("AA", "CC")), ]

Can someone please tell me what I am doing wrong?

Thanks

Try
any(my_file$var1 == c("A", "B", "C"))
and see if that helps.

1 Like
# 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".

1 Like