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.