[R] DROP OBSEVATION IN A GROUP

Peter Maclean pmaclean2011 at yahoo.com
Thu Jun 30 07:05:32 CEST 2011


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.



More information about the R-help mailing list