1

The max of the Close column is valid, but why is the max of the lagged Close NA?

> library(quantmod)
> s <- get(getSymbols('nvmi'))
> max(Cl(s))
[1] 11.48
> max(Lag(Cl(s)))
[1] NA
> max(as.numeric(Lag(Cl(s))))
[1] NA
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
haki
  • 9,389
  • 15
  • 62
  • 110

1 Answers1

1

Because Lag pads with NA by default. Use na.rm=TRUE in your call to max.

> head(Cl(s))
           NVMI.Close
2007-01-03       2.60
2007-01-04       2.59
2007-01-05       2.70
2007-01-08       2.60
2007-01-09       2.47
2007-01-10       2.42
> head(Lag(Cl(s)))
           Lag.1
2007-01-03    NA
2007-01-04  2.60
2007-01-05  2.59
2007-01-08  2.70
2007-01-09  2.60
2007-01-10  2.47
> max(Lag(Cl(s)), na.rm=TRUE)
[1] 11.48
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418