1

Example Dataset

ID <- c("A", "B", "C", "D", "E")
AmountofW <- c(NA, NA, NA, NA, NA)
AmountofX <- c(NA, 30, NA, 1, NA)
AmountofY <- c(NA, NA, 2, NA, NA)
AmountofZ <- c(NA, 15, NA, NA, 20)

datt <- data.frame(ID, AmountofW, AmountofX, AmountofY, AmountofZ)

  ID AmountofW AmountofX AmountofY AmountofZ
1  A        NA        NA        NA        NA
2  B        NA        30        NA        15
3  C        NA        NA         2        NA
4  D        NA         1        NA        NA
5  E        NA        NA        NA        20

What I want to do is create another column "Positives" with a factor variable of length 1 where "1" is returned if any of columns 2:5 have a value >=10, else a 0.

I have tried to use

mutate(data, Positives=ifelse(data[2:5] >= 10 & !is.na(data[2:5], 1, 0)

but this produces a column of all 0s, presumably because it is testing that all of the subset have the value

I have read the mutate and ifelse documentation and searched for an answer and many come close but it is the value being in any of a range of columns that I am stuck on

my intended output would be:

  ID AmountofW AmountofX AmountofY AmountofZ Positive
1  A        NA        NA        NA        NA        0
2  B        NA        30        NA        15        1
3  C        NA        NA         2        NA        0
4  D        NA         1        NA        NA        0
5  E        NA        NA        NA        20        1

Thanks

David Cox
  • 13
  • 3

1 Answers1

1

Here is an option using rowSums

datt$Positive <- as.integer(rowSums(datt[-1] >= 10, na.rm = TRUE)>0)
datt$Positive
#[1] 0 1 0 0 1
akrun
  • 874,273
  • 37
  • 540
  • 662