简体   繁体   English

如何将工作目录设置为子文件夹?

[英]How can I set the working directory to a subfolder?

I am using R to try to read all the.xlsx files in a subfolder within a main folder.我正在使用 R 尝试读取主文件夹中子文件夹中的所有 .xlsx 文件。 The code seems intuitive, but I am stumbling into a roadblock with the working directory.代码看起来很直观,但我在工作目录中遇到了障碍。

My relevant code:我的相关代码:

setwd("~/Downloads/Job Postings")

for (dir in list.dirs()[-1]) {
  setwd(dir)

  files <- list.files(pattern="*.xlsx")

  require(purrr)
  main_dF <- files %>% map_dfr(read.xlsx)
}

The code seems intuitive, but I receive error Error in setwd(dir): cannot change working directory .代码看起来很直观,但我收到错误Error in setwd(dir): cannot change working directory How can I adjust the setwd() command?如何调整setwd()命令? Thanks谢谢

I think you have two issues.我认为你有两个问题。

  1. You change directory into a sub directory in the loop, but never return您将目录更改为循环中的子目录,但永远不会返回
  2. You assign the result to main_df , but that won't ever be accumulated across the subdirectories您将结果分配给main_df ,但不会在子目录中累积

You might try something list this.您可以尝试列出此内容。

setwd("~/Downloads/Job Postings")
results <- list()
for (dir in list.dirs()[-1]) {
  setwd(dir)

  files <- list.files(pattern="*.xlsx")
  require(purrr)
  main_dF <- files %>% map_dfr(read.xlsx)
  results[[dir]] <- main_df
  setwd("~/Downloads/Job Postings")
}
finalresult <- bind_rows(results)

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

相关问题 如何找到目录并在R中自动设置工作目录路径? - How can I locate a directory and automatically set the working directory path to it in R? 如何在不更改R中的工作目录的情况下访问子文件夹中的指定文件? - How to access to specify file in subfolder without change working directory In R? 如何通过函数设置目录? - How can I set a directory through a function? 如何设置“ path.expand”从我的工作目录开始? - How can I set `path.expand` to begin at my working directory? 如何在R中将当前目录设置为其他目录? - How can I set different directory to the current directory in R? 将工作目录设置为默认WD中的子文件夹 - Setting Working Directory as subfolder in default WD 在工作目录中为R生成子文件夹的路径字符串 - Generate path string of subfolder in the working directory for R 如何将函数应用于特定目录中的一组.csv文件? - How can I apply a function to a set of .csv files in a particular directory? 如何将工作目录快速设置为 RStudio 中文件的位置? - How can you set the working directory quickly to a file's location in RStudio? 这个 for 循环中的“i”/类别应该是什么,我如何确保它在我的工作目录中? - What should the “i”/category be in this for loop and how can I ensure it is in my working directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM