Re: [R] interpolation
If your data is in data.txt file you can do the following: x - read.table(file=data.txt,header=TRUE) t-ISOdatetime(x[,1],x[,2],x[,3],x[,4],x[,5],x[,6]) secs - as.numeric(t) So now secs represents your time in seconds and you can use any type of interpolation you wish to interpolate co2obs. --- Yogesh Tiwari [EMAIL PROTECTED] wrote: Hello R Users, I am new to R and I have simple problem for R users. I have CO2 observations defined on time axis(yr,mo,day,hr,min,sec). (DATA ATTACHED HERE) First I want to convert time axis as one axis as 'hour' on regular interval as 1 hour. Say 00 hrs to 24hrs(jan1), 25hrs to 48hrs(jan2) and so on. Then I want to interpolate CO2 at every hour. Kindly anybody can help, Many thanks, Regards, Yogesh yr mo dy hr min sec co2obs 1998 1 9 0 35 24 365.19 1998 1 9 1 17 39 363.54 1998 1 9 1 58 41 364.24 1998 1 9 2 39 42 364.88 1998 1 9 3 20 43 365.06 1998 1 9 4 1 44 364.75 1998 1 9 4 42 45 364.77 1998 1 9 5 23 46 364.87 1998 1 9 6 4 51 364.77 1998 1 9 6 45 52 364.73 1998 1 9 7 26 54 364.76 1998 1 9 8 7 55 363.49 1998 1 23 1 6 16 364.31 1998 1 23 1 48 32 364.38 1998 1 23 2 29 33 364.67 1998 1 23 3 10 35 365.53 1998 1 23 3 51 36 365.16 1998 1 23 4 32 37 364.56 1998 1 23 5 13 40 364.62 1998 1 23 5 54 41 365.05 1998 1 23 6 35 42 365.13 1998 1 23 7 16 44 365.45 1998 1 23 7 57 49 364.77 1998 1 23 8 38 49 364.65 1998 2 3 0 44 21 362.43 1998 2 3 1 26 36 363.96 1998 2 3 2 7 38 364.59 1998 2 3 2 48 40 364.62 1998 2 3 3 29 40 366.22 1998 2 3 4 10 41 365.4 1998 2 3 4 51 41 365.34 1998 2 3 5 32 42 365.31 1998 2 3 6 13 44 364.84 1998 2 3 6 54 46 365.07 1998 2 3 7 35 51 364.84 1998 2 3 8 16 50 364.81 1998 2 19 0 35 14 363.41 1998 2 19 1 17 31 362.93 1998 2 19 1 58 32 363.86 1998 2 19 2 39 33 364.87 1998 2 19 3 20 34 366.05 1998 2 19 4 1 36 364.84 1998 2 19 4 42 36 364.77 1998 2 19 5 23 37 365.01 1998 2 19 6 4 39 365 1998 2 19 6 45 39 365.9 1998 2 19 7 26 40 366.24 1998 2 19 8 7 41 366.55 1998 3 5 0 50 20 363.13 1998 3 5 1 32 37 363.6 1998 3 5 2 13 39 364.26 1998 3 5 2 54 40 364.26 1998 3 5 3 35 41 364.39 1998 3 5 4 16 42 365.24 1998 3 5 4 57 42 365.48 1998 3 5 5 38 43 365.01 1998 3 5 6 19 44 365.43 1998 3 5 7 0 45 365.11 1998 3 5 7 41 46 368.54 1998 3 5 8 22 48 364.96 1998 3 19 0 46 36 363.25 1998 3 19 1 28 51 363.8 1998 3 19 2 9 53 364.21 1998 3 19 2 50 55 364.46 1998 3 19 3 31 58 365.61 1998 3 19 4 12 59 365.57 1998 3 19 4 53 59 365.53 1998 3 19 5 35 0 365.38 1998 3 19 6 16 3 366.23 1998 3 19 6 57 4 364.28 1998 3 19 7 38 6 367.08 1998 3 19 8 19 5 369.19 1998 4 2 1 8 1 363.76 1998 4 2 1 50 17 365.14 1998 4 2 2 31 18 365.26 1998 4 2 3 12 21 364.7 1998 4 2 3 53 24 364.04 1998 4 2 4
Re: [R] Interpolation in time
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) 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 good - !is.na(ta[,'dat']) x.f - approxfun(ta[good,'doy'], ta[good,'dat'], rule=2) ta[!good, 'dat'] - x.f(ta[!good, 'doy']) ta yr doy dat [1,] 2000 16 3.20 [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.10 [9,] 2000 24 5.016667 [10,] 2000 25 4.93 [11,] 2000 26 4.85 [12,] 2000 27 4.77 [13,] 2000 28 4.68 [14,] 2000 29 4.60 On 10/6/05, Anette Nørgaard [EMAIL PROTECTED] 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]] __ 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
Re: [R] Interpolation in time
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@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 __ 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
Re: [R] Interpolation in time
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 [EMAIL PROTECTED] 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 __ 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
Re: [R] Interpolation in time
na.approx(zoo(ta[,-seq(2)], tt)) where tt is as before. On 10/6/05, Anette Nørgaard [EMAIL PROTECTED] 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@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 __ 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
Re: [R] interpolation function
What you intend strikes me as being pretty silly. Do not expect R to work magic for you. Even if there were such a function as you desire in R, the results it would give would be effectively meaningless for data such as you exhibited. cheers, Rolf Turner [EMAIL PROTECTED] __ 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
Re: [R] interpolation function
I do not understand your question. If this was not a sparse matrix, then I would have asked you refer into the missing value literature. Even there, people generally remove any columns/rows that have too many missing values to avoid unreliable results. And since this is a sparse matrix, you are going to have too many missing values on all rows and columns. I could be wrong but if I am, someone will tell me that soon enough. Regards, Adai On Fri, 2005-08-05 at 12:12 +, 吴 昊 wrote: Hi, I have a sparse matrix.I want to fill values into the entries whose value is 0.The new generated values should come from the interpolation of the values have existed.Does R provide such interpolation functions which operate on Matrix, for example ,such a matrix below 0 0 0 0 2.3 0 0 0 0 0 0 3.1 0 0 0 0 1.4 0 0 0 0 0 0 0 0 0 0 1.1 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 3 0 0 0 0 6 0 0 0 0 0 0 9 0 0 0 0 thanks a lot hao wu __ 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 __ 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
Re: [R] interpolation function
I hope you don't take offense at anything said on this list. My philosophy about this is summarized in something I wrote last December that has since been immoratlized in the fortunes package: library(fortunes) fortune(Spencer Graves) Our great-great grandchilren as yet unborn may read some of the stupid questions and/or answers that I and perhaps others give from time to time. I'd rather get flamed for saying something stupid in public on this list than to continue to provide substandard service to the people with whom I work because I perpetrated the same mistake in an environment in which no one questioned so effectively my errors. -- Spencer Graves (in a discussion on whether answers on R-help should be more polite) R-help (December 2004) Best Wishes, spencer graves Rolf Turner wrote: What you intend strikes me as being pretty silly. Do not expect R to work magic for you. Even if there were such a function as you desire in R, the results it would give would be effectively meaningless for data such as you exhibited. cheers, Rolf Turner [EMAIL PROTECTED] __ 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 -- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA [EMAIL PROTECTED] www.pdf.com http://www.pdf.com Tel: 408-938-4420 Fax: 408-280-7915 __ 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
Re: [R] interpolation function
On Fri, 2005-05-08 at 12:12 +, 吴 昊 wrote: Hi, I have a sparse matrix.I want to fill values into the entries whose value is 0.The new generated values should come from the interpolation of the values have existed.Does R provide such interpolation functions which operate on Matrix, for example ,such a matrix below 0 0 0 0 2.3 0 0 0 0 0 0 3.1 0 0 0 0 1.4 0 0 0 0 0 0 0 0 0 0 1.1 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 3 0 0 0 0 6 0 0 0 0 0 0 9 0 0 0 0 thanks a lot hao wu Does this look like an answer? If anyone knows a better way please tell me. d-c(0,2,3,2,0,3,4,0,0,0,0,0) d.mat-matrix(data=d,nrow=4,ncol=3,byrow=TRUE) for(i in 1:length(d.mat[1,])){ + d.mat[,i][d.mat[,i]==0]-mean(d.mat[,i][d.mat[,i]0]) + } Thanks Tom __ 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
Re: [R] interpolation function
I don't know if what Hao wanted to do is to hang himself, but if so, perhaps here is one possible rope: idx - which(m 0, arr.ind=TRUE) m.nz - m[idx] library(akima) (m.int - interp.new(idx[,1], idx[,2], m.nz, xo=1:9, yo=1:9, extrap=TRUE)) $x [1] 1 2 3 4 5 6 7 8 9 $y [1] 1 2 3 4 5 6 7 8 9 $z [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 57.1356740 55.18522 38.413800 18.086957 2.30 0.737908 22. 544616 [2,] 18.8241001 19.35348 3.10 -20.355403 -41.610566 -51.308028 -40. 090327 [3,]3.2234619 8.63630 -5.312670 -29.221287 -53.732091 -69.487619 -67. 130408 [4,]1.100 13.75521 4.290890 -17.935507 -43.566514 -63.244671 -67. 612515 [5,]3.2013295 25.25298 22.408739 4.00 -20.615776 -42.081126 -51. 038588 [6,]0.2005624 33.62766 39.538938 27.083292 5.618185 -15.498922 -26. 910566 [7,] -17.2478151 29.37731 46.179546 41.812431 25.633428 7.00 -4. 730390 [8,] -58.4893169 3.0 32.828625 38.685475 29.928013 15.913700 6. 00 [9,] -130.5161684 -53.43667 -9.550716 8.258617 9.00 1.817369 -3. 604296 [,8] [,9] [1,] 76.7287988 170.407375 [2,] 1.400 80.913745 [3,] -37.3029968 28.876027 [4,] -47.3125835 6.953079 [5,] -38.1307000 6.00 [6,] -19.2592860 16.750369 [7,] -0.2080329 29.451691 [8,] 9.3350825 34.229953 [9,] 0.8303142 21.211142 Note how wild the interpolated/extrapolated values can be... Andy From: Adaikalavan Ramasamy I do not understand your question. If this was not a sparse matrix, then I would have asked you refer into the missing value literature. Even there, people generally remove any columns/rows that have too many missing values to avoid unreliable results. And since this is a sparse matrix, you are going to have too many missing values on all rows and columns. I could be wrong but if I am, someone will tell me that soon enough. Regards, Adai On Fri, 2005-08-05 at 12:12 +, 吴 昊 wrote: Hi, I have a sparse matrix.I want to fill values into the entries whose value is 0.The new generated values should come from the interpolation of the values have existed.Does R provide such interpolation functions which operate on Matrix, for example ,such a matrix below 0 0 0 0 2.3 0 0 0 0 0 0 3.1 0 0 0 0 1.4 0 0 0 0 0 0 0 0 0 0 1.1 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 3 0 0 0 0 6 0 0 0 0 0 0 9 0 0 0 0 thanks a lot hao wu __ 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 __ 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 __ 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
Re: [R] interpolation function in R
?approx ?splinefun __ 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
Re: [R] interpolation function in R
吴 昊 wrote: Hi does R provide some interpolation fucntions? thank __ 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 PLEASE do read the posting guide! help.search(interpolation) returns interpSpline(splines) Create an Interpolation Spline periodicSpline(splines) Create a Periodic Interpolation Spline NLSstClosestX(stats)Inverse Interpolation approx(stats) Interpolation Functions spline(stats) Interpolating Splines for example. There may be others in packages I don't have installed as well. HTH, --sundar __ 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
Re: [R] Interpolation of azimuth values
On Thu, 16 Oct 2003, temiz wrote: I will make an interpolation of data which represents azimuth direction ( angle from north in clockwise direction) values. But there is a problem. Say, for instance, while 1 and 359 indicate somewhat same direction, interpolation puts values in the range from 1 to 359. What can I do to solve the problem ? Anything you offer ? The usual solution is to extend the data periodically, that is to put copies shifted by one rotation on each side. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] interpolation methods
Dear, Have a look at: *akima http://cran.r-project.org/src/contrib/akima_0.3-4.tar.gz: Interpolation of irregularly spaced data* Linear or cubic spline interpolation for irregular gridded data Kris -- http://perswww.kuleuven.ac.be/~u0027178/VCard/mycard.php?name=krisn http://gloveg.kuleuven.ac.be/ Minds are like parachutes, they only work when open __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Interpolation
Remigijus Lapinskas wrote: Many thanks to all who replied to my e-mail. My problem was that I had not known about the approx function. By the way, if I have x - c(1990,1994,1995,1997), is there an automated way to fill in the gaps, i.e., to get c(1991,1992,1993,1996)? Try this: R x - c(1990, 1994, 1995, 1997) R all.x - seq(min(x), max(x)) R missing.x - all.x[!all.x %in% x] R missing.x [1] 1991 1992 1993 1996 R Regards, Sundar __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help
RE: [R] Interpolation
Here is another way of doing it: x - c(1990, 1994, 1995, 1997) all.x - seq(min(x), max(x)) complementary.x - setdiff(all.x, x) -Original Message- From: Sundar Dorai-Raj [mailto:[EMAIL PROTECTED]] Sent: Wednesday, February 12, 2003 9:39 PM To: Remigijus Lapinskas Cc: [EMAIL PROTECTED] Subject: Re: [R] Interpolation Remigijus Lapinskas wrote: Many thanks to all who replied to my e-mail. My problem was that I had not known about the approx function. By the way, if I have x - c(1990,1994,1995,1997), is there an automated way to fill in the gaps, i.e., to get c(1991,1992,1993,1996)? Try this: R x - c(1990, 1994, 1995, 1997) R all.x - seq(min(x), max(x)) R missing.x - all.x[!all.x %in% x] R missing.x [1] 1991 1992 1993 1996 R Regards, Sundar __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] interpolation and 3D plots
Dear Jonathan, The all.effects function in the car package will identify the high-order terms in a linear or generalized-linear model and absorb terms marginal to them (e.g., for a two-way interaction, the main effects marginal to the interaction and the constant). The plot method for the object produced uses trellis graphics to plot terms, but for a two-way interaction, the information in the object could easily be plotted as an image (or other 3D) plot. I hope that this helps, John At 10:02 AM 2/3/2003 +, Davies, Jonathan wrote: I've been told that you may be able to help. I have a complex linear model with multiple two-way interaction terms. Is there a way to view the interaction terms in a 3d plot equivalent to the S functions: plan-interp(x,y,z) image(plan) where x and y are my explanatory variables and z my response variable. - John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: [EMAIL PROTECTED] phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox - __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help