Skip to content

Commit fe98147

Browse files
committed
Simplified return value
1 parent 2623961 commit fe98147

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

R/mergeNA.r

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ mergeNA <- function(...) {
55
lengths <- sapply(vars,FUN=length)
66
# check variable lengths
77
if(!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)
1111
amat <- is.na(a)
1212
# check for mutual missigness
1313
mutual <- rowSums(!amat) > 1 #mutual <- apply(!amat, 1, sum) > 1
@@ -21,10 +21,7 @@ mergeNA <- function(...) {
2121
notNA <- lapply(seq_len(ncol(notNA)), function(i) notNA[,i])
2222
# pairs of NA positions and mergeable values
2323
p <- 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

Comments
 (0)