0

I want to run a loop using ifelse() and including NA in R. Here is a example of my dataset named DATA:

[,1]   [,2]    [,3]    [,4]
ID1     NA      NA      NA
ID2     0       0       0
ID3     1       1       1
ID4     0       0       1
ID5     1       1       0
ID6     0       NA      1
ID7     NA      0       0
ID8     NA      1       1

Basically, I want to add a new column at the end using these criteria:

  • If there is at least one "1" in column 2, 3 or 4 => 1
  • If there is NO "1" in column 2, 3 or 4, but at least one "0" => 0
  • If there is only "NA" in column 2, 3 or 4 => NOTHING

I thought to use something like that, but it doesn't work. It returns:

"NA" if there is no "1", but at least one "0", instead of
"NOTHING"

Example for line 6:

*ifelse(any((DATA[6,c(2:4)])==1), "1", ifelse(any((DATA[6,c(2:4)])==0), "0",
"NOTHING"))*

I don't really want to return "NOTHING". In reality I want "NA", but to verify if a "NA" was the result of something that doesn't work, I used "NOTHING". I also want to loop the whole thing for each line in my data set, but I'm not very good at it either.

1 Answers1

1
rowSums.na <-
    function(x, na.rm = T){

        suma <- rowSums(x, na.rm = na.rm)
        ind  <- apply(x, 1, function(el){ all(is.na(el)) })

        suma[ind] <- NA

        return(suma)
}

df1$newCol<- +(rowSums.na(df1[,2:4]) > 0)

#> df1$newCol
#[1] NA  0  1  1  1  1  0  1

  • df1 is your data.
  • normal rowSums/ sum returns 0 when all values are NA. A great mistake IMO. Eval:

    1. rowSums(matrix(NA, 3, 3), na.rm = T)
    2. rowSums.na(matrix(NA, 3, 3))
  • +( ... ) will convert the boolean into a numeric type. run +( c(TRUE, FALSE, TRUE) ) in R console.

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69