简体   繁体   English

如何处理R中的数百个文件?

[英]How to handle parsing hundreds files in R?

I want to parse last year weather data which are recorded in CSV files. 我想解析去年的天气数据,这些数据记录在CSV文件中。 Each CSV file includes one day of data. 每个CSV文件都包含一天的数据。 So I have 365 CSV files need to parse. 所以我有365个CSV文件需要解析。 What is the best way to handle these files? 处理这些文件的最佳方法是什么? As far as I know, I need to load all of them into R and bind them into one big data frame. 据我所知,我需要将它们全部加载到R中并将它们绑定到一个大数据帧中。 But I don't know whether this is the best solution. 但是我不知道这是否是最好的解决方案。 What if I have more than one years of data files? 如果我有超过一年的数据文件怎么办? Do I need to load all of them into memory? 我是否需要将所有这些都加载到内存中? Or is there any other way to handle them? 还是有其他方法来处理它们?

Each file is about 1M to 1.5M. 每个文件约为1M至1.5M。

The easiest way to do this is to get all your files to read using list.files , read them into a list of data frames, then rbind all the frames together: 最简单的方法是使用list.files读取所有文件,将它们读入数据帧列表,然后将所有帧捆绑在一起:

#setwd('dirwithallmycsvs')

x <- list.files(pattern = '.+\\.csv$')

out = lapply(x, read.csv)

out2 = do.call(rbind, out)

Your output should now be one dataframe. 现在,您的输出应该是一个数据帧。 You will need to take care all the columns are the same across your files. 您需要注意文件中的所有列都相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM