Warning message: In file(file, "rt") [duplicate]
Asked Answered
S

2

9

I am trying to import CSV files to graph for a project. I'm using R 2.15.2 on a Mac OS X.

  • The first way tried

    The script I'm trying to run to import the CSV file is this:

    group4 <- read.csv("XXXX.csv", header=T)
    

    But I keep getting this error message:

    Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
      object 'XXXXXX.csv' not found 
    
  • The second way tried

    I tried moving my working directory but got another error saying I can't move my working directory. So I went into Preferences tab and changed the working directory to the file that has my CSV files. But I still get the same error(as the first way).

  • The third way tried

    Then I tried this script:

    group4 <- read.table(file.choose(), sep="\t", header=T)
    

    And I get this error:

    Warning message: 
    In read.table(file.choose(), sep = "\t", header = T) :
      incomplete final line found by readTableHeader on '/Users/xxxxxx/Documents/Programming/R/xxxxxx/xxxxxx.csv' 
    

I've searched on the R site and all over the Internet, and nothing has got me to the point where I can import this simple CSV file into the R console.

Spaceless answered 17/1, 2013 at 10:25 Comment(3)
What does group4 look like? Does look like what you want, in spite of the warning? Check the end of it with tail(group4). If it looks good, you can just wrap the call to read.table with suppressWarnings and be on your way.Lauraine
This is also the warning you'd get if you tried to read an Excel file in that hasn't been properly converted to csv. Maybe someone just changed the extension and thought that was enough?Lauraine
From your responses to the comments and answers so far, I get the feeling you're not really familiar with software. You might benefit from reading some introductory books to understand what directories are, what files are (as opposed to what they look like when opened in Excel), etc.Pemmican
W
4

As to the missing EOF (i.e. last line in file is corrupted)... Usually, a data file should end with an empty line. Perhaps check your file if that is the case. As an alternative, I would suggest to try out readLines(). This function reads each line of your data file into a vector. If you know the format of your input, i.e. the number of columns in the table, you could do this...

number.of.columns <- 5 # the number of columns in your data file
delimiter <- "\t" # this is what separates the values in your data file
lines <- readLines("path/to/your/file.csv", -1L)
values <- unlist(lapply(lines, strsplit, delimiter, fixed=TRUE))
data <- matrix(values, byrow=TRUE, ncol=number.of.columns)
Whetstone answered 19/3, 2013 at 14:13 Comment(1)
I actually shocked with the solution when I saw this question, and thought the warning message is misleading.Crawl
M
5
  1. The file is not in your working directory, change it, or use an absolute path.
  2. Than you are pointing to a non-existing directory, or you do not have write privileges there.
  3. The last line of your file is malformed.
Merc answered 17/1, 2013 at 10:30 Comment(1)
Hi thanks for your answer. I used an absolute path and the cursor just gives me a new line, I don't know if it's loading. I added an EOL to the last line of my file.Spaceless
W
4

As to the missing EOF (i.e. last line in file is corrupted)... Usually, a data file should end with an empty line. Perhaps check your file if that is the case. As an alternative, I would suggest to try out readLines(). This function reads each line of your data file into a vector. If you know the format of your input, i.e. the number of columns in the table, you could do this...

number.of.columns <- 5 # the number of columns in your data file
delimiter <- "\t" # this is what separates the values in your data file
lines <- readLines("path/to/your/file.csv", -1L)
values <- unlist(lapply(lines, strsplit, delimiter, fixed=TRUE))
data <- matrix(values, byrow=TRUE, ncol=number.of.columns)
Whetstone answered 19/3, 2013 at 14:13 Comment(1)
I actually shocked with the solution when I saw this question, and thought the warning message is misleading.Crawl

© 2022 - 2024 — McMap. All rights reserved.