将多个csv文件平均化为1个平均文件 [英] Average multiple csv files into 1 averaged file in r

查看:190
本文介绍了将多个csv文件平均化为1个平均文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约300 csv文件的风速,温度,压力等,列,每一行是不同的时间,从2007年到2012年。每个文件是从不同的位置。我想将所有文件合并成一个是所有300个文件的平均值。因此,新文件将具有每个单独文件的相同数目的行和列,但每个单元格将是所有300个文件的对应平均值。有没有一个简单的方法来做到这一点?

I have approximately 300 csv files of wind speed, temp, pressure, etc, columns and each row is a different time from 2007 to 2012. Each file is from a different location. I want to combine all files into one that is the average of all 300 files. So the new file would have the same number of rows and columns of each individual file but each cell would be a corresponding average of all the 300 files. Is there an easy way to do this?

推荐答案

正在关注此帖,您可以将所有文件读入列表(这里我假设他们被命名为weather * .csv):

Following this post, you could read all the files into a list (here I've assumed they're named weather*.csv):

csvs <- lapply(list.files(pattern="weather*.csv"), read.csv)

剩下的就是取所有这些数据帧的平均值。您可以尝试类似:

All that remains is to take the average of all those data frames. You might try something like:

Reduce("+", csvs) / length(csvs)

如果只想添加一部分列,可以传递 Reduce 具有列的适当子集的数据帧列表。例如,如果你想删除每个列中的第一列,你可以这样做:

If you wanted to only add a subset of the columns, you could pass Reduce a list of data frames with the appropriate subset of the columns. For instance, if you wanted to remove the first column from each, you could do something like:

Reduce("+", lapply(csvs, "[", -1)) / length(csvs)

这篇关于将多个csv文件平均化为1个平均文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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