-1

I want to create a variable 'oth' which is TRUE if none of the variables 'com', 'edit' or 'cont' are TRUE. The three variables are logical vectors taking the values TRUE or FALSE.

I tried to do 2 things:

data$oth  <- all(!data$com, !data$edit, !data$cont)
data$oth  <- all(data$com==F, data$edit==F, data$cont==F)

None worked, i.e., $oth was just a vector of all FALSE. Are there an reverse to the all() function in R or how should I do this?

jpsmith
  • 11,023
  • 5
  • 15
  • 36
EmilA
  • 11
  • 2
  • Try `any()` maybe. – user2974951 Feb 21 '23 at 14:42
  • 1
    Try `!all(...)`? If you share reproducible data it will help us help you – jpsmith Feb 21 '23 at 14:43
  • any() won't work, as I need all to be FALSE, not just 1. !all() produces a vector of all TRUE, so does not work either – EmilA Feb 21 '23 at 14:45
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 21 '23 at 15:51

3 Answers3

2

You can use !any.

!any(TRUE, TRUE, TRUE)
#[1] FALSE

!any(FALSE, FALSE, TRUE)
#[1] FALSE

!any(FALSE, FALSE, FALSE)
#[1] TRUE

!any(c(FALSE, FALSE), c(FALSE, FALSE), c(FALSE, FALSE))
#[1] TRUE

!any(c(TRUE, FALSE), c(FALSE, FALSE), c(FALSE, FALSE))
#[1] FALSE

But it looks like that you want a result per row.

!(c(FALSE, FALSE) | c(FALSE, FALSE) | c(FALSE, FALSE))
#[1] TRUE TRUE

!(c(TRUE, FALSE) | c(FALSE, FALSE) | c(FALSE, FALSE))
#[1] FALSE  TRUE
GKi
  • 37,245
  • 2
  • 26
  • 48
0

Is this what you are looking for?

data$oth  <- all(data$com==F) & all(data$edit==F) & all(data$cont==F)
cory
  • 6,529
  • 3
  • 21
  • 41
0

To match your criteria of assigning TRUE if all values in the row are FALSE you can simply use:

dt$oth <- rowSums(dt) == 0
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22