I want to replace all the lists that are empty (numeric(0)) with the value 0 in the following list:
a <- list(numeric(0), 3.13887804749505, c(0.745977548064631, 15.7233179232099,
4.32068483740438, 19.6680377065919, 9.24007013740377), numeric(0),
c(28.8670111833615, 1.27199935252619, 26.6173612819351, 46.8824614685704
), c(3.03425142063166, 3.08366863855608, 4.37959434697201,
4.00518501422067, 2.05826729526789, 2.29413068424335))
I was trying this:
b <- lapply(a, function(x) ifelse(length(x)==0,0,x))
But I get the first number from every list:
list(0, 3.13887804749505, 0.745977548064631, 0, 28.8670111833615,
3.03425142063166)
Is there a way to do this with apply and not with a loop? The loop takes a very long time (the list is very large).