Hi,? I have a data set like below: Subject<- c("2", "2", "2", "3", "3", "3", "4", "4", "5", "5", "5", "5")dates<-c("2011-01-01", "2011-01-01", "2011-01-03" ,"2011-01-04", "2011-01-05", "2011-01-06" ,"2011-01-07", "2011-01-07", "2011-01-09" ,"2011-01-10"? ? ? ? ?,"2011-01-11" ,"2011-01-11")deps<-c("A", "B", "CC", "C", "CC", "A", "F", "DD", "A", "F", "FF", "D")df <- data.frame(Subject, dates, deps); df I want to choose unique dates per ID in a way there are not duplicate dates per ID. I don't mind what department to pick.?I really appreciate any help.?Best,Farnoosh [[alternative HTML version deleted]]
Hi Farnoosh, you can use unique in the R-base or distinct from the dplyr library. Best Ulrik On Tue, 15 Nov 2016 at 06:59 Farnoosh Sheikhi via R-help < r-help at r-project.org> wrote:> Hi, > I have a data set like below: > Subject<- c("2", "2", "2", "3", "3", "3", "4", "4", "5", "5", "5", > "5")dates<-c("2011-01-01", "2011-01-01", "2011-01-03" ,"2011-01-04", > "2011-01-05", "2011-01-06" ,"2011-01-07", "2011-01-07", "2011-01-09" > ,"2011-01-10" ,"2011-01-11" ,"2011-01-11")deps<-c("A", "B", "CC", > "C", "CC", "A", "F", "DD", "A", "F", "FF", "D")df <- data.frame(Subject, > dates, deps); df > I want to choose unique dates per ID in a way there are not duplicate > dates per ID. I don't mind what department to pick. I really appreciate any > help. Best,Farnoosh > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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]]
Hi Farnoosh, Try this: for(id in unique(df$Subject)) { whichsub<-df$Subject==id if(exists("newdf")) newdf<-rbind(newdf,df[whichsub,][which(!duplicated(df$dates[whichsub])),]) else newdf<-df[whichsub,][which(!duplicated(df$dates[whichsub])),] } Jim On Tue, Nov 15, 2016 at 9:38 AM, Farnoosh Sheikhi via R-help <r-help at r-project.org> wrote:> Hi, > I have a data set like below: > Subject<- c("2", "2", "2", "3", "3", "3", "4", "4", "5", "5", "5", "5")dates<-c("2011-01-01", "2011-01-01", "2011-01-03" ,"2011-01-04", "2011-01-05", "2011-01-06" ,"2011-01-07", "2011-01-07", "2011-01-09" ,"2011-01-10" ,"2011-01-11" ,"2011-01-11")deps<-c("A", "B", "CC", "C", "CC", "A", "F", "DD", "A", "F", "FF", "D")df <- data.frame(Subject, dates, deps); df > I want to choose unique dates per ID in a way there are not duplicate dates per ID. I don't mind what department to pick. I really appreciate any help. Best,Farnoosh > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
library(data.table) setDT(df) setkeyv(df, c("Subject", "dates")) unique(df) #gets what you want. On Mon, Nov 14, 2016 at 11:38 PM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Farnoosh, > Try this: > > for(id in unique(df$Subject)) { > whichsub<-df$Subject==id > if(exists("newdf")) > newdf<-rbind(newdf,df[whichsub,][which(!duplicated( > df$dates[whichsub])),]) > else newdf<-df[whichsub,][which(!duplicated(df$dates[whichsub])),] > } > > Jim > > > On Tue, Nov 15, 2016 at 9:38 AM, Farnoosh Sheikhi via R-help > <r-help at r-project.org> wrote: > > Hi, > > I have a data set like below: > > Subject<- c("2", "2", "2", "3", "3", "3", "4", "4", "5", "5", "5", > "5")dates<-c("2011-01-01", "2011-01-01", "2011-01-03" ,"2011-01-04", > "2011-01-05", "2011-01-06" ,"2011-01-07", "2011-01-07", "2011-01-09" > ,"2011-01-10" ,"2011-01-11" ,"2011-01-11")deps<-c("A", "B", "CC", > "C", "CC", "A", "F", "DD", "A", "F", "FF", "D")df <- data.frame(Subject, > dates, deps); df > > I want to choose unique dates per ID in a way there are not duplicate > dates per ID. I don't mind what department to pick. I really appreciate any > help. Best,Farnoosh > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at 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 at 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]]