0

I have a large number of files, each with a name format of "ARU1_20100706_6_164443000.csv". The part which I am interested in is the frequency identifier "164443000". Some of the files in this folder have the same frequency identifier; for each identifier, I would like to create a new folder and place all files with this identifier in this folder together.

So far I had this:

csvfiles <- list.files("C:\\Users\\name\\Documents\\CSV Files\\ARU1", pattern="*.csv", full.names=FALSE)

csv <- data.frame(csvfiles)

for (i in 1:length(csv)) {
  csv$freq <- str_sub(csvfiles, start = 18, end = 25)     
}

I am a complete R newbie and am struggling so thanks in advance for the help.

StrikeR
  • 1,598
  • 5
  • 18
  • 35
user3021648
  • 67
  • 2
  • 3
  • 11
  • You already have the basic stuff. Use "dir.create" to create directory (Check : http://stackoverflow.com/questions/4216753/check-existence-of-directory-and-create-if-doesnt-exist) and use "file.rename" for moving the file (Check : http://stackoverflow.com/questions/10266963/moving-files-between-folders) – rags Nov 22 '13 at 12:34
  • 1
    I suggest that you change the title of your question here. It is rather a grouping or aggregation not sorting. – agstudy Nov 22 '13 at 13:24

2 Answers2

0

You already have the list of csv files. Next I would extract the identifier's:

ids = substr(csvfiles, 17, 24)

under the assumption that the csv files always have the exact same length, i.e. the ID is always to the same place in the csv name.

Then you need to create the set of directories:

no_return_values = sapply(ids, dir.create)

and copy the files:

no_return_values = mapply(file.copy, csvfiles, ids)

I have not tested the code, but these are the basic commands and steps you need to take. Study the documentation of the functions I use to learn what they do.

You need to read the *apply family of functions as: for each element in the list, perform this function. The *apply functions have no return values as they are only used for their side-effect, i.e. create a directory and copy a file.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
0
  • I would use by for this task since it is a grouping task. You sh
  • file.copy To copy files. It is vectorized so no need to use a loop
  • use file.path to create the paths independently of the OS used.

Something like this :

dat <- data.frame(files = csvfiles)
dat$id <- gsub('.*_(.*)[.]csv','\\1',dat$files)
by(dat,dat$id,function(x){
 dir_path <- file.path(getwd(),unique(x$id)) ## replace getwd by any path
 dir.create(dir_path)
 file.copy(dat$files,dir_path)
})
agstudy
  • 119,832
  • 17
  • 199
  • 261