Hi I''m looking for an Excel Vlookup type function in R. Example: list <- c(1,2,3,4,5,6,7) base <- c(2.2,3,5.2) What I want is, for each number in base, the highest value in list, which is equal to or less than the number in base So the results would be: base ? ? ? ? list 2.2 ?------> 2 3 ? ?------> 3 5.2 ?------> ?5 Thanks for your help!
On Tue, Feb 28, 2012 at 09:02:04PM +0530, Priyan Fernando wrote:> Hi > > I''m looking for an Excel Vlookup type function in R. > > Example: > list <- c(1,2,3,4,5,6,7) > base <- c(2.2,3,5.2) > > What I want is, for each number in base, the highest value in list, > which is equal to or less than the number in base > > So the results would be: > > base ? ? ? ? list > 2.2 ?------> 2 > 3 ? ?------> 3 > 5.2 ?------> ?5Hi. Try the following. unlist(lapply(base, FUN = function(x) max(list[list <= x]))) Hope this helps. Petr Savicky.
On 28-02-2012, at 16:32, Priyan Fernando wrote:> Hi > > I''m looking for an Excel Vlookup type function in R. > > Example: > list <- c(1,2,3,4,5,6,7) > base <- c(2.2,3,5.2) > > What I want is, for each number in base, the highest value in list, > which is equal to or less than the number in base > > So the results would be: > > base list > 2.2 ------> 2 > 3 ------> 3 > 5.2 ------> 5Don't use "list" as an object name. It is a standard R function. vlist <- c(1,2,3,4,5,6,7) base <- c(2.2,3,5.2) findInterval(base, vlist) Berend
findInterval returns the index (into list) of what you want. Use [ to get the numbers at the bottom of the found intervals: > list <- c(1,2,3,4,5,6,7) > base <- c(2.2,3,5.2) > findInterval(base, list) [1] 2 3 5 > findInterval(base+100, list+100) [1] 2 3 5 > (list+100)[.Last.value] [1] 102 103 105 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Priyan Fernando > Sent: Tuesday, February 28, 2012 7:32 AM > To: r-help at r-project.org > Subject: [R] vlookup type function > > Hi > > I''m looking for an Excel Vlookup type function in R. > > Example: > list <- c(1,2,3,4,5,6,7) > base <- c(2.2,3,5.2) > > What I want is, for each number in base, the highest value in list, > which is equal to or less than the number in base > > So the results would be: > > base ? ? ? ? list > 2.2 ?------> 2 > 3 ? ?------> 3 > 5.2 ?------> ?5 > > Thanks for your help! > > ______________________________________________ > 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.
On Tue, Feb 28, 2012 at 09:02:04PM +0530, Priyan Fernando wrote:> Hi > > I''m looking for an Excel Vlookup type function in R. > > Example: > list <- c(1,2,3,4,5,6,7) > base <- c(2.2,3,5.2) > > What I want is, for each number in base, the highest value in list, > which is equal to or less than the number in base > > So the results would be: > > base ? ? ? ? list > 2.2 ?------> 2 > 3 ? ?------> 3 > 5.2 ?------> ?5Hi. If "base" may contain numbers smaller than all numbers in "list", the the following modification of the previous suggestion does not generate a warning. list <- c(1,2,3,4,5,6,7) base <- c(0, 2.2, 3, 5.2, 8) unlist(lapply(base, FUN=function(x) max(list[list <= x], -Inf))) [1] -Inf 2 3 5 7 Hope this helps. Petr Savicky.