Thanks a bunch.

I happened to generate another dataset to try the command you all 
provided. my.data$group is factor and so the "!my.data$group" command 
doesn't work but the "!with" statement works for both situation. So what i 
can modify and make the first one work, just curious. 

It is of great help.

Kevin


>my.data
             y group f1
1           NA     a  1
7           NA     a  1
4           NA     a  2
10 -0.04517434     a  2
5  -0.86386128     b  1
11 -0.06011077     b  1
2   0.19355534     b  2
8   0.39855870     b  2
3   0.76880313     c  1
9  -0.56934018     c  1
6           NA     c  2
12          NA     c  2

Kevin H. Lin
The University of Texas MD Anderson Cancer Center
1808 Park Road 1C
Smithville, Texas 78957
512-237-9379 (voice)
[EMAIL PROTECTED]






"Petr Pikal" <[EMAIL PROTECTED]>
02/18/05 01:41 AM

 

To:
[email protected], [EMAIL PROTECTED]
cc:

Subject:
Re: [R] help on deleting NAs





 
 
> There's probably a lot of niftier ways but this will give an idea: If
> X is your dataframe above,

Hi

I am not sure if it is niftier but

> x <- read.table("clipboard",header=T)
> x[!x$group %in% which(tapply(is.na(x$y), x$group, sum) > 2), ]
            y group f1 f2 f3
25  1.3401226     2  1  1  1
13  1.2619082     2  1  2  1
14 -0.4323220     2  3  1  1
36  0.8406529     2  3  2  2
21  0.9604758     3  1  2  1
18  0.9562072     3  2  1  1
45  1.1285016     3  2  1  1
50         NA     4  1  1  1
11         NA     4  1  1  2
41 -1.1017167     4  2  1  1
37  0.9661283     4  3  1  1
39 -0.2540905     4  3  1  2

or if you want to use this 50% margin

x[!x$group %in% which (tapply(is.na(x$y),x$group,sum)/ 
tapply(is.na(x$y),x$group,length)>.5),]

gives you what you want.

Cheers
Petr






> 
> > aa <- with(X, tapply((y), group, function(x) length(x[is.na(x)])))
> > names(aa[aa>2])
> [1] "1"
> 
> > X[!with(X, group%in%as.numeric(names(aa[aa>2]))),]
>             y group f1 f2 f3
> 6   1.3401226     2  1  1  1
> 7   1.2619082     2  1  2  1
> 8  -0.4323220     2  3  1  1
> 9   0.8406529     2  3  2  2
> 10  0.9604758     3  1  2  1
> 11  0.9562072     3  2  1  1
> 12  1.1285016     3  2  1  1
> 13         NA     4  1  1  1
> 14         NA     4  1  1  2
> 15 -1.1017167     4  2  1  1
> 16  0.9661283     4  3  1  1
> 17 -0.2540905     4  3  1  2
> > 
> 
> The function in the tapply part could be made more general if 3
> doesn't always constitute a majority.
> 
> HTH
> 
> -- 
> Patrick Connolly
> HortResearch
> Mt Albert
> Auckland
> New Zealand 
> Ph: +64-9 815 4200 x 7188
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
> ~ I have the world`s largest collection of seashells. I keep it on all
> the beaches of the world ... Perhaps you`ve seen it.  ---Steven Wright
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
> ~
> 
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
[EMAIL PROTECTED]




        [[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to