Hello, I have a question regarding subsetting of vectors. Here's an example of what I'm trying to do: vect.1 <- c(76,195, 290, 380) vect.2 <- c(63, 95, 133, 170, 215, 253, 285, 299, 325, 375) I would like to subset vect.2 so that it has the same length as vect.1, and its numbers are the first corresponging higher value compared to vect.1. The output should be: final.output = (95, 215, 299, NA) What is the fastest/most eficient way to accompllish this in R? Thanks for the help in advance, Mahesh Krishnan
> vect.1[1] 76 195 290 380> vect.2[1] 63 95 133 170 215 253 285 299 325 375> unlist(lapply(vect.1, function(x)vect.2[which(vect.2 > x)[1]]))[1] 95 215 299 NA>On 9/3/06, Mahesh Krishnan <mskrishnan71 at yahoo.com> wrote:> Hello, > > I have a question regarding subsetting of vectors. Here's an example of > what I'm trying to do: > > vect.1 <- c(76,195, 290, 380) > > vect.2 <- c(63, 95, 133, 170, 215, 253, 285, 299, 325, 375) > > I would like to subset vect.2 so that it has the same length as vect.1, > and its numbers are the first corresponging higher value compared to > vect.1. > > The output should be: > > final.output = (95, 215, 299, NA) > > What is the fastest/most eficient way to accompllish this in R? > > Thanks for the help in advance, > > Mahesh Krishnan > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Mahesh Krishnan wrote:> Hello, > > I have a question regarding subsetting of vectors. Here's an example of > what I'm trying to do: > > vect.1 <- c(76,195, 290, 380) > > vect.2 <- c(63, 95, 133, 170, 215, 253, 285, 299, 325, 375) > > I would like to subset vect.2 so that it has the same length as vect.1, > and its numbers are the first corresponging higher value compared to > vect.1. > > The output should be: > > final.output = (95, 215, 299, NA) > > What is the fastest/most eficient way to accompllish this in R? > > Thanks for the help in advance, > > Mahesh Krishnan > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >> vect.1 <- c(76,195, 290, 380) > vect.2 <- c(63, 95, 133, 170, 215, 253, 285, 299, 325, 375) > vect.2[ findInterval(vect.1,vect.2) + 1 ] [1] 95 215 299 NA J. R. M. Hosking