Can anybody help me write a code on the following data example, which fills out all NA values by using a linear interpolation with the two closest values? Doy is day of year (%j). Code example: yr<-c(rep(2000,14)) doy<-c(16:29) dat<-c(3.2,NA,NA,NA,NA,NA,NA,5.1,NA,NA,NA,NA,NA,4.6) ta<-cbind(yr,doy,dat) ta yr doy dat [1,] 2000 16 3.2 [2,] 2000 17 NA [3,] 2000 18 NA [4,] 2000 19 NA [5,] 2000 20 NA [6,] 2000 21 NA [7,] 2000 22 NA [8,] 2000 23 5.1 [9,] 2000 24 NA [10,] 2000 25 NA [11,] 2000 26 NA [12,] 2000 27 NA [13,] 2000 28 NA [14,] 2000 29 4.6 Anette Norgaard [[alternative HTML version deleted]]
Is this what you want? > yr<-c(rep(2000,14))> doy<-c(16:29) > dat<-c(3.2,NA,NA,NA,NA,NA,NA,5.1,NA,NA,NA,NA,NA,4.6) > ta<-cbind(yr,doy,dat) > tayr doy dat [1,] 2000 16 3.2 [2,] 2000 17 NA [3,] 2000 18 NA [4,] 2000 19 NA [5,] 2000 20 NA [6,] 2000 21 NA [7,] 2000 22 NA [8,] 2000 23 5.1 [9,] 2000 24 NA [10,] 2000 25 NA [11,] 2000 26 NA [12,] 2000 27 NA [13,] 2000 28 NA [14,] 2000 29 4.6> good <- !is.na(ta[,'dat']) > x.f <- approxfun(ta[good,'doy'], ta[good,'dat'], rule=2) > ta[!good, 'dat'] <- x.f(ta[!good, 'doy']) > tayr doy dat [1,] 2000 16 3.200000 [2,] 2000 17 3.471429 [3,] 2000 18 3.742857 [4,] 2000 19 4.014286 [5,] 2000 20 4.285714 [6,] 2000 21 4.557143 [7,] 2000 22 4.828571 [8,] 2000 23 5.100000 [9,] 2000 24 5.016667 [10,] 2000 25 4.933333 [11,] 2000 26 4.850000 [12,] 2000 27 4.766667 [13,] 2000 28 4.683333 [14,] 2000 29 4.600000>On 10/6/05, Anette Nørgaard <anette@geoplus.dk> wrote:> > Can anybody help me write a code on the following data example, which > fills out all NA values by using a linear interpolation with the two > closest values? > > Doy is day of year (%j). > > Code example: > yr<-c(rep(2000,14)) > doy<-c(16:29) > dat<-c(3.2,NA,NA,NA,NA,NA,NA,5.1,NA,NA,NA,NA,NA,4.6) > ta<-cbind(yr,doy,dat) > > ta > yr doy dat > [1,] 2000 16 3.2 > [2,] 2000 17 NA > [3,] 2000 18 NA > [4,] 2000 19 NA > [5,] 2000 20 NA > [6,] 2000 21 NA > [7,] 2000 22 NA > [8,] 2000 23 5.1 > [9,] 2000 24 NA > [10,] 2000 25 NA > [11,] 2000 26 NA > [12,] 2000 27 NA > [13,] 2000 28 NA > [14,] 2000 29 4.6 > > Anette Norgaard > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]]
This is exactly what I requested, thank you!! However I do actually have several columns in my data sheet where I need to do the same thing, then how do I come about that? e.g. yr<-c(rep(2000,14)) doy<-c(16:29) dat1<-c(3.2,NA,NA,NA,NA,NA,NA,5.1,NA,NA,NA,NA,NA,4.6) dat2<-c(2.2,NA,NA,NA,NA,NA,NA,6.1,NA,NA,NA,NA,NA,4.2) dat3<-c(3.4,NA,NA,NA,NA,NA,NA,4.1,NA,NA,NA,NA,NA,4.7) ta<-cbind(yr,doy,dat1,dat2,dat3) [[alternative HTML version deleted]]
On Thu, 6 Oct 2005 16:10:15 +0200 Anette N??rgaard wrote:> This is exactly what I requested, thank you!! However I do actually > have several columns in my data sheet where I need to do the same > thing, then how do I come about that?Look at na.approx() in package zoo. Best, Z> e.g. > > yr<-c(rep(2000,14)) > doy<-c(16:29) > dat1<-c(3.2,NA,NA,NA,NA,NA,NA,5.1,NA,NA,NA,NA,NA,4.6) > dat2<-c(2.2,NA,NA,NA,NA,NA,NA,6.1,NA,NA,NA,NA,NA,4.2) > dat3<-c(3.4,NA,NA,NA,NA,NA,NA,4.1,NA,NA,NA,NA,NA,4.7) > ta<-cbind(yr,doy,dat1,dat2,dat3) > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Is doy intended to represent the number of days since the beginning of the year? In that case convert the first two columns to class Date and interpolate using approx. See ?approx for variations: tt <- as.Date(paste(yr, 1, 1, sep = "-")) + doy - 1 ta[,"dat"] <- approx(tt, dat, tt)$y Even better would be to create an irregular time series object. library(zoo) tt <- as.Date(paste(yr, 1, 1, sep = "-")) + doy - 1 ta.z <- na.approx(zoo(dat, tt)) Now ta.z is a zoo object representing your time series. coredata(ta.z) is the data and time(ta.z) are the dates. See: library(zoo) vignette("zoo") for more info. On 10/6/05, Anette N??rgaard <anette at geoplus.dk> wrote:> Can anybody help me write a code on the following data example, which > fills out all NA values by using a linear interpolation with the two > closest values? > > Doy is day of year (%j). > > Code example: > yr<-c(rep(2000,14)) > doy<-c(16:29) > dat<-c(3.2,NA,NA,NA,NA,NA,NA,5.1,NA,NA,NA,NA,NA,4.6) > ta<-cbind(yr,doy,dat) > > ta > yr doy dat > [1,] 2000 16 3.2 > [2,] 2000 17 NA > [3,] 2000 18 NA > [4,] 2000 19 NA > [5,] 2000 20 NA > [6,] 2000 21 NA > [7,] 2000 22 NA > [8,] 2000 23 5.1 > [9,] 2000 24 NA > [10,] 2000 25 NA > [11,] 2000 26 NA > [12,] 2000 27 NA > [13,] 2000 28 NA > [14,] 2000 29 4.6 > > Anette Norgaard > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
na.approx(zoo(ta[,-seq(2)], tt)) where tt is as before. On 10/6/05, Anette N??rgaard <anette at geoplus.dk> wrote:> This is exactly what I requested, thank you!! However I do actually have > several columns in my data sheet where I need to do the same thing, then > how do I come about that? > > e.g. > > yr<-c(rep(2000,14)) > doy<-c(16:29) > dat1<-c(3.2,NA,NA,NA,NA,NA,NA,5.1,NA,NA,NA,NA,NA,4.6) > dat2<-c(2.2,NA,NA,NA,NA,NA,NA,6.1,NA,NA,NA,NA,NA,4.2) > dat3<-c(3.4,NA,NA,NA,NA,NA,NA,4.1,NA,NA,NA,NA,NA,4.7) > ta<-cbind(yr,doy,dat1,dat2,dat3) > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >