R< - 当数据包含换行符时如何使用data.table的fread和fwrite?

时间:2017-12-14 09:36:39

标签: r csv data.table fwrite fread

我不得不将data.table对象保存到文件列中,该文件列可以包含新行或其他类型的字符。

例如:

x <- data.frame( 
       x = c(1,2,3), 
       y = c("Lorem ipsum dixit", 'the "brown"\nfox', "a|b"),  
       stringsAsFactors = F)

我将其写入文件:

data.table::fwrite(
     dataset,
     "testfile.tsv",
     sep = "\t",
     na = "NULL",
     quote = TRUE,
     eol = "\n",
     append = FALSE,
     col.names = !append,
     row.names = FALSE,
     qmethod = "escape"
)

输出对我来说似乎没问题:

"x" "y"
1   "Lorem ipsum dixit"
2   "the \"brown\"
fox"
3   "a|b"

但是当我用

从文件中读取数据集时
data.table::fread("testfile.tsv")

我收到错误:

Error in data.table::fread("x.gitignore.tsv") : 
Expected sep (' ') but new line, 
  EOF (or other non printing character) ends field 0 
  when detecting types from point 0: fox"

我试图明确地阅读说明引用字符:

data.table::fread("testfile.tsv", 
  sep "\t", header = T, na.strings = "NULL", quote = "\"")

但我仍然得到同样的错误。

我也尝试使用read.delim,但由于某种原因它跳过了第一行数据:

read.delim("testfile.tsv", header = T, sep = "\t", na.strings = "NULL")

那么如何使用data.table和fwrite来编写和读回这些数据帧?

0 个答案:

没有答案