带有反斜杠的fread和column

时间:2014-06-23 22:52:00

标签: r data.table

我有一个问题,fread()使用“\”作为目录分隔符读取一列目录路径。问题是尾随目录分隔符在fread()中抛出错误。

对于以下示例csv文件,

file,size
"windows\user",123

fread()和read.csv()都同意并且都将\转换为\\

> fread("example.csv")
            file size
1: windows\\user  123

但是,对于以下示例,fread()在read.csv()没问题时给出错误。

file,size
"windows\user\",123

read.csv()给出了

> read.csv("example.csv")
             file size
1 windows\\user\\  123

虽然fread()错误看起来像这样

> fread("example.csv",verbose=TRUE)
Input contains no \n. Taking this to be a filename to open
File opened, filesize is 0.000 GB
File is opened and mapped ok
Detected eol as \r\n (CRLF) in that order, the Windows standard.
Using line 2 to detect sep (the last non blank line in the first 'autostart') ... sep=','
Found 2 columns
First row with 2 fields occurs on line 1 (either column names or first row of data)
All the fields on line 1 are character fields. Treating as the column names.
Count of eol after first data row: 2
Subtracted 1 for last eol and any trailing empty lines, leaving 1 data rows
Error in fread("example.csv", verbose = TRUE) : 
' ends field 1 on line 1 when detecting types: "windows\user\",123

我真的想避免做

DT = data.table(read.csv("example.csv"))

如果可能的话。

1 个答案:

答案 0 :(得分:5)

现已在GitHub的v1.9.3中修复。

  
      
  • fread()现在接受引用字段中的尾随反斜杠。感谢user2970844突出显示。
  •   
$ cat example.csv
file,size
"windows\user\",123

> require(data.table)
> fread("example.csv")
              file size
1: windows\\user\\  123
> read.csv("example.csv")
             file size
1 windows\\user\\  123
>