简体   繁体   中英

How to use the rollmean function on only one column of a zoo object?

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 ?

使用as.numeric将其转换为向量

rollmean(as.numeric(z$bar), 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM