Fast reading and combining several files using data.table (with fread)

前端 未结 2 969
猫巷女王i
猫巷女王i 2020-12-23 16:55

I have several different txt files with the same structure. Now I want to read them into R using fread, and then union them into a bigger dataset.

## First          


        
2条回答
  •  萌比男神i
    2020-12-23 17:44

    Use rbindlist() which is designed to rbind a list of data.table's together...

    mylist <- lapply(all.files, readdata)
    mydata <- rbindlist( mylist )
    

    And as @Roland says, do not set the key in each iteration of your function!

    So in summary, this is best :

    l <- lapply(all.files, fread, sep=",")
    dt <- rbindlist( l )
    setkey( dt , ID, date )
    

提交回复
热议问题