@@ -5,9 +5,9 @@ mergeNA <- function(...) {
55lengths <- sapply(vars ,FUN = length )
66# check variable lengths
77if (! identical(rep(lengths [1 ],length(vars )), lengths ))
8- stop(" Vectors specified in '...' have different lengths" )
8+ stop(" Vectors specified have different lengths" )
99# test for NAs in each vector
10- a <- do.call(cbind ,a )
10+ a <- do.call(cbind ,vars )
1111amat <- is.na(a )
1212# check for mutual missigness
1313mutual <- rowSums(! amat ) > 1 # mutual <- apply(!amat, 1, sum) > 1
@@ -21,10 +21,7 @@ mergeNA <- function(...) {
2121notNA <- lapply(seq_len(ncol(notNA )), function (i ) notNA [,i ])
2222# pairs of NA positions and mergeable values
2323p <- mapply(function (val ,pos ) val [pos ], vars , notNA , SIMPLIFY = FALSE )
24- pairs <- cbind(unlist(notNA ),unlist(p ))
25- # start with first variable
26- out <- vars [[1 ]]
27- # replace NAs, if applicable with values from `pairs`
28- out [pairs [,1 ]] <- pairs [,2 ]
29- return (out )
24+ # replace NAs, if applicable
25+ vars [[1 ]][unlist(notNA )] <- unlist(p )
26+ return (vars [[1 ]])
3027}
0 commit comments