Suppose we have a nested list:
test <- list( list(a = 1, b = 2, c = NULL), list(a = NULL, b = 2, c = 3))
How to replace all NULL values ββwith, for example, NA to preserve the data structure? So I do not lose value / structure when I try to make a data frame from a list. For example:
data.frame(matrix(unlist(test), nrow = 2, byrow = T)) X1 X2 1 1 2 2 2 3
The desired output looks something like this:
X1 X2 X3 1 1 2 NA 2 NA 2 3
There are suggestions to do this as follows:
rbind.fill(lapply(test, function(f) { as.data.frame(Filter(Negate(is.null), f)) }))
This is not quite as we would like. Obviously, size and performance are a problem. The one workaround that comes to mind replaces all NULL values ββin the same way as you can do for the entire data frame at a time. And then unlist() and matrix() list.
I'm not sure about the performance gains (if any). Perhaps the good old lapply() not so bad.
r
A.Val.
source share