Combining two vectors for each element

I have 2 vectors, for example:

A <- c(1,2,NA,NA,NA,NA,7) B <- c(NA,NA,3,4,NA,NA,7) 

I would like to combine them so that the resulting vector is

 1,2,3,4,NA,NA,-1 

it

  • when only 1 value (for example, X) exists in any vector at position i (the other is NA), the new vector should take the value X at position i.

  • when both values ​​are NA at position i, the new vector should take the value NA at position i

  • when both vectors have a value at position i, the new vector should take the value -1 at position i.

I can easily do this with a loop, but it is very slow on a large dataset, so can anyone provide a quick way to do this?

+6
source share
3 answers

These commands create a vector:

 X <- A X[is.na(A)] <- B[is.na(A)] X[is.na(B)] <- A[is.na(B)] X[!is.na(A & B)] <- -1 #[1] 1 2 3 4 NA NA -1 
+8
source
 A <- c(1,2,NA,NA,NA,NA,7) B <- c(NA,NA,3,4,NA,NA,7) C <- rowMeans(cbind(A,B),na.rm=TRUE) C[which(!is.na(A*B))]<- -1 #[1] 1 2 3 4 NaN NaN -1 

Landmarks:

 Unit: microseconds expr min lq median uq max 1 Roland(A, B) 17.863 19.095 19.710 20.019 68.985 2 Sven(A, B) 11.703 13.243 14.167 14.783 100.398 
+4
source

A bit late for the batch, but here is another option defining a function that works by applying the rules to two cbind -ed vectors together.

 # get the data A <- c(1,2,NA,NA,NA,NA,7) B <- c(NA,NA,3,4,NA,NA,7) # define the function process <- function(A,B) { x <- cbind(A,B) apply(x,1,function(x) { if(sum(is.na(x))==1) {na.omit(x)} else if(all(is.na(x))) {NA} else if(!any(is.na(x))) {-1} }) } # call the function process(A,B) #[1] 1 2 3 4 NA NA -1 

The main advantage of using a function is that it is easier to update rules or inputs to apply code to new data.

+1
source

All Articles