Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一列每小时数据,想用它rollapply来计算每小时的 24 小时滚动平均值。我的数据包含NA's,如果 24 小时期间 75% 的数据可用,我只想计算滚动平均值,否则我希望考虑 24 滚动平均值NA。
rollapply
NA
df %>% mutate(rolling_avg = rollapply(hourly_data, 24, FUN = mean ,align = "right", fill = NA ))
我怎样才能修改上面的代码来完成这个?
定义一个函数来完全按照您的说明进行操作:
f <- function( v ) { if( sum(is.na(v)) > length(v)*0.25 ) return(NA) mean(v, na.rm = TRUE) }
然后用它代替mean:
mean
df %>% mutate(rolling_avg = rollapply(hourly_data, 24, FUN = f, align = "right", fill = NA ))