fread unable to read .csv files with first column empty

后端 未结 3 1578
梦谈多话
梦谈多话 2021-01-02 01:17

Say I have the first test.csv that looks like this

,a,b,c,d,e

If I try to read it using read.csv, it works fine.<

相关标签:
3条回答
  • 2021-01-02 01:58

    I think you could use skip/select/drop attributes of the fread function for this purpose.

    fread("myfile.csv",sep=",",header=FALSE,skip="A")#to just skip the 1st column
    fread("myfile.csv",sep=",",header=FALSE,select=c(2,3,4,5)) # to read other columns except 1
    fread("myfile.csv",sep=",",header=FALSE,drop="A") #to drop first column
    
    0 讨论(0)
  • 2021-01-02 02:07

    I've tried making that csv file and running the code. It seems to work now - same for other people? I thought it might be an issue with not having a new line at the end (hence the warning from read.csv), but fread copes fine whether there's an new line at the end or not.

    0 讨论(0)
  • 2021-01-02 02:17

    As for me, my problem was only that the first ? rows of my file had a missing ID value.

    So I was able to solve the problem by specifying autostart to be sufficiently far into the file that a nonmissing value popped up:

    fread("test.csv", autostart = 100L, skip = "A")
    

    This guarantees that when fread attempts to automatically identify sep and sep2, it does so at a well-formatted place in the file.

    Specifying skip also makes sure fread finds the correct row in which to base the names of the columns.

    If indeed there are no nonmissing values for the first field, you're better off just deleting that field from the .csv with Richard Scriven's approach or a find-and-replace in your favorite text editor.

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