[R] format.data.frame and NA control

Sebastian P. Luque
Mon, 22 Oct 2007 17:16:10 -0000

Hi,

Is there a more efficient way to output NA strings as empty strings in
format.data.frame than this:

---<---------------cut here---------------start-------------->---
R> tt <- data.frame(a=c(NA, rnorm(8), NA), b=c(NA, letters[1:8], NA))
R> tt <- format(tt, digits=5, trim=TRUE)
R> tt
           a  b
1         NA NA
2   2.012460  a
3   0.364181  b
4   1.398317  c
5   0.730969  d
6  -1.321741  e
7   0.081472  f
8   2.019201  g
9   0.090003  h
10        NA NA
R> as.data.frame(lapply(tt, function(x) {x[x == "NA"] <-
""; x}))
           a b
1
2   2.012460 a
3   0.364181 b
4   1.398317 c
5   0.730969 d
6  -1.321741 e
7   0.081472 f
8   2.019201 g
9   0.090003 h
10
---<---------------cut here---------------end---------------->---

Thanks.


--
Seb