将脚本重复应用于 R 中的 n 个 .csv 文件的最佳方法是什么?
我的情况:
- 我有许多csv文件,它们的后缀都是相同的pre .csv,但文件名的前两个字符不同(即AA01.csv,AB01.csv,AC01.csv)等)
- 我有一个 R 脚本,我想在每个文件上运行它。该文件本质上是从 .csv 中提取数据并将它们分配给向量/将它们转换为时间序列对象。 (例如,AA01 xts timeseries 对象、AB01 xts 对象)
我想要实现的目标:
- 将脚本嵌入到更大的循环中(或根据需要),以按顺序运行每个文件并应用脚本
- 删除创建的中间对象(参见下面的代码片段)
- 留给我从每个原始数据文件创建的最终 xts 对象(即 AA01 到 AC01 等作为值/向量等)
嵌入此脚本的正确方法是什么在 R 中?抱歉,我是一个编程菜鸟!
我的脚本代码如下...每个 CSV 中每列的标题是日期、时间、值
# Pull in Data from the FileSystem and attach it
AA01raw<-read.csv("AA01.csv")
attach(AA01raw)
#format the data for timeseries work
cdt<-as.character(Date)
ctm<-as.character(Time)
tfrm<-timeDate(paste(cdt,ctm),format ="%Y/%m/%d %H:%M:%S")
val<-as.matrix(Value)
aa01tsobj<-timeSeries(val,tfrm)
#convert the timeSeries object to an xts Object
aa01xtsobj<-as.xts(tsobj)
#remove all the intermediate objects to leave the final xts object
rm(cdt)
rm(ctm)
rm(aa01tsobj)
rm(tfrm)
gc()
,然后对每个 .csv 文件重复,直到提取所有 xts 对象。
也就是说,我们在 R 中最终会得到什么,为进一步的应用做好准备:
aa01xtsobj, ab01xtsobj, ac01xtsobj....etc
任何有关如何做到这一点的帮助将非常感激。
My situation:
- I have a number of csv files all with the same suffix pre .csv, but the first two characters of the file name are different (ie AA01.csv, AB01.csv, AC01.csv etc)
- I have an R script which I would like to run on each file. This file essentially extracts the data from the .csv and assigns them to vectors / converts them into timeseries objects. (For example, AA01 xts timeseries object, AB01 xts object)
What I would like to achieve:
- Embed the script within a larger loop (or as appropriate) to sequentially run over each file and apply the script
- Remove the intermediate objects created (see code snippet below)
- Leave me with the final xts objects created from each raw data file (ie AA01 to AC01 etc as Values / Vectors etc)
What would be the right way to embed this script in R? Sorry, but I am a programming noob!
My script code below...heading of each column in each CSV is DATE, TIME, VALUE
# Pull in Data from the FileSystem and attach it
AA01raw<-read.csv("AA01.csv")
attach(AA01raw)
#format the data for timeseries work
cdt<-as.character(Date)
ctm<-as.character(Time)
tfrm<-timeDate(paste(cdt,ctm),format ="%Y/%m/%d %H:%M:%S")
val<-as.matrix(Value)
aa01tsobj<-timeSeries(val,tfrm)
#convert the timeSeries object to an xts Object
aa01xtsobj<-as.xts(tsobj)
#remove all the intermediate objects to leave the final xts object
rm(cdt)
rm(ctm)
rm(aa01tsobj)
rm(tfrm)
gc()
and then repeat on each .csv file til all xts objects are extracted.
ie, what we would end up within R, ready for further applications are:
aa01xtsobj, ab01xtsobj, ac01xtsobj....etc
any help on how to do this would be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请务必使用 Rs
dir
命令生成文件名列表,而不是手动输入它们。Be sure to use Rs
dir
command to produce the list of filenames instead of manually entering them in.我发现 for 循环和列表对于这样的事情来说已经足够了。一旦你有了一组工作代码,就很容易从循环转移到可以 sapply 或类似的函数中,但这种向量化无论如何都是特殊的,并且可能在私有向量化之外没有用处-衬垫。
您可能希望避免分配给工作区中具有不同名称的多个对象(这是一个常见问题解答,通常显示为“如何分配()......”)。
请注意我未经测试的代码。
文件名向量以及每个文件的命名元素列表。
循环每个文件。
现在所有读取的对象都在
lst
中,但您需要测试该文件是否可用、是否已正确读取,并且您可能需要修改名称以使其比仅修改名称更明智文件名。按名称索引打印列表中的第一个对象:
I find a for loop and lists is well enough for stuff like this. Once you have a working set of code it's easy enough to move from a loop into a function which can be
sapply
ied or similar, but that kind of vectorization is idiosyncratic anyway and probably not useful outside of private one-liners.You probably want to avoid assigning to multiple objects with different names in the workspace (this a FAQ which usually comes up as "how do I assign() . . .").
Please beware my untested code.
A vector of file names, and a list with a named element for each file.
Loop over each file.
Now all the read objects are in
lst
, but you'll want to test that the file is available, that it was read correctly, and you may want to modify the names to be more sensible than just the file name.Print out the first object by name index from the list: