[R] Odp: (no subject)

Petr PIKAL petr.pikal at precheza.cz
Thu Aug 14 18:00:04 CEST 2008


Hi


r-help-bounces at r-project.org napsal dne 14.08.2008 16:34:32:

> 
> Hi, I am working with R and I have some doubts. I working with daily 
data of 
> MNEE parameter for 6 years (2001:2006). What I am trying to do is to 
replace 
> missing data for a particular period of time of a particular year, with 
the 
> mean of that variable for the rest of the years during that given 
period.For 
> instance if I have missing data during the period 100:1000 during the 
year 
> 2002 I want to replace that gap with the mean of the other five years. 
> Apparently it is working but after running it gives an error:  Error in 
if 
> (trim > 0 && n > 0) { : missing value where TRUE/FALSE neededWhat does 
it 
> mean? Because after doing the means there is not NA values. I attach the 
code 
> I wrote. If you need any explanation about the variables, please tell 
me.

try

debug(name.of.your.function)

and see what it is actually doing. My guess is that using na.rm = FALSE in 
mean computation does not compute mean but results in NA. 

Based on your description I would use other approach.
Preparing faked data

period <- rep(1:5,10)
year <- rep(2001:2010, each=5)
daf <- data.frame(year, period, value=rnorm(50))
daf$value[c(10,17,25)] <- NA

repl <- tapply(daf$value, daf$period, mean, na.rm=T)
# computing mean for each period in all years
not <- which(is.na(daf$value))
# estimating which values are necessary to replace

daf$value[not] <- repl[daf$period[not]]
# replacing those values with corresponding means

Is this what you want?

Regards
Petr


> 
> All my best
> 
> Laura
> 
> 
> _________________________________________________________________
> 
> 
> [příloha period1.txt odstraněna uživatelem Petr PIKAL/CTCAP] 
> ______________________________________________
> 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.



More information about the R-help mailing list