[R] Summary: Process multiple columns of data.frame

Gregor Gorjanc gregor.gorjanc at bfro.uni-lj.si
Fri Nov 23 09:42:24 CET 2007


Thompson, David (MNR <David.John.Thompson <at> ontario.ca> writes:
> Thank you Jim Holtman and Mark Leeds for your help.
> 
> Original question:
> >How do I do the following more concisely?
> >	Bout[is.na(Bout$bd.n), 'bd.n'] <- 0
> >	Bout[is.na(Bout$ht.n), 'ht.n'] <- 0
> >	Bout[is.na(Bout$dbh.n), 'dbh.n'] <- 0
> >. . . 
> 
> Solution:
> for (i in c('bd.n', 'ht.n', 'dbh.n')) Bout[is.na(Bout[[i]]), i] <- 0

ABove solution is completely OK, but can be cumbersome. I wrote functions
NAToUnknown() and unknownToNA() with exactly the same problem in mind. 
Take a look in gdata package. I also described the function in RNews

G. Gorjanc. Working with unknown values: the
gdata package. R News, 7(1):24–26, 2007.
http://CRAN.R-project.org/doc/Rnews/Rnews_2007-1.pdf.

For your example try the following:

library(gdata)

df.0 <- as.data.frame( cbind( 
c1=c(NA, NA, 10, NA, 15, 11, 12, 14, 14, 11), 
c2=c(13, NA, 16, 16, NA, 12, 14, 19, 18, NA), 
c3=c(NA, NA, 11, 19, 17, NA, 11, 16, 20, 13), 
c4=c(20, NA, 15, 11, NA, 15, NA, 13, 14, 15), 
c5=c(14, NA, 13, 16, 17, 17, 16, NA, 15, NA), 
c6=c(NA, NA, 13, 11, NA, 16, 15, 12, NA, 20)) )

df.0
   c1 c2 c3 c4 c5 c6
1  NA 13 NA 20 14 NA
2  NA NA NA NA NA NA
3  10 16 11 15 13 13
4  NA 16 19 11 16 11
5  15 NA 17 NA 17 NA
6  11 12 NA 15 17 16
7  12 14 11 NA 16 15
8  14 19 16 13 NA 12
9  14 18 20 14 15 NA
10 11 NA 13 15 NA 20

NAToUnknown(df.0, unknown=0)
   c1 c2 c3 c4 c5 c6
1   0 13  0 20 14  0
2   0  0  0  0  0  0
3  10 16 11 15 13 13
4   0 16 19 11 16 11
5  15  0 17  0 17  0
6  11 12  0 15 17 16
7  12 14 11  0 16 15
8  14 19 16 13  0 12
9  14 18 20 14 15  0
10 11  0 13 15  0 20



More information about the R-help mailing list