Ati
Ati

Reputation: 55

how to create some folders with specific names in R?

I have a list of character names! I want to create some folders with each names of these character in a specific drive and put each result in its folder?!

My list of character names is:

met <- c( 'aaa', 'bbb',  'abcd', 'efg', 'tszck')

I calculated a function for them and then I want to put the results of each of them in a folder whose name is similar to the names of this character!

for (i in seq_along(met)) {
     my_formula <-  paste0(met[i],"~+pc1+pc2+pc3")
     prep <- prepscores(Z=metGT,formula=my_formula,  ...)
     save(prep,file=paste0("prep",met[i],".RData"),compress="bzip2")
     }

Before save the results,I want to create 5 folders in D drive with these names of met values and then save each result in its folder?!

My output will be:

D://aaa/prepaaa
D://bbb/prepbbb
D://abcd/prepabcd
D://efg/prepefg
D://tsczk/preptsczk

Upvotes: 0

Views: 219

Answers (1)

Parfait
Parfait

Reputation: 107577

Simply use dir.create() in your loop:

for (i in seq_along(met)){
  my_formula<- paste0(met[i],"~+pc1+pc2+pc3")

  prep<- prepscores(Z=metGT,formula=my_formula, ...)

  outdir <- paste0("D://", met[i])  # DECLARE NEW FOLDER
  dir.create(outdir, showWarnings = FALSE)    # CREATE NEW FOLDER

  setwd(outdir)  # SET WORKING DIRECTORY TO NEW FOLDER    
  save(prep,file=paste0("prep",met[i],".RData"),
       compress="bzip2")   # OUTPUTTED FILE SAVED TO NEW FOLDER
}

Upvotes: 1

Related Questions