--- Begin Message ---
This may be useful; convert them to NAs then omit them afterwards, you
end up keeping the positions where you had 0s.

> n[y==0] <- NA

> n
 [1]  1  1  1  1  1  1  2  2 NA NA NA  2  3  3 NA NA  3  3

> na.omit(n)
 [1] 1 1 1 1 1 1 2 2 2 3 3 3 3
 attr(,"na.action")
 [1]  9 10 11 15 16
 attr(,"class")
 [1] "omit"

HTH, Filipe

-----Mensagem original-----
De: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
Em nome de Duncan Murdoch
Enviada em: quarta-feira, 29 de junho de 2011 17:33
Para: Peter Maclean
Cc: r-help@r-project.org
Assunto: 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")

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@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.

______________________________________________
R-help@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.

--- End Message ---
"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."
______________________________________________
R-help@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.

Reply via email to