Try this:

> x <- read.table(textConnection(
+ "   ID             Date      Segment Slice Tract     Lesion
+ CSPP005 12/4/2007       1     1      LCST         0
+ CSPP005 12/4/2007       1     1      LPC           2
+ CSPP005 12/4/2007       1     1      RPC          3
+ CSPP005 12/4/2007       1     1      RCST        1
+ CSPP005 12/4/2007       1     1      LGM          0
+ CSPP005 12/4/2007       1     1      RGM          0
+ CSPP005 12/4/2007       1     1      Whole        NA
+ CSPP005 12/4/2007       1     2      LCST         0
+ CSPP005 12/4/2007       1     2      LPC           -2
+ CSPP005 12/4/2007       1     2      RPC          3
+ CSPP005 12/4/2007       1     2      RCST        1
+ CSPP005 12/4/2007       1     2      LGM          0
+ CSPP005 12/4/2007       1     2      RGM          0
+ CSPP005 12/4/2007       1     2      Whole        NA
+ CSPP005 12/4/2007       1     3      LCST         0
+ CSPP005 12/4/2007       1     3      LPC           2
+ CSPP005 12/4/2007       1     3      RPC          3
+ CSPP005 12/4/2007       1     3      RCST        1
+ CSPP005 12/4/2007       1     3      LGM          0
+ CSPP005 12/4/2007       1     3      RGM          0
+ CSPP005 12/4/2007       1     3      Whole        NA"), header=TRUE, as.is
=TRUE)
> closeAllConnections()
> # split by ID, Date, Segment & Slice
> x.s <- split(x, list(x$ID, x$Date, x$Segment, x$Slice))
> # now process each group for the conditions
> result <- lapply(x.s, function(.grp){
+     # see if any Lesion has the value 2
+     if (any(na.omit(.grp$Lesion) == 2)) ans <- 2
+     else ans <- 1
+     # assign the ans to "Whole" (I assume each group has one
+     .grp$Lesion[which(.grp$Tract == "Whole")] <- ans
+     .grp  # return changed .grp
+ })
> # put the result back into a single dataframe
> do.call(rbind, result)
                              ID      Date Segment Slice Tract Lesion
CSPP005.12/4/2007.1.1.1  CSPP005 12/4/2007       1     1  LCST      0
CSPP005.12/4/2007.1.1.2  CSPP005 12/4/2007       1     1   LPC      2
CSPP005.12/4/2007.1.1.3  CSPP005 12/4/2007       1     1   RPC      3
CSPP005.12/4/2007.1.1.4  CSPP005 12/4/2007       1     1  RCST      1
CSPP005.12/4/2007.1.1.5  CSPP005 12/4/2007       1     1   LGM      0
CSPP005.12/4/2007.1.1.6  CSPP005 12/4/2007       1     1   RGM      0
CSPP005.12/4/2007.1.1.7  CSPP005 12/4/2007       1     1 Whole      2
CSPP005.12/4/2007.1.2.8  CSPP005 12/4/2007       1     2  LCST      0
CSPP005.12/4/2007.1.2.9  CSPP005 12/4/2007       1     2   LPC     -2
CSPP005.12/4/2007.1.2.10 CSPP005 12/4/2007       1     2   RPC      3
CSPP005.12/4/2007.1.2.11 CSPP005 12/4/2007       1     2  RCST      1
CSPP005.12/4/2007.1.2.12 CSPP005 12/4/2007       1     2   LGM      0
CSPP005.12/4/2007.1.2.13 CSPP005 12/4/2007       1     2   RGM      0
CSPP005.12/4/2007.1.2.14 CSPP005 12/4/2007       1     2 Whole      1
CSPP005.12/4/2007.1.3.15 CSPP005 12/4/2007       1     3  LCST      0
CSPP005.12/4/2007.1.3.16 CSPP005 12/4/2007       1     3   LPC      2
CSPP005.12/4/2007.1.3.17 CSPP005 12/4/2007       1     3   RPC      3
CSPP005.12/4/2007.1.3.18 CSPP005 12/4/2007       1     3  RCST      1
CSPP005.12/4/2007.1.3.19 CSPP005 12/4/2007       1     3   LGM      0
CSPP005.12/4/2007.1.3.20 CSPP005 12/4/2007       1     3   RGM      0
CSPP005.12/4/2007.1.3.21 CSPP005 12/4/2007       1     3 Whole      2
>
>
>
>


On Sat, Mar 13, 2010 at 9:37 PM, Junqian Gordon Xu <xjqian@gmail.com> wrote:

> 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@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@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?
> >
>



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

What is the problem that you are trying to solve?

	[[alternative HTML version deleted]]

