-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
  • 373
  • 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
  • 494
  • 4
  • 15
  • I am getting this error Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names – Praveen Chougale Oct 22 '18 at 17:56
  • Try using `dat[i]` instead of `dat[[i]]` – automa7 Oct 22 '18 at 17:58
  • I am not doing anything before saving.As I am facing issue when I directly import the files ,whereas when open each file and do save as it works fine.Thats why I need to open each file and save it with same name – Praveen Chougale Oct 22 '18 at 18:00
  • are you being able to read the files? can you show me the output of the dat list? – automa7 Oct 22 '18 at 18:04
  • I am not able to read the file ,I am getting the below error when I run this code fi<-list.files("C:/Users/Desktop/DL/odi_csv_male",full.names=T) dat<-lapply(fi, read.csv, row.names = NULL) Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names – Praveen Chougale Oct 22 '18 at 18:05
  • can you show a line of the csv? What is the separator, is it a comma? – automa7 Oct 22 '18 at 18:10
  • Microsoft Excel Comma Separated Values File (.csv),yes it is – Praveen Chougale Oct 22 '18 at 18:12
  • It will be hard to help you without seeing your code and the csv file, i'm sorry. You can try skiping the headings first to see if it will read, use the following line: `dat<-lapply(fi, read.csv, skip=1, row.names = NULL)` – automa7 Oct 22 '18 at 18:17
  • I have edited the question and shared the contains of the file – Praveen Chougale Oct 22 '18 at 18:19
  • I saw it, you need to skip the first line, it is not following the standard. see my code above, it may help you. – automa7 Oct 22 '18 at 18:20