3

I've got some basic code for reading in multiple files from a folder that share the same file format. The text files themselves each contain one or two sentences; no columns, headers or anything like that.

I initally used read.table to do this but after doing some reading round and in particular looking at this recent post I thought I'd see if I could use fread to make things a little bit quicker.

library(data.table)

files <- list.files(path = "C:/Documents", pattern = "*.txt")

readdata <- function(x)
{
    mydata <- fread(x, sep=" ")
    return(mydata)
}

all.files <- lapply(files, readdata)
final.data <- rbindlist(all.files)

When I run this code, I get the following error:

Error in fread(x, sep = " ") : File not found: 1.txt

1.txt is the first file in the folder. Can anyone figure out why this? The path folder is correct (I changed the one above to something generic to put on here) so I can't seem to see what the problem is.

Community
  • 1
  • 1
user1988898
  • 185
  • 2
  • 9

1 Answers1

5

You have to specify the path to the file:

path <- "C:/Documents"

readdata <- function(x)
{
  f <- file.path(path, x)
  mydata <- fread(f, sep=" ")
  return(mydata)
}

Another option is to use list.files with the argument full.names = TRUE. This returns the filenames together with their path.

files <- list.files(path = "C:/Documents", pattern = "*.txt", full.names = TRUE)

readdata <- function(x)
{
    mydata <- fread(x, sep=" ")
    return(mydata)
}
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • @agstudy Agreed. I tried to keep the number of changes to the original code as low as possible. – Sven Hohenstein Jan 16 '14 at 12:28
  • thank you for replying. I'll try what you've written and see what works. – user1988898 Jan 16 '14 at 12:32
  • ok so on the one hand what you've written does work (thank so much - it's silly how long i can stare at something and yet not seem to get anywhere). my only problem now is that i'm being thrown the following error: The supplied 'sep' was not found on line 15. To read the file as a single character column set sep='\n'. So I do that (i thought the separation should be whitespace?) and then i get this error: mmap'd region has EOF at the end In addition: There were 50 or more warnings (use warnings() to see the first 50). thank you for your help - i will try & solve this one myself. – user1988898 Jan 16 '14 at 12:39
  • @SvenHohenstein it is better, just no need to set the `sep` for `fread`, generally it tries to guess the best one. – agstudy Jan 16 '14 at 12:42