In case there are different types the replacement should be done columnwise. Another simple way allowing in place exchange might be.
for(i in seq_along(dfa)) {
. <- is.na(dfa[[i]])
dfa[[i]][.] <- dfrepair[[i]][.]
}
Or using in addition which
which might improve speed / memory usage in some cases.
for(i in seq_along(dfa)) {
. <- which(is.na(dfa[[i]]))
dfa[[i]][.] <- dfrepair[[i]][.]
}
Benchmark of columnwise base options.
dfa <- data.frame(a=c("A",NA,"B","C","D",NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA))
dfrepair <- data.frame(a=letters[2:7],b=c(6:1),c=c(8:3))
bench::mark(
akrun = local({dfa[] <- Map(function(x,y) {x[is.na(x)] <- y[is.na(x)]; x}, dfa, dfrepair); dfa}),
GKi = local({for(i in seq_along(dfa)) {. <- is.na(dfa[[i]])
dfa[[i]][.] <- dfrepair[[i]][.]}
dfa})
)
# expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time
# <bch:expr> <bch:tm> <bch:> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm>
#1 akrun 64.4µs 70.3µs 12895. 280B 26.7 5793 12 449ms
#2 GKi 54.8µs 60µs 16347. 280B 28.7 7395 13 452ms
replace(dfa, is.na(dfa), dfrepair[is.na(dfa)])
– Sulphurbottom