Hi all, I have a sample of data set with variables day, ID,var and month: day ID var Month 1 11 x March 1 11 x March 1 11 x April 1 11 y March 1 11 x March 1 11 y March 2 11 x March 2 11 y March 3 11 x March 3 11 y March 4 11 y March In the above data set there are four unique days within ID. For example ID "11" has four records on day 1 and if this ID has "y" in "March" then I want keep "y" and discard "xs". Is it possible to keep them in two data sets ( wanted and unwanted) The output for wanted data set will be: 1 11 y March 1 11 x April 1 11 y March 2 11 y March 3 11 y March 4 11 y March thanks in advance [[alternative HTML version deleted]]
I don't quite understand what you are looking for. I originally thought that a simple subset would do it but I don't understand the selection criteria are for the output data file.s Original idea: want <- subset(dat1, dat1$var =="y") John Kane Kingston ON Canada> -----Original Message----- > From: valkremk at gmail.com > Sent: Tue, 11 Feb 2014 08:26:39 -0500 > To: r-help at r-project.org > Subject: [R] (no subject) > > Hi all, > I have a sample of data set with variables day, ID,var and month: > day ID var Month > 1 11 x March > 1 11 x March > 1 11 x April > 1 11 y March > 1 11 x March > 1 11 y March > 2 11 x March > 2 11 y March > 3 11 x March > 3 11 y March > 4 11 y March > > In the above data set there are four unique days within ID. For example > ID "11" has four records on day 1 and if this ID has "y" in "March" then > I > want keep "y" and discard "xs". Is it possible to keep them in two data > sets ( wanted and unwanted) > The output for wanted data set will be: > > 1 11 y March > 1 11 x April > 1 11 y March > 2 11 y March > 3 11 y March > 4 11 y March > thanks in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
Hi, May be this helps: dat <- read.table(text="day ID var? Month ? 1 11? x March ? 1 11? x March ? 1 11? x April ? 1 11? y March ? 1 11? x March ? 1 11? y March ? 2 11? x March ? 2 11? y March ? 3 11? x March ? 3 11? y March ? 4 11? y March",sep="",header=TRUE,stringsAsFactors=FALSE) ?indx <- with(dat,var=="y"& Month=="March"|var=="x" & Month!="March") ?dat1 <- dat[indx,] dat2 <- dat[!indx,] dat1$Month <- factor(dat1$Month,levels=month.name) ?dat1[with(dat1,order(day,ID,Month)),] A.K. On Tuesday, February 11, 2014 8:29 AM, Val <valkremk at gmail.com> wrote: Hi all, I have a sample of data? set with variables day, ID,var and month: day ID var? Month ? 1 11? x March ? 1 11? x March ? 1 11? x April ? 1 11? y March ? 1 11? x March ? 1 11? y March ? 2 11? x March ? 2 11? y March ? 3 11? x March ? 3 11? y March ? 4 11? y March In the above data set there are four unique days within ID. For example ID "11" has four records? on day 1 and if this ID has "y" in "March" then I want keep "y"? and discard "xs". Is it possible to keep them in two data sets ( wanted and unwanted) The output for wanted data set will be: 1 11 y March 1 11 x April 1 11 y March 2 11 y March 3 11 y March 4 11 y March thanks in advance ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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 and provide commented, minimal, self-contained, reproducible code.
On Feb 11, 2014, at 5:26 AM, Val wrote:> Hi all, > I have a sample of data set with variables day, ID,var and month: > day ID var Month > 1 11 x March > 1 11 x March > 1 11 x April > 1 11 y March > 1 11 x March > 1 11 y March > 2 11 x March > 2 11 y March > 3 11 x March > 3 11 y March > 4 11 y March > > In the above data set there are four unique days within ID. For example > ID "11" has four records on day 1 and if this ID has "y" in "March" then I > want keep "y" and discard "xs". Is it possible to keep them in two data > sets ( wanted and unwanted) > The output for wanted data set will be: > > 1 11 y March > 1 11 x April > 1 11 y March > 2 11 y March > 3 11 y March > 4 11 y March > thanks in advancePerhaps: wanted <- dat[!duplicated(dat), ] unwanted <- dat[duplicated(dat), ] ?duplicated -- David. David Winsemius Alameda, CA, USA