I have been trying to create a new dataframe from several computations with lapply(). I have reached this so far reading several questions (1, 2, 3):
lapply(mtcars, function(x) c(colnames(x),
NROW(unique(x)),
sum(is.na(x)),
round(sum(is.na(x))/NROW(x),2)
)
)
However, colnames(x) doesn't give the colname as x it's a vector. Second, I can't figure out a way to transform this output into a dataframe:
lapply(mtcars, function(x) data.frame(NROW(unique(x)), # if I put colnames(x) here it gives an error
sum(is.na(x)),
round(sum(is.na(x))/NROW(x),2)
)
)
As you might see above, the final dataframe should follow a structure like:
| Variable_name | sum_unique | NA_count | NA_percent |