Hi Ashta, This does not seem too difficult: DF$flag<-"n" for(thisname in unique(DF$Name)) { if(any(DF$year[DF$Name == thisname] %in% c(2014,2015) & DF$tag[DF$Name == thisname])) DF$flag[DF$Name == thisname]<-"y" }
Jim On Sun, Feb 28, 2016 at 1:23 PM, Ashta <sewa...@gmail.com> wrote: > Hi all, > > I have a data set represented by the following sample. > > I want flag records of an individual as "N", if if the tag column of > an individual is equal to zero for the last two years. So in the > following example, Alex1 records are flagged as "y", On the other > hand Carla's records are flagged as "N" because all values of tag for > Carla are zero. Another typical example is that Jon, although the tag > values of Jon are greater than 0 it is flagged as "N", because his > record are more than two years old. > > DF <- read.table(textConnection(" Name year tag > Alex1 2011 0 > Alex1 2012 1 > Alex1 2013 0 > Alex1 2014 1 > > Carla 2013 0 > Carla 2014 0 > Carla 2015 0 > Carla 2012 0 > > Tom 2014 1 > Tom 2015 1 > > Jon 2010 1 > Jon 2011 1 "),header = TRUE) > > I want create another variable " Flag with value Y or N" if an > individual has a value greater than 0 in the tag column for the last > two years then the flag value will be y otherwise it n. > > > the outcome will be > name year tag Flag > Alex1 2011 0 y > Alex1 2012 1 y > Alex1 2013 0 y > Alex1 2014 1 y > > Carla 2013 0 n > Carla 2014 0 n > Carla 2015 0 n > Carla 2012 0 n > > Tom 2014 1 y > Tom 2015 1 y > > Jon 2010 1 n > Jon 2011 1 n > > Thank you in advance > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.