How to read every .csv file in R and export them into single large file

前端 未结 1 1739
感情败类
感情败类 2021-01-17 02:33

Hi so I have a data in the following format

101,20130826T155649
------------------------------------------------------------------------
3,1,round-0,10552,18         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 02:52

    You can use lapply to read all the files, clean and format them, and store the resulting data frames in a list. Then use do.call to combine all of the data frames into single large data frame.

    # Get vector of files names to read
    files.to.load = list.files(pattern="csv$")
    
    # Read the files
    df.list = lapply(files.to.load, function(file) {
       df = read.table(file, sep = ',', fill=TRUE, col.names=paste("V", 1:18,sep=""))
       ... # Cleaning and formatting code goes here
       df$file.name = file  # In case you need to know which file each row came from
       return(df)
    })
    
    # Combine into a single data frame
    df.combined = do.call(rbind, df.list)
    

    0 讨论(0)
提交回复
热议问题