R 分组数据移动平均

R 分组数据移动平均,r,R,我想计算我的数据集的移动平均值,它由一列表示分组患者的指数和第二列表示血液循环分子的一些测量值组成。根据感兴趣分子的连续测量对患者进行分组 此外,我还想绘制输出,其中每组的测量值与患者组编号对应 我试着编写这个分析代码,但我不确定我是否做得很好 SURG_DATE VES_2A索引 21/05/2013 1 1 10/06/2013 1 1 06/01/2014 1 1 29/01/2014 0 1 11/03/2014 3

我想计算我的数据集的移动平均值,它由一列表示分组患者的指数和第二列表示血液循环分子的一些测量值组成。根据感兴趣分子的连续测量对患者进行分组

此外,我还想绘制输出,其中每组的测量值与患者组编号对应

我试着编写这个分析代码,但我不确定我是否做得很好

SURG_DATE VES_2A索引
21/05/2013    1     1
10/06/2013    1     1
06/01/2014    1     1
29/01/2014    0     1
11/03/2014    3     2
05/04/2014    1     2
06/04/2014    1     2
14/05/2014    1     2
28/05/2014    3     3
02/09/2014    2     3
16/09/2014    2     3
17/09/2014    0     3
21/10/2014    2     5
05/12/2014    0     5
19/12/2014    2     5
11/01/2015    1     5
15/01/2015    1     6
17/01/2015    2     6
24/01/2015    1     6
19/02/2015    1     6
我尝试的代码:

tapply(test$VES_2A, 
       test$Index, 
       function(x) rollmean(x, 12, na.pad=TRUE))

使用末尾注释中重复显示的数据,分别对每个索引值取当前和之前两次观察的滚动平均值,并添加一个序列号。由于索引的每个值占用4行,所以我们使用1:4

这个问题不清楚要绘制什么,但我们在一个面板上绘制每个指数的滚动平均值vs.seq。对于经典grpahics,如果需要单独的面板,请将
screen=1
替换为
screen=colnames(宽)
。对于ggplot2,要获得单独的面板,请忽略
facet=NULL

library(zoo)

roll <- function(x) rollmeanr(x, 3, fill = NA)
df3 <- transform(df, mean3 = ave(VES_2A, Index, FUN = roll), seq = 1:4)

wide <- na.omit(read.zoo(df3[-1], index = "seq", split = "Index"))

# classic graphics
plot(wide, screen = 1, type = "o", pch = colnames(wide))

# ggplot2 gtraphics
library(ggplot2)
autoplot(wide[-3], facet = NULL)
图书馆(动物园)

滚动使用末尾注释中重复显示的数据,分别对每个索引值取当前和之前两次观察的滚动平均值,并添加一个序列号。由于索引的每个值占用4行,所以我们使用1:4

这个问题不清楚要绘制什么,但我们在一个面板上绘制每个指数的滚动平均值vs.seq。对于经典grpahics,如果需要单独的面板,请将
screen=1
替换为
screen=colnames(宽)
。对于ggplot2,要获得单独的面板,请忽略
facet=NULL

library(zoo)

roll <- function(x) rollmeanr(x, 3, fill = NA)
df3 <- transform(df, mean3 = ave(VES_2A, Index, FUN = roll), seq = 1:4)

wide <- na.omit(read.zoo(df3[-1], index = "seq", split = "Index"))

# classic graphics
plot(wide, screen = 1, type = "o", pch = colnames(wide))

# ggplot2 gtraphics
library(ggplot2)
autoplot(wide[-3], facet = NULL)
图书馆(动物园)

滚动这有点模棱两可,但我想你想要这个:

test <- cbind(time=rownames(test), test)  # first add a time variable

# then create a list with rolling mean for each id and time
ls1 <- lapply(seq_along(test$time), 
              function(x) cbind(time=x,  # time variable
                                with(test[test$time %in% 1:x, ], 
                                     aggregate(list(VES_2A=VES_2A), 
                                               list(Index=Index), mean))  # rolling mean
                                ))

tot <- transform(t(sapply(ls1, colMeans)), Index="total")  # occasionally add a total column

long <- rbind(do.call(rbind, ls1), tot)  # bind all rows together into long format data frame
wide <- reshape2::dcast(long, time ~ Index)  # reshape to wide w/ e.g. reshape2::dcast()
rm(ls1, tot)  # clean up
绘图

告诉我你的想法,希望那是你想要的

数据


test有点模棱两可,但我认为您需要:

test <- cbind(time=rownames(test), test)  # first add a time variable

# then create a list with rolling mean for each id and time
ls1 <- lapply(seq_along(test$time), 
              function(x) cbind(time=x,  # time variable
                                with(test[test$time %in% 1:x, ], 
                                     aggregate(list(VES_2A=VES_2A), 
                                               list(Index=Index), mean))  # rolling mean
                                ))

tot <- transform(t(sapply(ls1, colMeans)), Index="total")  # occasionally add a total column

long <- rbind(do.call(rbind, ls1), tot)  # bind all rows together into long format data frame
wide <- reshape2::dcast(long, time ~ Index)  # reshape to wide w/ e.g. reshape2::dcast()
rm(ls1, tot)  # clean up
绘图

告诉我你的想法,希望那是你想要的

数据


测试谢谢jay.sf。这正是我所需要的。我不得不说,我有时间信息,但我不清楚如何管理它们。由于需要您的帮助时间信息,我将编辑我的问题。此外,我还要问你:移动平均值是通过在你的答案中删除前一个测量值并添加下一个测量值来计算的吗?该函数计算各组在各自时间的
VES_2A
平均值。前面的平均值对计算没有影响。谢谢jay.sf。这正是我所需要的。我不得不说,我有时间信息,但我不清楚如何管理它们。由于需要您的帮助时间信息,我将编辑我的问题。此外,我还要问你:移动平均值是通过在你的答案中删除前一个测量值并添加下一个测量值来计算的吗?该函数计算各组在各自时间的
VES_2A
平均值。先前的平均值对计算没有影响。
test <- structure(list(VES_2A = c(1L, 1L, 1L, 0L, 3L, 1L, 1L, 1L, 3L, 
                                  2L, 2L, 0L, 2L, 0L, 2L, 1L, 1L, 2L, 1L, 1L), Index = c(1L, 1L, 
                                                                                         1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 6L, 6L, 
                                                                                         6L, 6L)), class = "data.frame", row.names = c(NA, -20L))