I have a data.frame with NA's and I would like to coalesce them relative to their location in the data.frame and finally if all in a row are missing then use some arbitray value I provide :
I've come up with this code which works recursively on the supplied ...
argument.
I'm pretty sure there is a better idea though, or a built in function.
coalesce <- function(x,...) {
fillerList <- list(...)
y <- try(y <- unlist(..1))
if(class(y)=="try-error" | length(y)==0L) {
x <- x
}
else if(length(y)==1L) {
x[is.na(x)] <- y
}
else {
x[is.na(x)] <- y[is.na(x)]
}
# recursion
if(length(fillerList)-1L<=0L) {return(x)}
else {return(coalesce(x,fillerList[-1]))}
}