s-news
[Top] [All Lists]

Logical operators on vector

To: <s-news@lists.biostat.wustl.edu>
Subject: Logical operators on vector
From: "Haiyan Gao" <h.gao@herts.ac.uk>
Date: Sun, 26 Oct 2003 19:20:56 -0000

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

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

Can anyone point to me the reasons and suggest me how to deal with missing values?

Thanks,

Haiyan Gao

 

<Prev in Thread] Current Thread [Next in Thread>