Try this. dfsub <- df[df$deps %in% c("CC", "DD", "FF"), ] names(dfsub) <- c("Subject", "newdate", "origdep") final <- merge(df, dfsub)
Jean On Wed, Aug 10, 2016 at 10:58 AM, Farnoosh Sheikhi via R-help < r-help@r-project.org> wrote: > blockquote, div.yahoo_quoted { margin-left: 0 !important; border-left:1px > #715FFA solid !important; padding-left:1ex !important; > background-color:white !important; } > > Hi > I have a data set like below and wanted to create a new date variable by > extracting the dates for specific departments.I want to extract the dates > for departments CC, DD, FF, put it in a new column and repeat it for other > unique IDs. > Subject<- c("2", "2", "2", "3", "3", "3", "4", "4", "5", "5", "5", > "5")dates<-seq(as.Date('2011-01-01'),as.Date('2011-01-12'),by = > 1) deps<-c("A", "B", "CC", "C", "CC", "A", "F", "DD", "A", "F", "FF", > "D")df <- data.frame(Subject, dates, deps)df > The final data set should look like this:newdate<-c(" 2011-01-03", > "2011-01-03", "2011-01-03", "2011-01-05", "2011-01-05", "2011-01-05" , > "2011-01-08", "2011-01-08", "2011-01-08", "2011-01-11", "2011-01-11", > "2011-01-11")final<-data.frame(Subject, dates, deps, newdate)final > I really appreciate any help. > Best,Farnoosh > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. [[alternative HTML version deleted]] ______________________________________________ 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.