Re: [R] Quick recode of -999 to NA in R

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Wed, 30 Mar 2011 14:27:20 +0100

On Wed, 2011-03-30 at 08:15 -0500, Christopher Desjardins wrote:
> 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")

dat0 <- read.table("time1.dat", na.strings = c("NA","-999"))

would seem the easiest option.

Do read ?read.table for details.

HTH G

> 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
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 30 Mar 2011 - 13:36:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 Mar 2011 - 13:50:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive