On Sun, 26 Oct 2003, Haiyan Gao wrote:
> Dear S-plus users,
>
> I encountered a problem in using logical operators on vector.
>
> In my data file, testData, there are three variables. I want to select
> "speldur" when both "hrg" and "filter1" are. The program and data are as
> follows :
>
> trimcase<-function(A1, opt){
> filter1 <- A1$filter1
> speldur<- A1$speldur
> hrg <- A1$hrg
> speli <- speldur[hrg==1&filter1==1]
> speli
> }
>
> testData <-
> hrg filter1 speldur
> 1 1 1 11
> 2 2 1 12
> 3 1 NA 13
> 4 2 1 NA
> 5 2 NA 4
>
> The answer should be 11, but the output
Not so.
> >trimcase(testData)
>
> >[1] 11 NA
>
> I also tried when there is no missing values in the data, the output is
> correct. It seems caused by missing values.
The output is correct in your example. In case 3 you don't know what
hrg==1&filter1==1 is: it might or might not be true. So there is case 1
and perhaps case 3, and the NA results from case 3.
> Can anyone point to me the reasons and suggest me how to deal with
> missing values?
You could read a good book on the subject to increase your understanding:
this is described in Venables & Ripley (2002) for example. As a hint, what
is the difference between filter1==1 and filter1 %in% 1?
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
|