【问题标题】:R: use read.table to load data with doublequotesR:使用 read.table 加载带双引号的数据
【发布时间】:2017-09-19 21:05:21
【问题描述】:

我的 csv 版本数据是这样的:

name,words,name
John, "He says:"I love it!"", 18

起初,我尝试用

加载数据
data <- read.table("data.csv",header = T,sep = ',',quote = "",stringsAsFactors = FALSE)

错误是:

Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : 
  line 1 did not have 3 elements

嗯,我可以理解,因为 R 会使用很多双引号。

我修复了它

data <- read.table("data.csv",header = T,sep = ',',quote = "\"",,stringsAsFactors = FALSE) #change the name of the output file 

但是,我不知道为什么会这样,R 怎么知道他应该停在哪个双引号?

【问题讨论】:

  • 试试read.csv("data.csv")
  • @RichScriven 我只是好奇这种行为

标签: r csv


【解决方案1】:

嗯,这是一种有趣的数据格式——而且是有趣的行为。帮助页面显示“查看对引号中嵌入引号的行为的扫描”,但我在该帮助页面中没有看到任何有用的信息,因此我尝试了一些方法。

我相信 quote 参数所做的是告诉 R 忽略引号之间出现的任何 sep 元素,并删除任何 quote 元素(因为这意味着仅用于分隔列,而不是作为数据)。因此,这仅适用于您,因为您在 words 列中的第二个引号之后没有任何逗号。

这里有四个例子。

引号中没有逗号(您的示例)

name,words,name
John, "He says:"I love it!"", 18

有趣的是,这个例子在你的代码的两个版本中都适用于我。第一个保留所有引号,第二个删除它们。

read.table("data.csv", header = TRUE, sep = ',', quote = "", stringsAsFactors = FALSE)
##   name                   words name.1
## 1 John  "He says:"I love it!""     18
read.table("data.csv", header = TRUE, sep = ',', quote = "\"", stringsAsFactors = FALSE)
##   name               words name.1
## 1 John  He says:I love it!     18

第一个引号后的逗号

name,words,name
John, "He says, "I love it!"", 18

这里的第一个版本 (quote="") 根据逗号将行分成四列,而不是三列,并使用额外的列作为行名。第二个版本忽略了添加的逗号,但也删除了实际引号周围的引号。

read.table("text.csv", header = TRUE, sep = ',', quote = "", stringsAsFactors = FALSE)
##           name          words name.1
## John  "He says  "I love it!""     18
read.table("text.csv", header = TRUE, sep = ',', quote = "\"", stringsAsFactors = FALSE)
##   name                words name.1
## 1 John  He says, I love it!     18

第二个引号后的逗号

name,words,name
John, "He says: "I love it, do you?"", 18

这两个版本的作用几乎相同(四列),因为逗号不在成对的引号之间。第一个保留引号,第二个不保留。

read.table("text.csv", header = TRUE, sep = ',', quote = "", stringsAsFactors = FALSE)
##                       name      words name.1
## John  "He says: "I love it  do you?""     18
read.table("text.csv", header = TRUE, sep = ',', quote = "\"", stringsAsFactors = FALSE)
##                     name    words name.1
## John  He says: I love it  do you?     18

两个引号之间的逗号

name,words,name
John, "He says, "I love it, do you?"", 18

这里第一个不起作用,因为它在第一行找到三个列名但有五个列。第二个跳过第一个逗号,但不跳过第二个,因此再次将其分成四列,并使用额外的作为行名。

read.table("text.csv", header = TRUE, sep = ',', quote = "", stringsAsFactors = FALSE)
## Error in read.table("text.csv", header = TRUE, sep = ",", quote = "",  : 
##  more columns than column names
read.table("text.csv", header = TRUE, sep = ',', quote = "\"", stringsAsFactors = FALSE)
##                     name    words name.1
## John  He says, I love it  do you?     18

最后,所有这些例子都只有一行;如果你有不止一行并且它们解析成不同数量的列,你会得到一个与你得到的错误类似的错误,除了第一行的列数不同。

您的错误让我感到惊讶的是,它发生在第 1 行;如果 R 认为您在该行中的列少于三列(它在标题行中找到的数字),您会收到此错误,但在我的系统上,无论如何,它会在该行中找到三个元素。

【讨论】:

  • 所以我可以这样理解吗:在第二种情况下,R 将中和第一个双引号和第二个双引号之间的所有 sep 和 \n,然后像往常一样解析每一行。
  • +1 - 逗号和第一个引号之间的空格也很有趣。不同的解析器可能会以不同的方式处理它。 PS:示例中“第一个引号后的逗号”没有多余的逗号,应该是“他说”?
  • @Lihaonan,是的,我相信。
猜你喜欢
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多