So basically I want to do this - 4 %in% 1:10 should return true Would there be another way of doing this without having to do the 1:10 part? I am using a very large data set and trying to do 459124 %in% 103000:983000 multiple times for many values, and it is taking quite a long time Also, I would like to vary the x:y part, so I can't even make an object that is c(x:y) and do a which(#=c(x:y)) because I would need to change x and y each time. -- View this message in context: http://r.789695.n4.nabble.com/find-value-between-two-other-numbers-tp2323513p2323513.html Sent from the R help mailing list archive at Nabble.com.
Hi, Here is one way:> is.there <- function(mynumber, a, b) mynumber %in% a:b > is.there(4, 1, 10)[1] TRUE> is.there(459124, 103000, 983000)[1] TRUE> is.there(4, 103000, 983000)[1] FALSE HTH, Jorge On Thu, Aug 12, 2010 at 7:10 PM, fishkbob <> wrote:> > So basically I want to do this - > > 4 %in% 1:10 > should return true > > Would there be another way of doing this without having to do the 1:10 > part? > > I am using a very large data set and trying to do > > 459124 %in% 103000:983000 > > multiple times for many values, and it is taking quite a long time > > Also, I would like to vary the x:y part, so I can't even make an object > that > is c(x:y) and do a which(#=c(x:y)) because I would need to change x and y > each time. > -- > View this message in context: > http://r.789695.n4.nabble.com/find-value-between-two-other-numbers-tp2323513p2323513.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi, I see no need to construct the vector, try this instead, belong = function(x=4, y=c(1,10)) x <= y[2] && x >= y[1] see also ?findInterval HTH, baptiste On 13 August 2010 01:10, fishkbob <fishkbob at gmail.com> wrote:> > So basically I want to do this - > > 4 %in% 1:10 > should return true > > Would there be another way of doing this without having to do the 1:10 part? > > I am using a very large data set and trying to do > > 459124 %in% 103000:983000 > > multiple times for many values, and it is taking quite a long time > > Also, I would like to vary the x:y part, so I can't even make an object that > is c(x:y) and do a which(#=c(x:y)) because I would need to change x and y > each time. > -- > View this message in context: http://r.789695.n4.nabble.com/find-value-between-two-other-numbers-tp2323513p2323513.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Awesome, that works great, and it cuts my runtime down by a lot. thanks baptiste I totally forgot that I could just check to see if the number was inbetween via less than and greater than rather than having to construct the vector. findInterval also helps with another problem I was having somewhere else... -- View this message in context: http://r.789695.n4.nabble.com/find-value-between-two-other-numbers-tp2323513p2324409.html Sent from the R help mailing list archive at Nabble.com.