如何解决计算R中特定于行的时间间隔的平均值
我有一个数据框,上面有猎物,携带物品的日期和估计的重量。有些行的NA为估计重量,我想通过在3天(即猎物交付的前一天,同一天和第二天)的3天内对同一类型的猎物的重量求平均值来推断出粗略的估计。我该怎么办?同样,数据按嵌套ID分组。 MRE:
example_data <- structure(list(broodID = structure(c(7L,1L,2L,3L,4L,6L,7L,8L,9L,9L),.Label = c("2018_1","2019_3","2020_10","2020_11","2020_12","2020_2","2020_5","2020_8","2020_9"),class = "factor"),date = structure(c(18469,17743,17724,17730,17731,17739,17723,17738,17742,17735,17732,17745,17737,17751,17725,17726,17729,18104,18102,18103,18098,18100,18462,18470,18459,18471,18473,18463,18474,18493,18479,18475,18472,18411,18461,18464,18449,18453,18452,18448,18456,18450,18451,18460,18447,18488,18458,18455,18469,18468,18466,18476,18482,18454,18457,18467,18465,18459),class = "Date"),PreyGroup = structure(c(1L,1L),.Label = "bird",weightcalc = c(10,5,10,15,20,25,40,45,NA,35,50,120,6,7,8,12,14,55,60,70,30,NA)),class = "data.frame",row.names = c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","123","124","125","126","127","128","129","130","131","132","133","134","135","136","137","138","139","140","141","142","143","144","145","146","147","148","149","150","151","152","153","154","155","156","157","158","159"))
解决方法
您可以首先创建一个新变量,其移动平均值为weightcalc
变量。 rollmean
软件包中的zoo
函数可以帮助您完成此任务。确保您查看?rollmean
来了解选项(align
和k
参数对您来说尤其重要):
library(zoo)
rollmean(c(1,2,3,4,NA,6,7,7),k=3,na.rm=TRUE)
#> [1] 2.000000 2.666667 3.333333 3.500000 5.000000 6.500000 6.666667
使用rollmean
的{{1}}和dplyr
函数在组内应用group_by
:
mutate
或者,您可以使用library(dplyr)
library(zoo)
dat = dataset %>%
arrange(PreyGroup,date) %>%
group_by(PreyGroup) %>%
mutate(weightcalc_ma = rollmean(weightcalc,k=4,fill=NA,na.rm=TRUE),weightcalc_new = ifelse(is.na(weightcalc),weightcalc_ma,weightcalc))
dat
#> # A tibble: 159 x 6
#> # Groups: PreyGroup [1]
#> broodID date PreyGroup weightcalc weightcalc_ma weightcalc_new
#> <fct> <date> <fct> <dbl> <dbl> <dbl>
#> 1 2018_1 2018-07-11 bird 15 NA 15
#> 2 2018_1 2018-07-11 bird NA 13.3 13.3
#> 3 2018_1 2018-07-12 bird 10 16.7 10
#> 4 2018_1 2018-07-12 bird 15 23.8 15
#> 5 2018_1 2018-07-12 bird 25 32.5 25
#> 6 2018_1 2018-07-13 bird 45 40 45
#> 7 2018_1 2018-07-13 bird 45 45 45
#> 8 2018_1 2018-07-13 bird 45 45 45
#> 9 2018_1 2018-07-13 bird 45 45 45
#> 10 2018_1 2018-07-14 bird NA 27.5 27.5
#> # … with 149 more rows
软件包:
data.table
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。