I think that's in general a trickier question:

If you are absolutely certain your id's are in blocks and that there are at
least k of them, it would perhaps work to do a manual adjustment of the
indices:something like

# UNTESTED
with(dat, dat[which(!duplicated(id)) + k - 1, ]) # where k is the
whatever-eth element you want to look at

If you aren't guaranteed a block structure, you might have to use some sort
of "rinse and repeat" code (i.e., get the first occurrences, throw them out,
get the new first occurrences = the old 2nd occurrences, etc). If you aren't
certain that you have at least k occurences, you'll want to handle that as
well. There might be a smarter way, but I'm not seeing one off hand

One nice solution if you just want the last is to use the dupicated(,
fromLast = TRUE) optional argument.

Michael Weylandt


On Sun, Sep 25, 2011 at 6:08 PM, AC Del Re <acde...@gmail.com> wrote:

> perfect. Thank you, Michael. Out of curiosity, how would the code differ if
> you were interested in selecting the second row (or third, etc) per id?
>
> Thanks again for your help.
>
> AC
>
>
> On Sun, Sep 25, 2011 at 1:48 PM, R. Michael Weylandt <
> michael.weyla...@gmail.com> wrote:
>
>> dat <- data.frame(id = c(1,1,1,2,2), value = c(5,6,7,4,5), value2 =
>> c(1,4,3,3,4))
>> with(dat, dat[!duplicated(id),])
>>
>> Michael
>>
>>
>> On Sun, Sep 25, 2011 at 4:43 PM, AC Del Re <acde...@gmail.com> wrote:
>>
>>> Great but how can I then retain the other variable (or variables if >1)
>>> value associated with those retained values?
>>> thank you.
>>>
>>> On Sun, Sep 25, 2011 at 1:27 PM, R. Michael Weylandt <
>>> michael.weyla...@gmail.com> wrote:
>>>
>>>> How about something like
>>>>
>>>> with(dat, value[!duplicated(id)])
>>>>
>>>> ?
>>>>
>>>> Michael Weylandt
>>>> On Sun, Sep 25, 2011 at 4:22 PM, AC Del Re <de...@wisc.edu> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I am trying to select the first row of a variable with data in
>>>>> long-format,
>>>>> e.g.,
>>>>>
>>>>> # sample data
>>>>> id <- c(1,1,1,2,2)
>>>>> value <- c(5,6,7,4,5)
>>>>> dat <- data.frame(id, value)
>>>>> dat
>>>>>
>>>>> How can I select/subset the first 'value'  for each unique 'id'?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> AC
>>>>>
>>>>>        [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help@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@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.

Reply via email to