【问题标题】:Create subdirectories inside a directory at once in R在R中一次在目录内创建子目录
【发布时间】:2018-12-05 06:21:13
【问题描述】:

我一直在 SO 中寻找有关如何在 R 中一次在目录中创建两个(或 n 个)子目录的线程,但只找到了 this 这不是我想要的。

我可以通过以下两行来完成:

dir.create(file.path(getwd(), "test"))

sapply(letters[1:2], 
   function(x) dir.create(file.path(getwd(), "test", paste0(x, "_test"))))

#   a    b 
#TRUE TRUE 

如何在一行中完成?

谢谢。

【问题讨论】:

  • 一行是什么意思?你的方法有什么问题?
  • 我的意思是“一行”,没有第一个dir.create
  • 所以要创建子目录而不创建父目录?
  • 对不起,我的评论可能模棱两可。我想创建一个目录test,然后在其中创建两个子目录a_testb_test。但我宁愿一次做,而不是分两步做。

标签: r function directory subdirectory


【解决方案1】:

dir.create(file.path(getwd(), 'test'), recursive = TRUE)

【讨论】:

    【解决方案2】:
    new_folder_path<-paste(getwd(),"new_folder",sep = "/")      
    create_sub_folders<-function(x){
          dir.create(x)
          setwd(x)
          ifelse(dir.exists(x)!=TRUE,print("dir already exists"),dir.create(paste(x,"test_a",sep = "/")))
          ifelse(dir.exists(x)!=TRUE,print("dir already exists"),dir.create(paste(x,"test_b",sep = "/")))}
    

    不确定这是否是您想要的。 创建您想要的new_folder_path,然后将其输入create_sub_folders 函数,它将创建您想要的子文件夹..(对不起,不是一行!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多