[R] Quick recode of -999 to NA in R
Bernd Weiss
bernd.weiss at uni-koeln.de
Wed Mar 30 15:26:44 CEST 2011
Am 30.03.2011 09:15, schrieb Christopher Desjardins:
> Hi,
> I am trying to write a loop to recode my data from -999 to NA in R. What's
> the most efficient way to do this? Below is what I'm presently doing, which
> is inefficient. Thanks,
> Chris
>
>
> dat0<- read.table("time1.dat")
>
> colnames(dat0)<- c("e1dq", "e1arcp", "e1dev", "s1prcp", "s1nrcp", "s1ints",
> "a1gpar", "a1pias", "a1devt")
>
> dat0[dat0$e1dq==-999.00000000,"e1dq"]<- NA
>
> dat0[dat0$e1arcp==-999.00000000,"e1arcp"]<- NA
>
> dat0[dat0$e1dev==-999.00000000,"e1dev"]<- NA
>
> dat0[dat0$s1prcp==-999.00000000,"s1prcp"]<- NA
>
> dat0[dat0$s1nrcp==-999.00000000,"s1nrcp"]<- NA
>
> dat0[dat0$s1ints==-999.00000000,"s1ints"]<- NA
>
> dat0[dat0$a1gpar==-999.00000000,"a1gpar"]<- NA
>
> dat0[dat0$a1pias==-999.00000000,"a1pias"]<- NA
>
> dat0[dat0$a1devt==-999.00000000,"a1devt"]<- NA
>
Given that all your variables share the same code for missing values,
e.g. -99, you could do the following:
df <- data.frame(a = c(1, 2, -99),
b = c(33, -99, 22),
c = c(-99, -99, 101))
df
df[df == -99] <- NA
df
HTH,
Bernd
More information about the R-help
mailing list