[R] dataframe: how to select an element from a row

Florent D. flodel at gmail.com
Fri Jan 20 01:28:25 CET 2012


Another possibility:

df$Date[match(1800, df$myvalue)]

match() stops at the first value encountered so it may be a bit faster
than a full subset(), depending on your table size.
Another difference: this approach would return NA if there was no
match, subset(...)[1, ] would trigger an error. Depends what your
needs are.

On Thu, Jan 19, 2012 at 7:03 PM, Jorge I Velez <jorgeivanvelez at gmail.com> wrote:
> Hi ikuzar,
>
> Try
>
> subset(df, myvalue == 1800, select = Date)[1, ]
>
> See ?subset for more information.
>
> HTH,
> Jorge.-
>
>
> On Thu, Jan 19, 2012 at 6:42 PM, ikuzar <> wrote:
>
>> Hi,
>> I 'd like to select the Date where myvalue =1800 appears the* first time*.
>>
>> For instance:
>> df =data.frame(date, myvalue, ...)
>>
>> ...
>>
>> Date                            myvalue
>> 2012-01-05                2500
>> 2012-01-06               2450
>> *2012-01-07               1800*
>> 2012-01-08                2200
>> 2012-01-09                1800
>>
>> I'd like to retrieve the third line.
>>
>> I do not find a clean way.
>>
>> Thanks for your help
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/dataframe-how-to-select-an-element-from-a-row-tp4311881p4311881.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.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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