[R] code rows depending on the value of other rows in multilevel dataframe
Junqian Gordon Xu
xjqian at gmail.com
Sun Mar 14 02:54:34 CET 2010
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