简体   繁体   English

使用与数据帧对应的文件名的 for 循环保存数据帧

[英]Saving data frames using a for loop with file names corresponding to data frames

I have a few data frames (colors, sets, inventory) and I want to save each of them into a folder that I have set as my wd.我有一些数据框(颜色、集合、库存),我想将它们中的每一个保存到我设置为 wd 的文件夹中。 I want to do this using a for loop, but I am not sure how to write the file argument such that R understands that it should use the elements of the vector as the file names.我想使用 for 循环来执行此操作,但我不确定如何编写文件参数,以便 R 理解它应该使用向量的元素作为文件名。

I might write:我可能会写:

DFs <- c("colors", "sets", "inventory")
for (x in 1:length(DFs)){
  save(x, file = "x.Rda")
  }

The goal would be that the files would save as colors.Rda, sets.Rda, etc. However, the last element to run through the loop simply saves as x.Rda.目标是文件将保存为 colors.Rda、sets.Rda 等。但是,通过循环运行的最后一个元素只是保存为 x.Rda。

In short, perhaps my question is: how do you tell R that I am wanting to use elements being run through a loop within an argument when that argument requires a character string?简而言之,也许我的问题是:当参数需要字符串时,您如何告诉 R 我想使用在参数中循环运行的元素?

For bonus points, I am sure I will encounter the same problem if I want to load a series of files from that folder in the future.对于奖励积分,如果我将来想从该文件夹加载一系列文件,我相信我会遇到同样的问题。 Rather than loading each one individually, I'd also like to write a for loop.与其单独加载每个,我还想编写一个 for 循环。 To load these a few minutes ago, I used the incredibly clunky code:为了在几分钟前加载这些,我使用了非常笨重的代码:

sets_file <- "~/Documents/ME teaching/R notes/datasets/sets.csv"
sets <- read.csv(sets_file)

inventories_file <- "~/Documents/ME teaching/R notes/datasets/inventories.csv"
inventories <- read.csv(inventories_file)

colors_file <- "~/Documents/ME teaching/R notes/datasets/colors.csv"
colors <- read.csv(colors_file)

To save you can do this:为了节省你可以这样做:

DFs <- list(color, sets, inventory)
names(DFs) = c("color", "sets", "inventory")
for (x in 1:length(DFs)){
  dx = paste(names(DFs)[[x]], "Rda", sep = ".")
  dfx = DFs[[x]]
  save(dfx, file = dx)
}

To specify the path just inform in the construction of the dx object as following to read.要指定路径,只需在 dx object 的构造中通知如下即可阅读。

To read:读书:

DFs <- c("colors", "sets", "inventory")
# or
DFs = dir("~/Documents/ME teaching/R notes/datasets/")
for(x in 1:length(DFs)){
  arq = paste("~/Documents/ME teaching/R notes/datasets/", DFs[x], ".csv", sep = "")
  DFs[x] = read.csv(arq)
}

It will read as a list, so you can access using [[]] indexation.它将作为列表读取,因此您可以使用 [[]] 索引进行访问。

For compactness I use lapply instead of a for loop here, but the idea is the same:为了紧凑,我在这里使用lapply而不是for循环,但想法是一样的:

lapply(DFs, \(x) save(list=x, file=paste0(x, ".Rda"))))

Note that you need to generate the varying file names by providing x as a variable and not as a character (as part of the file name).请注意,您需要通过将x作为变量而不是字符(作为文件名的一部分)提供来生成不同的文件名。

To load those files, you can simply do:要加载这些文件,您只需执行以下操作:

lapply(paste0(DFs, ".Rda"), load, envir = globalenv())

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

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