R中时间序列的均值 [英] Means of timeseries in R

查看:38
本文介绍了R中时间序列的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 很陌生,只是写了这个来获取一个文件中多个时间序列的平均值:

I'm very new to R and just wrote this to obtain the mean for a number of timeseries in one file:

compiled<-read.table("/Users/Desktop/A/1.txt", header=TRUE)

z<-ncol(compiled)

comp_df<-data.frame(compiled[,2:z])

indmean<- rowMeans(comp_df)

每个文件中的数据如下所示:

The data in each file looks something like this:

Time A1 A2 A3 A4 A5

1 0.1 0.2 0.1 0.2 0.3


2 0.2 0.3 0.4 0.2 0.3

...

它工作正常,但我希望将其应用于许多相同性质的文件,每个文件中的时间序列数量不同.如果有人可以就我如何改进上述内容提出建议,那就太好了.提前致谢!

It works fine but I am hoping to apply this to many files of the same nature, with varying numbers of timeseries in each file. If anyone could advise on how I can improve the above to do so, it would be great. Thank you in advance!

推荐答案

您可以执行上面概述的步骤 - 将它们汇总到一个函数中,然后将它们 lapply 放在包含您要对其进行此分析的文件的名称.根据您需要做什么,从后续分析中拆分数据读取可能有意义也可能没有意义,以便您可以将数据保留在您的工作环境中.为简单起见,我假设您不需要数据后记.

You can steps you've outlined above - roll them up into a function, and the lapply them over a vector that contains the names of the files you want to do this analysis on. Depending on what you need to do, splitting the reading of the data in from the subsequent analysis may or may not make sense so that you can keep the data in your working environment. For the sake of simplicity, I'm going to assume you don't need the data afterwords.

一般步骤是:

1) 创建要处理的文件的向量.类似的东西:

1) Create a vector of your files to be processed. Something like:

filesToProcess <- dir(pattern = "yourPatternHere")

2) 把你上面的代码变成一个函数

2) Turn your code above into a function

FUN <- function(dat){   
  compiled<-read.table(dat, header=TRUE)
  z<-ncol(compiled)
  comp_df<-data.frame(compiled[,2:z])
  indmean<- rowMeans(comp_df)
  return(indmean)
}

3) lapply 将 FUNction 添加到您的文件列表并分配一个新变量:

3) lapply the FUNction to your list of files and assign a new variable:

out <- lapply(filesToProcess, FUN)

4) 给 out 一些名字,让你知道什么是什么:

4) Give out some names so you know what goes to what:

names(out) <- filesToProcess

您现在有一个命名列表,其中包含您在 filesToProcess 中列出的所有文件的 rowMeans.

You now have a named list that contains the rowMeans for all files you listed in filesToProcess.

这篇关于R中时间序列的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆