[R] data frame question

Toth, Denes toth.denes at ttk.mta.hu
Mon Dec 9 22:15:11 CET 2013


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


> 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:
>> Sarah,
>>
>> thank you, not homework though, I guess it just looks like it.... I will
>> look into subset()
>>
>> Andras
>>
>>
>> On Monday, December 9, 2013 3:45 PM, Sarah Goslee
>> <sarah.goslee at gmail.com>
>> wrote:
>> Thank you for providing a reproducible example. I tweaked it a little
>> bit to make it actually a data frame problem.
>>
>> There are lots of ways to do this; here's one approach.
>>
>> On second thought, this looks a lot like homework, so perhaps instead
>> I'll just suggest using subset() with more than one condition.
>>
>> Sarah
>>
>> On Mon, Dec 9, 2013 at 3:27 PM, Andras Farkas <motyocska at yahoo.com>
>> wrote:
>>> Dear All
>>>
>>> please help with the following:
>>>
>>> I have:
>>>
>>> a <-seq(0,10,by=1)
>>> b <-c(10:20)
>>> d <-cbind(a,b)
>>> f <-16
>>>
>>> I would like to select the value in column a based on a value in column
>>> b,
>>> where the value in column b is the 1st value that is smaller then f.
>>> Thus I
>>> should end up with the number 5 because the 1st value that is below 16
>>> would
>>> be 15, and in the same row column a has the number 5....
>>>
>>> appreciate your insights,
>>>
>>> andras
>>
>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list