Max Powers
Max Powers

Reputation: 1

Replacing missing numbers based on multiple conditions

I have the following problem: I have a dataset with three variables. let's call them a, b and c.

I have to combine them to an additive index. But if one of the three is missing, I have to replace it with a certain value (0.5). If two or three are missing, it stays NA.

ds is the name of the dataset

for(i in ds$a) {
  if is.na(i) && !is.na(ds$b) && !is.na(ds$c)  {
    ds$a <- 0.5
  }
}

Upvotes: 0

Views: 82

Answers (1)

Shaswata_m
Shaswata_m

Reputation: 13

Would you please clarify the term "combine them to an additive index". Does it mean you want to rowsum three existing columns? And also not clear why you are replacing values to the existing variable a when the conditions are met?

I have way around of your problem that checks your NA conditions(and replaces to a new column)

 (ds <- data.frame(a= c(1,NA,2),
                  b= c(10,20,30), 
                  c= c(100,200,300)))
   a  b   c
1  1 10 100
2 NA 20 200
3  2 30 300
 
 (flag <-  sapply(ds, anyNA))  ## check whether any NA's columns-wise
    a     b     c 
 TRUE FALSE FALSE 
 
 if(sum(flag)==1){
   ds$d <- 0.5 
 }else if(sum(flag) %in% 2:3){
   ds$d <- NA
 }
 ds
   a  b   c   d
1  1 10 100 0.5
2 NA 20 200 0.5
3  2 30 300 0.5

Upvotes: 1

Related Questions