1

I have multiple csv files in different folders i.e. Main folder contains week 1 and week 2 folder. Week1 in turns contains file1.csv and week2 contains file2.csv. All files have same column name. There are 100's of such files in different directories

file1 <- data.frame(name = c("Bill","Tom"),Age = c(23,45))
file2 <- data.frame(name = c("Harry","John"),Age = c(34,56)) 

How can I load them and do a rbind in r and get them in a final data frame

I got some clue here: How can I read multiple files from multiple directories into R for processing?

what I did is slight modification to the function to do row bind as follows but nowhere near to what I want

 # Creating a function to process each file
 empty_df <- data.frame()
 processFile <- function(f) {
 df <- read.csv(f)
 rbind(empty_df,df)
 }

# Find all .csv files
files <- dir("/foo/bar/", recursive=TRUE, full.names=TRUE, pattern="\\.csv$")

# Apply the function to all files.
result <- sapply(files, processFile)

Any help is greatly appreciated!

joy_1379
  • 487
  • 3
  • 17

1 Answers1

0

I'd have tried to do something with a for loop on my side such as

temp = read.csv('week1/file1.csv')

for(i in 2:n){ #n being the number of weeks you have
  temp= rbind(temp, read.csv(paste('week',i,'/file',i,'.csv', sep='')))
  }

I hope it helped

elielink
  • 1,174
  • 1
  • 10
  • 22