【问题标题】:Issue with double quotes and fread function双引号和 fread 函数的问题
【发布时间】:2015-07-17 14:35:13
【问题描述】:

我有一些如下所示的列条目:

c("This is just a "shame"...") # since its a character

这将在您的 C:\ 驱动器上写入文件:

sample.data <- data.frame(case1=c("This is just a 'shame'..."), 
                          case2="This is just a shame") # here I could not make it to insert the double quotes 
write.csv(sample.data, file="C:/sample_data.csv")

require(data.table)
test.fread <- fread("C:/sample_data.csv")
test.read.csv <- read.csv("C:/sample_data.csv")

如果我使用fread 函数(来自data.table)读取csv 数据,我会得到他的错误:

Bumped column 79 to type character on data row 12681, field contains '   
a.n."'. Coercing previously read values in this column from logical, 
integer or numeric back to character which may not be lossless; e.g., if 
'00' and '000' occurred before they will now be just '0', and there 
may be inconsistencies with treatment of ',,' and ',NA,' too (if they 
occurred in this column before the bump). If this matters please rerun 
and set 'colClasses' to 'character' for this column. Please note that column
type detection uses the first 5 rows, the middle 5 rows and the 
last 5 rows, so hopefully this message should be very rare. 
If reporting to datatable-help, please rerun and include 
the output from verbose=TRUE.

如果我使用read.csv 不会发生错误并且条目被正确读取!

问题 1:如何删除 character 名称中的双引号。

问题 2:为什么 read.csv 可以正确读取条目但 fread 失败?

【问题讨论】:

  • 谢谢,但我正在读取的数据中有随机名称和多个条目,所以不能只是逃避条目。
  • 一般在 R " 中引用单引号,反之亦然。
  • here
  • @Michael:谢谢,它似乎解释了一切。不过,我必须检查我的数据。
  • @Maximilian,您可以自己回答并接受它,以便对可能遇到此问题的其他人有用..(直到 1.9.5 作为 1.9.6 推送到 CRAN)。

标签: r data.table


【解决方案1】:

正如@Arun 所建议的那样,目前在 github 上的data.table 开发版本 1.9.5 在这里可能会有所帮助。

要安装,请按照以下步骤操作(需要 Rtools):

# To install development version

library(devtools)
install_github("Rdatatable/data.table", build_vignettes = FALSE)

已经过测试,所以这是为了确认最新版本的data.table 可以毫无问题地解决双引号问题。

欲了解更多详情和更新,请查看以下链接github data.table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 2018-10-25
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多