[R] Fwd: select a subset

jim holtman jholtman at gmail.com
Tue Nov 25 14:26:43 CET 2008


Not tested since you did not provide any data:

results <- lapply(split(df, df$id), function(.data){
    .which <- which(.data$censor == 1)[1]  # get first one
    if (length(.which) > 0) return(.data[.which,])
    else return(.data[nrow(.data),])
})

On Tue, Nov 25, 2008 at 2:45 AM, gallon li <gallon.li at gmail.com> wrote:
> ---------- Forwarded message ----------
> From: gallon li <gallon.li at gmail.com>
> Date: Tue, Nov 25, 2008 at 1:58 PM
> Subject: Re: [R] select a subset
> To: Stefan Grosse <singularitaet at gmx.net>
>
>
> I am sorry but my question is not solvable by using subset alone.
>
> You see, the selection criterion is different for different id. This may not
> be easily specified in the subset function.
>
> Yes, I did think about this function before but couldn't find ways to use
> it.
>
>  On Mon, Nov 24, 2008 at 8:42 PM, Stefan Grosse <singularitaet at gmx.net>wrote:
>
>> gallon li schrieb:
>> > for id 1, i want to select the last row since all censor indicator is 0;
>> for
>> > id 2, i want to select the row where censor ==1; for id 3, i also want to
>> > select the row where censor==1. So if there is a 1 for censor, then I
>> want
>> > to select such a row, otherwise I want to select the last obs. for this
>> id.
>> > I am wondering if there is a quick way to solve this?
>> >
>> ?subset
>>
>> and please do read an introductionary text....
>>
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list