I have a long list of numbers [3.4,5.4,3.67,....], and I basically want to find the index of the number closest to this number that I have, let's say 5.43. How would I do this without writing a for loop (I have to do this many times for several lists)? Is there a "lookup" function in R? Thanks! -- View this message in context: http://www.nabble.com/Find-the-closest-value-in-a-list-or-matrix-tp18363290p18363290.html Sent from the R help mailing list archive at Nabble.com.
Try this: which.min(abs(x - 5.43)) where x is your vector of numbers. On Wed, Jul 9, 2008 at 12:28 PM, R_Learner <sschiang8 at gmail.com> wrote:> > I have a long list of numbers [3.4,5.4,3.67,....], and I basically want to > find the index of the number closest to this number that I have, let's say > 5.43. How would I do this without writing a for loop (I have to do this many > times for several lists)? Is there a "lookup" function in R? > > Thanks! > -- > View this message in context: http://www.nabble.com/Find-the-closest-value-in-a-list-or-matrix-tp18363290p18363290.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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
x=c(1:100) your.number=5.43 which(abs(x-your.number)==min(abs(x-your.number))) Best, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von R_Learner Gesendet: Wednesday, July 09, 2008 11:28 AM An: r-help at r-project.org Betreff: [R] Find the closest value in a list or matrix I have a long list of numbers [3.4,5.4,3.67,....], and I basically want to find the index of the number closest to this number that I have, let's say 5.43. How would I do this without writing a for loop (I have to do this many times for several lists)? Is there a "lookup" function in R? Thanks! -- View this message in context: http://www.nabble.com/Find-the-closest-value-in-a-list-or-matrix-tp18363290p 18363290.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.
Hi there, Is this what you want? your.number=5.43 # For a vector x=c(1,2,4,3,2,5,6,7,5.42,6) which.min(abs(x-your.number)) [1] 9 # For a matrix set.seed(123) X=matrix(rpois(100,4.5),ncol=10) apply(X,2,function(x) which.min(abs(x-your.number))) [1] 9 3 2 3 2 5 1 2 2 2 HTH, Jorge On Wed, Jul 9, 2008 at 11:28 AM, R_Learner <sschiang8@gmail.com> wrote:> > I have a long list of numbers [3.4,5.4,3.67,....], and I basically want to > find the index of the number closest to this number that I have, let's say > 5.43. How would I do this without writing a for loop (I have to do this > many > times for several lists)? Is there a "lookup" function in R? > > Thanks! > -- > View this message in context: > http://www.nabble.com/Find-the-closest-value-in-a-list-or-matrix-tp18363290p18363290.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]]
Thanks! I'll use the which.min function. I've been learning R from the pdfs on the CRAN website, but I find the texts to be generally introductory, not comprehensive, and focused on one niche purpose. Can anyone suggest a good self-teach guide? Thanks again! R_Learner wrote:> > I have a long list of numbers [3.4,5.4,3.67,....], and I basically want to > find the index of the number closest to this number that I have, let's say > 5.43. How would I do this without writing a for loop (I have to do this > many times for several lists)? Is there a "lookup" function in R? > > Thanks! >-- View this message in context: http://www.nabble.com/Find-the-closest-value-in-a-list-or-matrix-tp18363290p18375656.html Sent from the R help mailing list archive at Nabble.com.