【问题标题】:Looping over all files in a folder in R over multiple loops在多个循环中循环遍历 R 中文件夹中的所有文件
【发布时间】:2020-08-23 04:23:11
【问题描述】:

我已经编写了在生物群系的 csv 文件上运行采样函数的代码。 我有 30 个 csv 文件,我想循环这些文件。我正在努力应用我在文件夹中的所有文件中轻松编写的代码。我确信这是一个简单的解决方法,我只是发现循环内的循环存在问题。

temp <- read.csv("tropical_grassland_N_Am_point_summary_table_gdrive.csv")
temp <- temp[which(temp$nd > 0.1),]


index <- substr(temp[,5],30,(nchar(as.character(temp[,5]))-2))
unique_index <- unique(index)
unique_index <- sample(unique_index, 20, replace=F)
for (i in 1:length(unique_index)){

  temp2 <- temp[which(index==unique_index[i]),]
  theDates = strptime(temp2[,2], format="%Y-%m-%d")
}

thresh <- 0.95

for (i in 1:length(unique_index)){

    temp2 <- temp[which(index==unique_index[i]),]
    theDates = strptime(temp2[,2], format="%Y-%m-%d")
  }

所有的 csv 文件在大陆和生物群落的描述之后都有 _point_summary_table_gdrive.csv 的共同点。

【问题讨论】:

  • 你可以从list.files(pattern="+csv")开始,然后通过文件名循环你的代码。

标签: r loops csv for-loop


【解决方案1】:

它们是否在没有其他文件的同一文件夹中?如果是这样,如果您的代码有效,您不需要知道文件名。只是做一个功能,然后

filenames <- list.files(path = whatyouwant, pattern = "*\\.csv$", all.files = TRUE,full.names = TRUE, recursive = TRUE)
files <- lapply(filenames,read.csv)
lapply(files,yourfunction)

【讨论】:

    猜你喜欢
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多