How to clean rectangular dataframes full of NA values in R

Problem: how to clean a rectangular dataframe full of NA values without remove the rows or columns?

> messy <- data.frame(a = c(1, NA, 2), b = c(2, NA, 3), c = c(NA, "A", "B"))
> messy
   a  b    c
1  1  2 <NA>
2 NA NA    A
3  2  3    B
> clean <- as.data.frame(lapply(messy, na.omit))
> clean
  a b c
1 1 2 A
2 2 3 B

Brilliant, kudos to Gregor

Comments