1

I am using R. I have a series of txt files that all all ; seperated. I want to combined those files into 1 data frame. Each individual file has the variable names in the top row.

I found a useful post for doing this online. How to import multiple .csv files at once?. I am using rbindlist to do this, though I am open to suggestions if there is an easier way to import this data.

Here is my code.

# create list of files
temp <- NULL
temp <-  as.list (list.files ("F:/Desktop/data"))
temp

# now rbind the list using rbindlist
data <- NULL
data <- rbindlist(temp, fill=FALSE, idcol=NULL)

is.list (temp)
typeof (temp)

When I run this code, I get an error

Error in rbindlist(temp, fill = FALSE, idcol = NULL) : 
  Item 1 of input is not a data.frame, data.table or list

However, when I check temp using is.list and typeof, it shows up as a list. I am not sure why rbindlist isn't reading the list as a list. Is rbindlist the best way to import this data.

r2evans
  • 141,215
  • 6
  • 77
  • 149

1 Answers1

5

The list.files is just giving the file names. We need to read those files. As we are using rbindlist from data.table, read with fread and then use the rbindlist

library(data.table)
temp <- list.files ("F:/Desktop/data", full.names = TRUE, pattern = "\\.txt$")
out <- rbindlist(lapply(temp, fread), fill = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662