[R] vlookup type function

Petr Savicky savicky at cs.cas.cz
Tue Feb 28 17:23:04 CET 2012


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  ------>  5

Hi.

Try the following.

  unlist(lapply(base, FUN = function(x) max(list[list <= x])))

Hope this helps.

Petr Savicky.



More information about the R-help mailing list