将脚本重复应用于 R 中的 n 个 .csv 文件的最佳方法是什么?

发布于 2024-11-04 00:21:27 字数 1206 浏览 0 评论 0原文

我的情况

  1. 我有许多csv文件,它们的后缀都是相同的pre .csv,但文件名的前两个字符不同(即AA01.csv,AB01.csv,AC01.csv)等)
  2. 我有一个 R 脚本,我想在每个文件上运行它。该文件本质上是从 .csv 中提取数据并将它们分配给向量/将它们转换为时间序列对象。 (例如,AA01 xts timeseries 对象、AB01 xts 对象)

我想要实现的目标

  1. 将脚本嵌入到更大的循环中(或根据需要),以按顺序运行每个文件并应用脚本
  2. 删除创建的中间对象(参见下面的代码片段)
  3. 留给我从每个原始数据文件创建的最终 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:

  1. 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)
  2. 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:

  1. Embed the script within a larger loop (or as appropriate) to sequentially run over each file and apply the script
  2. Remove the intermediate objects created (see code snippet below)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-11-11 00:21:27

请务必使用 Rs dir 命令生成文件名列表,而不是手动输入它们。

filenames = dir(pattern="*01.csv")
for( i in 1:length(filenames) )
{
  ...

Be sure to use Rs dir command to produce the list of filenames instead of manually entering them in.

filenames = dir(pattern="*01.csv")
for( i in 1:length(filenames) )
{
  ...
南巷近海 2024-11-11 00:21:27

我发现 for 循环和列表对于这样的事情来说已经足够了。一旦你有了一组工作代码,就很容易从循环转移到可以 sapply 或类似的函数中,但这种向量化无论如何都是特殊的,并且可能在私有向量化之外没有用处-衬垫。

您可能希望避免分配给工作区中具有不同名称的多个对象(这是一个常见问题解答,通常显示为“如何分配()......”)。

请注意我未经测试的代码。

文件名向量以及每个文件的命名元素列表。

files <- c("AA01.csv", "AA02.csv")
lst <- vector("list", length(files))
names(lst) <- files

循环每个文件。

library(timeSeries)

for (i in 1:length(files)) {
    ## read strings as character
    tmp <- read.csv(files[i], stringsAsFactors = FALSE)
    ## convert to 'timeDate'
    tmp$tfrm <- timeDate(paste(tmp$cdt, tmp$ctm),format ="%Y/%m/%d %H:%M:%S"))
    ## create timeSeries object
    obj <- timeSeries(as.matrix(tmp$Value), tmp$tfrm)
    ## store object in the list, by name
    lst[[files[i]]] <- as.xts(obj)
}

## clean up
rm(tmp, files, obj)

现在所有读取的对象都在 lst 中,但您需要测试该文件是否可用、是否已正确读取,并且您可能需要修改名称以使其比仅修改名称更明智文件名。

按名称索引打印列表中的第一个对象:

lst[[files[1]]]

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 sapplyied 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.

files <- c("AA01.csv", "AA02.csv")
lst <- vector("list", length(files))
names(lst) <- files

Loop over each file.

library(timeSeries)

for (i in 1:length(files)) {
    ## read strings as character
    tmp <- read.csv(files[i], stringsAsFactors = FALSE)
    ## convert to 'timeDate'
    tmp$tfrm <- timeDate(paste(tmp$cdt, tmp$ctm),format ="%Y/%m/%d %H:%M:%S"))
    ## create timeSeries object
    obj <- timeSeries(as.matrix(tmp$Value), tmp$tfrm)
    ## store object in the list, by name
    lst[[files[i]]] <- as.xts(obj)
}

## clean up
rm(tmp, files, obj)

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:

lst[[files[1]]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文