problem with specifying colClasses in read.csv in R

后端 未结 2 969
南笙
南笙 2021-01-07 09:42

I am trying to specify colClasses in read.csv in an attempt to speed up the reading of csv file. However, I encounter the following problem:

assuming that i have a f

相关标签:
2条回答
  • 2021-01-07 10:27

    Your second column is not numeric as it is quoted -- that makes it text.

    So read it as text, then call as.numeric(...) on the column. Or alter the file.

    0 讨论(0)
  • 2021-01-07 10:46

    Further to Dirk,

    You can simply drop the colClasses argument and the file will read in fine.

    data <- read.csv('t.csv' , stringsAsFactors=FALSE, check.names=FALSE , comment.char='')
    str(data)
    

    Gives:

    > str(data)
    'data.frame':   1 obs. of  2 variables:
     $ a: chr "x"
     $ b: int 0
    > class(data$b)
    [1] "integer"
    

    You should be able to do everything you want with that second column now.

    GL

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