0

I have some code that combines a few data files from a folder into one ggplot graph and saves it in a different folder:

setwd("~/data/observation1")

files <- list.files(pattern=".txt")
for (i in files){
  mylist <- lapply(setNames(files, files), read.table, header = T)
  mylist <- lapply(names(mylist), function(i) {cbind(mylist[[i]], ID = i)})
  mydata <- do.call(rbind, mylist)
}

setwd("~/data/graphs")
pdf()
ggplot(mydata, aes(x = place, y = firing_rate, colour = ID)) + geom_point() + geom_path()
dev.off()

I want to apply this to all the other folders within the data folder (observation2, observation3, ... observation200), without having to manually change the setwd each time. Is there anyway to do faster? Maybe another for loop or some application of lapply?

Al G
  • 43
  • 2
  • 7
  • A possible workaround: use `dir([directory name here])` to obtain all the folders in in a string vector. Loop through each folder and apply the function. – Oliver May 01 '19 at 21:06
  • Possible duplicate of [How to import multiple .csv files at once?](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once) – camille May 01 '19 at 22:30
  • @Al G: see this too https://stackoverflow.com/a/48105838/786542 – Tung May 02 '19 at 04:02

0 Answers0