Question

I am trying to use the rollmean function on a zoo object that contains non-numerical columns. My goal is to add a new column to the existing object.

Why does the following test code not work?

library(zoo)
d <- data.frame(time=c(1, 2, 3), foo=c('a', 'b', 'c'), bar=c(32, 4, 1))
z <- zoo(d, order.by=d$time)
rollmean(z$bar, 2)

I get a "non-numeric argument to binary operator" error. Shouldn't the function be only using the bar column? Do I have to create a separate zoo object for the bar column, use the rolling function on it and then merge the results with z?

Was it helpful?

Solution

Use as.numeric to convert it to vector

rollmean(as.numeric(z$bar), 2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top