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

Junqian Gordon Xu xjqian at gmail.com
Sun Mar 14 03:37:47 CET 2010


Hi,

In the example given below, I want to code the "Lesion" in the 7th row
as 2 because the 2nd row has a Lesion value of 2.

  ID             Date      Segment Slice Tract     Lesion
7 CSPP005 12/4/2007       1     1      Whole        2

Otherwise (if there is no lesion value of 2 in row 1-6),  I want to
code the "Lesion" in the 7th row as 0.

  ID             Date      Segment Slice Tract     Lesion
7 CSPP005 12/4/2007       1     1      Whole        0

The whole data frame is just repeated units of the 7 different Tracts
for different ID->Date->Segment->Slice

Hope it helps
Gordon

On Sat, Mar 13, 2010 at 8:04 PM, jim holtman <jholtman at gmail.com> wrote:
> It is not entirely clear what type of transformation you are trying to do.
> Can you provide some sample data and then show what you would expect the
> output to look like; make sure the data covers all the cases you want to
> transform.
>
> On Sat, Mar 13, 2010 at 8: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
>>
>> ______________________________________________
>> 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