sts
sts

Reputation: 231

How to use the "!is.na" function with zoo objects

I have problems using the is.na function with a zoo object. Here is what I tried:

z1 <- zoo(matrix(1:8, 4, 2), as.Date("2003-01-01") + 0:3)
z1[2,1]<-NA

So, z1 is...

2003-01-01  1 5
2003-01-02 NA 6
2003-01-03  3 7
2003-01-04  4 8

When I use multiplying a column with is.na I get:

!is.na(z1[,1])*z1[,2]

It returned:

2003-01-01 2003-01-02 2003-01-03 2003-01-04 
      TRUE      FALSE       TRUE       TRUE 

However, when I simply do TRUE*100, the program returns numbers :

TRUE*100
[1] 100
FALSE*100
[1] 0

What function should I use to have the program returning numbers instead of TRUEs and FALSEs (I'm looking here for the equivalent function of !is.na for zoo objects)?

Upvotes: 0

Views: 209

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57686

! has lower precedence than *, so your first example is being parsed as

!(is.na(z1[,1]) * z1[,2]) 

See ?Syntax for operator precedence in R.

Upvotes: 2

Related Questions