save files into a specific subfolder in a loop in R

只谈情不闲聊 提交于 2021-01-29 03:07:06

问题


I feel I am very close to the solution but at the moment i cant figure out how to get there.

I´ve got the following problem. In my folder "Test" I´ve got stacked datafiles with the names M1_1; M1_2, M1_3 and so on: /Test/M1_1.dat for example. No I want to seperate the files, so that I get: M1_1[1].dat, M1_1[2].dat, M1_1[3].dat and so on. These files I´d like to save in specific subfolders: Test/M1/M1_1[1]; Test/M1/M1_1[2] and so on, and Test/M2/M1_2[1], Test/M2/M1_2[2] and so on.

Now I already created the subfolders. And I got the following command to split up the files so that i get M1_1.dat[1] and so on:

for (e in dir(path = "Test/", pattern = ".dat", full.names=TRUE, recursive=TRUE)){
  data <- read.table(e, header=TRUE)
  df <- data[ -c(2) ]
  out <- split(df , f = df$.imp)
    lapply(names(out),function(z){
    write.table(out[[z]], paste0(e, "[",z,"].dat"),
                sep="\t", row.names=FALSE, col.names = FALSE)})
}

Now the paste0 command gets me my desired split up data (although its M1_1.dat[1] instead of M1_1[1].dat), but i cant figure out how to get this data into my subfolders.

Maybe you´ve got an idea?

Thanks in advance.


回答1:


I don't have any idea what your data looks like so I am going to attempt to recreate the scenario with the gender datasets available at baby names

Assuming all the files from the zip folder are stored to "inst/data"

store all file paths to all_fi variable

all_fi <- list.files("inst/data", 
                         full.names = TRUE, 
                         recursive = TRUE, 
                         pattern = "\\.txt$")

    > head(all_fi, 3)
    [1] "inst/data/yob1880.txt" "inst/data/yob1881.txt"

Preset function that will apply to each file in the directory

f.it <- function(f_in = NULL){
# Create the new folder based on the existing basename of the input file
   new_folder <- file_path_sans_ext(f_in)
   dir.create(new_folder)

    data.table::fread(f_in) %>% 
    select(name = 1, gender = 2, freq = 3) %>% 
    mutate(
     gender = ifelse(grepl("F", gender), "female","male")
    ) %>% (function(x){

     # Dataset contains names for males and females
     # so that's what I'm using to mimic your split
     out <- split(x, x$gender)
      o <- rbind.pages(
             lapply(names(out), function(i){
             # New filename for each iteration of the split dataframes

             ###### THIS IS WHERE YOU NEED TO TWEAK FOR YOUR NEEDS
             new_dest_file <- sprintf("%s/%s.txt", new_folder, i)
             # Write the sub-data-frame to the new file
             data.table::fwrite(out[[i]], new_dest_file)
             # For our purposes return a dataframe with file info on the new
             # files...

              data.frame(
                file_name = new_dest_file,
                file_size = file.size(new_dest_file), 
                stringsAsFactors = FALSE)
            })
           )
        o
    })
}

Now we can just loop through:

NOTE: for my purposes I'm not going to spend time looping through each file, for your purposes this would apply to each of your initial files, or in my case all_fi rather than all_fi[2:5].

> rbind.pages(lapply(all_fi[2:5], f.it))

============================  =========
file_name                     file_size
============================  =========
inst/data/yob1881/female.txt      16476
inst/data/yob1881/male.txt        15306
inst/data/yob1882/female.txt      18109
inst/data/yob1882/male.txt        16923
inst/data/yob1883/female.txt      18537
inst/data/yob1883/male.txt        15861
inst/data/yob1884/female.txt      20641
inst/data/yob1884/male.txt        17300
============================  =========


来源:https://stackoverflow.com/questions/42367526/save-files-into-a-specific-subfolder-in-a-loop-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!