-1

I have 1500 .csv files in a folder, but I am not able to read them directly.

If I open each file, save it manually, I am then able to read it in R.

Hence I need to automate this process of opening each file and saving it in .csv in desired folder? Any help would be appreciated.

fi <- list.files("C:/Users/Desktop/DL/Test", full.names = T)
dat <- lapply(fi, read.csv, row.names = NULL)

The contains of the file are

    version 1.3.0                           
info    team    Ireland                     
info    team    England                     
info    gender  male                        
info    season  2006                        
info    date    6/13/2006                       
info    venue   Civil Service Cricket Club, Stormont                        
info    city    Belfast                     
info    toss_winner England                     
info    toss_decision   bat                     
info    player_of_match ME Trescothick                      
info    umpire  R Dill                      
info    umpire  DB Hair                     
info    match_referee   CH Lloyd                        
info    winner  England                     
info    winner_runs 38                      
ball    1   0.1 England ME Trescothick  EC Joyce    DT Johnston 0   0
ball    1   0.2 England ME Trescothick  EC Joyce    DT Johnston 0   0
ball    1   0.3 England ME Trescothick  EC Joyce    DT Johnston 0   4
Praveen Chougale
  • 355
  • 3
  • 11
  • Please post example contents of a file. Otherwise we are shooting in the dark. – Roman Oct 22 '18 at 18:03
  • version 1.3.0 info team Ireland info team England info gender male info season 2006 info date 6/13/2006 info venue Civil Service Cricket Club, Stormont info city Belfast info toss_winner England info toss_decision bat info player_of_match ME Trescothick info umpire R Dill info umpire DB Hair info match_referee CH Lloyd info winner England info winner_runs 38 ball 1 0.1 England ME Trescothick EC Joyce DT Johnston 0 0 This is what the file contains – Praveen Chougale Oct 22 '18 at 18:09
  • Hey Praveen, [please see here examples of how to post data in an effective manner](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Consider, e.g., posting the output of `dput(df)` below the original question (not in the comments). – Roman Oct 22 '18 at 18:13

1 Answers1

0

I'm assuming you are doing something to the csv's before saving them, because otherwise this would be unproductive and would be better to simply create a script to copy the files.

You can use lapply with write.csv in the following manner:

fi<-list.files("C:/Users/Desktop/DL/Test",full.names=T) 

dat<-lapply(fi, read.csv, row.names = NULL)

lapply(seq_along(dat), function(i) {
    write.csv(dat[i], paste0('address/to/file', i,'.csv'))
  })
automa7
  • 456
  • 4
  • 15