R: merge copies of the same variable

I have data similar to this in R:

subjID = c(1,2,3,4) var1 = c(3,8,NA,6) var1.copy = c(NA,NA,5,NA) fake = data.frame(subjID = subjID, var1 = var1, var1 = var1.copy) 

which is as follows:

 > fake subjID var1 var1.1 1 1 3 NA 2 2 8 NA 3 3 NA 5 4 4 6 NA 

Var1 and Var1.1 represent the same variable, so each object has an NA for one column and a numerical value in the other (who does not have two NA or two numbers). I want to combine the columns to get one Var1: (3, 8, 5, 6).

Any tips on how to do this?

+3
source share
3 answers

You can use is.na, which can be vectorized as:

 # get all the ones we can from var1 var.merged = var1; # which ones are available in var1.copy but not in var1? ind = is.na(var1) & !is.na(var1.copy); # use those to fill in the blanks var.merged[ind] = var1.copy[ind]; 
+2
source

If you are dealing with only two columns, and there are never two numbers or two NA, you can calculate the average value of the row and ignore the missing values:

 fake$fixed <- rowMeans(fake[, c("var1", "var1.1")], na.rm=TRUE) 
+3
source

It depends on how you want to come together if there are conflicts.

You can simply put all non-NA values ​​in var.1.1 into the corresponding var1 interval. In case of conflict, this will contribute to var.1.1 .

 var1[!is.na(var1.copy)] <- var1.copy[!is.na(var1.copy)] 

You can simply populate all the NA values ​​in var1 with the corresponding var1.1 values. In case of conflict, this will contribute to var1 .

 var1[is.na(var1)] <- var1.copy[is.na(var1)] 
+2
source

Source: https://habr.com/ru/post/1207796/


All Articles