People with more experience in R I need help on this. I would like to drop observation if they meet certain condition. In this example I would like to drop group 2?in "n" because the group in "Y" has more than 2 zeroes.? #Example n <- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,3) y <- c(2,3,2,3,4,5,6,1,0,0,0,6, 2, 1, 0, 0,9,3) z <- as.data.frame(cbind(n,y)) colnames(z) <- c("n","y")
On 29/06/2011 4:29 PM, Peter Maclean wrote:> People with more experience in R I need help on this. > I would like to drop observation if they meet certain condition. In this example > I would like to drop group 2 in "n" because the group in "Y" has more than 2 > zeroes. > #Example > n<- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,3) > y<- c(2,3,2,3,4,5,6,1,0,0,0,6, 2, 1, 0, 0,9,3) > z<- as.data.frame(cbind(n,y)) > colnames(z)<- c("n","y")The general way to drop observations is to construct a logical vector to use as an index. Entries which are FALSE are dropped. Doing that based on your "more than 2 zeroes" rule looks a little tricky; I think you want to count zeros first (e.g. using by()), then construct the TRUE/FALSE vector based on that. Duncan Murdoch> > > ______________________________________________ > 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 Jun 29, 2011, at 4:29 PM, Peter Maclean wrote:> People with more experience in R I need help on this. > I would like to drop observation if they meet certain condition. In > this example > I would like to drop group 2 in "n" because the group in "Y" has > more than 2 > zeroes. > #Example > n <- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,3) > y <- c(2,3,2,3,4,5,6,1,0,0,0,6, 2, 1, 0, 0,9,3) > z <- as.data.frame(cbind(n,y))The strategy of cbind vectors as an argument to data.frame and then naming them seems wasteful and error prone. Why not: z <- data.frame(n=factor(n),y=y) # all one step, no issues about every element needing to be the same mode # and not removing attributes that matrix class imposes. The you can use ave() to return a group-computed counts of zeroes: z> with(z, ave(y, n, FUN=function(x) sum(x==0) ) ) [1] 0 0 0 0 0 0 3 3 3 3 3 3 2 2 2 2 2 2 And us that to test for you condition for not dropping: > z[ with(z, ave(y, n, FUN=function(x) sum(x==0) ) ) <= 2, ] n y 1 1 2 2 1 3 3 1 2 4 1 3 5 1 4 6 1 5 13 3 2 14 3 1 15 3 0 16 3 0 17 3 9 18 3 3> colnames(z) <- c("n","y") > > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
An embedded message was scrubbed... From: "Filipe Leme Botelho" <filipe.botelho at vpar.com.br> Subject: RES: [R] DROP OBSEVATION IN A GROUP Date: Wed, 29 Jun 2011 17:46:46 -0300 Size: 3416 URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110629/1a2fe184/attachment.mht> -------------- next part -------------- "This message and its attachments may contain confidential and/or privileged information. If you are not the addressee, please, advise the sender immediately by replying to the e-mail and delete this message." "Este mensaje y sus anexos pueden contener informaci?n confidencial o privilegiada. Si ha recibido este e-mail por error por favor b?rrelo y env?e un mensaje al remitente." "Esta mensagem e seus anexos podem conter informa??o confidencial ou privilegiada. Caso n?o seja o destinat?rio, solicitamos a imediata notifica??o ao remetente e exclus?o da mensagem."
I tried this but did not work: z0<- by(z, z[,"n"], function(x) subset(x, sum(n==0)>2)) ?Peter Maclean Department of Economics UDSM ----- Original Message ---- From: Duncan Murdoch <murdoch.duncan at gmail.com> To: Peter Maclean <pmaclean2011 at yahoo.com> Cc: r-help at r-project.org Sent: Wed, June 29, 2011 3:33:25 PM Subject: Re: [R] DROP OBSEVATION IN A GROUP On 29/06/2011 4:29 PM, Peter Maclean wrote:> People with more experience in R I need help on this. > I would like to drop observation if they meet certain condition. In this >example > I would like to drop group 2 in "n" because the group in "Y" has more than 2 > zeroes. > #Example > n<- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,3) > y<- c(2,3,2,3,4,5,6,1,0,0,0,6, 2, 1, 0, 0,9,3) > z<- as.data.frame(cbind(n,y)) > colnames(z)<- c("n","y")z0<- by(z, z[,"n"], function(x) subset(x, sum(n==0)>2)) The general way to drop observations is to construct a logical vector to use as an index.? Entries which are FALSE are dropped. Doing that based on your "more than 2 zeroes" rule looks a little tricky; I think you want to count zeros first (e.g. using by()), then construct the TRUE/FALSE vector based on that. Duncan Murdoch> > > ______________________________________________ > 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.