[R] code rows depending on the value of other rows in multilevel dataframe

Junqian Gordon Xu xjqian at gmail.com
Sun Mar 14 04:08:32 CET 2010


merge(Whole,Where.Lesion2)

gives the intersections (ID/Date/Segment/Slice, i.e. the first 4
columns), except that the merge function drops the original row.names
in the dataframe "Whole".

Now, the question evolves to finding row.names of one data frame that
matches any of the rows in another dataframe.

Gordon

On Sat, Mar 13, 2010 at 7:54 PM, Junqian Gordon Xu <xjqian at gmail.com> wrote:
> I have a multilevel dataframe (df):
>
>   ID             Date      Segment Slice Tract     Lesion
> 1 CSPP005 12/4/2007       1     1      LCST         0
> 2 CSPP005 12/4/2007       1     1      LPC           2
> 3 CSPP005 12/4/2007       1     1      RPC          3
> 4 CSPP005 12/4/2007       1     1      RCST        1
> 5 CSPP005 12/4/2007       1     1      LGM          0
> 6 CSPP005 12/4/2007       1     1      RGM          0
> 7 CSPP005 12/4/2007       1     1      Whole        NA
>
> The five levels are:
>
> ID->Date->Segment->Slice->Tract
>
> The observation variable is: Lesion
>
> What I want to do is (in pseudo code)
>
> if  (any of the Tract (for a given ID, Date, Segment, and Slice),
> which( Lesion == 2) )
>    df [ for that particular (ID, Date,Segment, and slice) ,
> which(Tract == Whole) ] $Lesion <- 2
> else
>    df [ for that particular (ID, Date,Segment, and slice) ,
> which(Tract == Whole) ] $Lesion <- 0
>
>
>
> I started with (don't know if this is the right path),
>
> Lesion2<-df[which(df$Lesion == 2),]
> Where.Lesion2<-unique(Lesion2[,1:4])
> Whole<-subset(df, Tract == "Whole")
>
> But stuck at how to match the ID/Date/Segment/Slice from Where.Lesion2 to Whole.
>
> Regards
> Gordon
>



More information about the R-help mailing list