My problem in R is that I want to replace values in a data frame by checking a condition for any entry (namely if it is NA) and subsequently inserting a value selected from a list. The nested for loops seem to work but they are way too slow!
for (i in 1:nrow(data)) {
for (j in 1:ncol(data)) {
if (is.na(data[i,j]) == FALSE){
df[i,j] <- some_list[[j]]$freq[ which(some_list[[j]][1] == data[i,j]) ]
}
if (is.na(data[i,j]) == TRUE){
df[i,j] <- NA
}
}
}
I would prefer a solution using apply in combination with ifelse or any other fast approach, but I don't see it unfortunately.