Dear all, I have two vectors and connect the points by line segments: x <- c(1990,1994,1995,1997) y <- c(30,40,80,20) plot(x,y,type="l") I want to get the values of y's on these lines at missing x's, i.e., at 1991,1992,1993,and 1996. Is there a simple way to do this? Best regards, Remigijus mailto:remigijus.lapinskas at maf.vu.lt
> I have two vectors and connect the points by line segments: > > x <- c(1990,1994,1995,1997) > y <- c(30,40,80,20) > plot(x,y,type="l") > > I want to get the values of y's on these lines at missing x's, > i.e., at 1991,1992,1993,and 1996. Is there a simple way to do this? >Easy: approx(x, y, c(1991:1993, 1996))$y while approx(x, y, 1990:1997)$y will give you all the y values. Hope this helps, Ray Brownrigg <ray at mcs.vuw.ac.nz> http://www.mcs.vuw.ac.nz/~ray
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)? Cheers, Remigijus Wednesday, February 12, 2003, 12:09:33 PM, you wrote: PDB> Remigijus Lapinskas <remigijus.lapinskas at maf.vu.lt> writes:>> Dear all, >> >> I have two vectors and connect the points by line segments: >> >> x <- c(1990,1994,1995,1997) >> y <- c(30,40,80,20) >> plot(x,y,type="l") >> >> I want to get the values of y's on these lines at missing x's, >> i.e., at 1991,1992,1993,and 1996. Is there a simple way to do this?PDB> approx(x,y,c(1991,1992,1993,1996)) PDB> plot(x,y,type="l") PDB> points(approx(x,y,c(1991,1992,1993,1996)),pch="*",col="red",cex=5) PDB> -- PDB> O__ ---- Peter Dalgaard Blegdamsvej 3 PDB> c/ /'_ --- Dept. of Biostatistics 2200 Cph. N PDB> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 PDB> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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
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:sundar.dorai-raj at pdf.com] Sent: Wednesday, February 12, 2003 9:39 PM To: Remigijus Lapinskas Cc: r-help at stat.math.ethz.ch 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 ______________________________________________ R-help at stat.math.ethz.ch mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help