[R] Summary: Process multiple columns of data.frame
Thompson, David (MNR)
David.John.Thompson at ontario.ca
Thu Nov 22 14:20:01 CET 2007
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
Toy example:
> 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.1 <- df.0
> for (i in c('c2', 'c3', 'c4')) df.1[is.na(df.1[[i]]), i] <- 0
> 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
> df.1
c1 c2 c3 c4 c5 c6
1 NA 13 0 20 14 NA
2 NA 0 0 0 NA NA
3 10 16 11 15 13 13
4 NA 16 19 11 16 11
5 15 0 17 0 17 NA
6 11 12 0 15 17 16
7 12 14 11 0 16 15
8 14 19 16 13 NA 12
9 14 18 20 14 15 NA
10 11 0 13 15 NA 20
Thank you, DaveT.
*************************************
Silviculture Data Analyst
Ontario Forest Research Institute
Ontario Ministry of Natural Resources
david.john.thompson at ontario.ca
http://ofri.mnr.gov.on.ca
More information about the R-help
mailing list