0

I am using R to sum up column "A" in multiple .CSV files, I've prepared code for 1 csv file, is there anyway that i can process all the csv files in one go and output as one?

MyFile <- read.csv(tk_choose.files(caption = "Choose CSV files from directory",),header = TRUE)%>% # Select Input CSV Data
transmute(A)%>%
summarise_all(sum,)%>%
write.csv(file = choose.files()) # Output As CSV File)

here is the output looks like

               A
file1   658839755

What i want is

            A
file1   658839755
file2   1541654313
file3   4643213843

Is it possible to make it happen?

Thanks guys

zx8754
  • 52,746
  • 12
  • 114
  • 209
Bomber Gay
  • 39
  • 4
  • Do you have problems opening the files, merging them to one dataframe or to save them? –  Jul 31 '19 at 08:54
  • Use loops? Something like `write.table(rbindlist(lapply(list.files, function(i){read stuff, do stuff, output})), "final_all_results.txt")` – zx8754 Jul 31 '19 at 08:55
  • Related https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once – zx8754 Jul 31 '19 at 08:56
  • Hi guys, I forget to mention, i cant really merging them into one dataframe. They all give me unique output. what i need is process those files in one go and combine the output into one file. – Bomber Gay Jul 31 '19 at 09:10
  • This is exactly what loops do, see my example code, loop through files, read them summarise, and finally rowbind them into one datframe which then written out to one file. – zx8754 Jul 31 '19 at 09:14
  • Other option would be to use `for loop`, and `write.table` with option of `append = TRUE`. – zx8754 Jul 31 '19 at 09:14
  • 1
    @zx8754 thanks mate, I'm sorry I am a newbie here, need time for finger it out how to loop them... – Bomber Gay Jul 31 '19 at 09:19
  • @zx8754 Hi mate, i am wondering after i list those files, how should i define them as df? – Bomber Gay Jul 31 '19 at 09:33
  • See above related post on how to read multiple files. – zx8754 Jul 31 '19 at 09:34

1 Answers1

0

This may help you

csv_path <-"C:/csvfiles/"# set your path to csv files
    data <- readbulk::read_bulk(directory = csv_path,subdirectories = F,extension = ".csv",data = NULL) #read all files into a data frame

    data %>% 
      group_by(File) %>% #group by file name
      summarise(my.sum=sum(A)) %>% # summarise column A using sum
      write.csv(.,"C:/....") # write to csv if you want
e.matt
  • 836
  • 1
  • 5
  • 12