Skip to content

data frame question

2 messages · Sarah Goslee, Dénes Tóth

#
If it's not homework, then I'm happy to provide more help:


a <-seq(0,10,by=1)
b <-c(10:20)
d <-data.frame(a=a,b=b)
f <-16

subset(d, b < f & b == max(b[b < f]))$a

# I'd turn it into a function
getVal <- function(d, f) {
    subset(d, b < f & b == max(b[b < f]))$a
}


Sarah
On Mon, Dec 9, 2013 at 3:50 PM, Andras Farkas <motyocska at yahoo.com> wrote:

  
    
#
Hi Andras,

here is an other solution which also works if b contains missing values:

a <-seq(0,10,by=1)
b <-c(NA, 11:20)
f <-16
#
a[which.max(b[b<f])]
#

However, your question seems a bit artificial. Maybe you converted your
original question to a suboptimal problem.

HTH,
  Denes