Junqian Gordon Xu
2010-Mar-14 01:54 UTC
[R] code rows depending on the value of other rows in multilevel dataframe
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
jim holtman
2010-Mar-14 02:04 UTC
[R] code rows depending on the value of other rows in multilevel dataframe
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@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@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<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? [[alternative HTML version deleted]]
Junqian Gordon Xu
2010-Mar-14 03:08 UTC
[R] code rows depending on the value of other rows in multilevel dataframe
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 >