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
[[alternative HTML version deleted]]
Try:
dat0 <- read.table('tim1.dat', na = -999)
On Wed, Mar 30, 2011 at 10:15 AM, Christopher Desjardins
<cddesjardins at gmail.com> 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")
>
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paran?-Brasil
25? 25' 40" S 49? 16' 22" O
On 30.03.2011 15:15, 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, > ChrisI think read.table(na.string="-999.00000000") is. Uwe Ligges> > 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 > > [[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.
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
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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
It could be done in a large number of ways depending on how often you
need it etc.
You might take a look at defmacro in package gtools:
# library(gtools)
setNA <- macro(df, var, values)
{
df$var[df$var %in% values] <- NA
}
then instead of> dat0[dat0$e1dq==-999.00000000,"e1dq"] <- NA
you could have
setNA(dat0, e1dq, -999)
Otherwise, with lapply, you could have
dat0.na_ized <- lapply(dat0, function(x) x[x%in% 999.00000] <- NA)
(but I didn't try this so I can't tell if you need to use
as.data.frame on the result or not)
Kenn
On Wed, Mar 30, 2011 at 4:15 PM, Christopher Desjardins
<cddesjardins at gmail.com> 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")
>
> 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.
>
Try using a loop like the following
dat0 <- read.table("time1.dat")
id <- c("e1dq", "e1arcp", "e1dev",
"s1prcp", "s1nrcp","s1ints","a1gpar",
"a1pias", "a1devt")
for (a in 1:length(id)) {
dat0[dat0$id[a]==-999.00000000,as.character(id[a])] <- NA
}
--
Muhammad Rahiz
Researcher & DPhil Candidate (Climate Systems & Policy)
School of Geography & the Environment
University of Oxford
On Wed, 30 Mar 2011, 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")
>
> 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.
>