r - "Incorrect number of dimensions"错误,帮我理解为什么

标签 r dimensions

这个问题的组织:

I.   Background
II.  The Problem/Question
III. Steps Taken to Make this Question Good
IV.  Update: the output of head(x.path) and dput(x.path)

一、背景

我正在定制/改编 O'Reilly 书籍“黑客的机器学习”(第 3 章)中的电子邮件分类代码。可以在此处找到该代码及其随附的数据:https://github.com/johnmyleswhite/ML_for_Hackers/tree/master/03-Classification

二、问题/疑问

该代码中的主要功能之一称为 get.msg() .原来的功能是
get.msg <- function(path)
{
  con <- file(path, open = "rt", encoding = "latin1")
  text <- readLines(con)
  # The message always begins after the first full line break
  msg <- text[seq(which(text == "")[1] + 1, length(text), 1)]
  close(con)
  return(paste(msg, collapse = "\n"))
}

不过,我的数据在很多方面都不同,因此我必须对其进行大量编辑。我的数据较早从关系数据库中读入,因此我不必读入和清理文本文件。相反,我的电子邮件正文数据是数据框的第 18 列,我们可以将其称为 x .这是我的 get.msg() 版本:
get.msg <- function(path) {
  bodyvector <- path[!(is.na(path[,18]) | path[,18]==""), ]
  return(paste(bodyvector))
}

原来我简称为x$email这适用于大部分代码,但是在后面的步骤中 get.msg()函数用于 x.path ,其中 x.path指向x并在另一个函数中与 paste() 结合使用函数,根据示例代码的作者:
 z.spam <- sapply(spam.docs, function(p) count.word(paste(x.path,p,sep = ""),         "keyword"))

在这里,count.word() function 是一个包含 get.msg() 的函数.所以,paste()函数导致问题,因为它导致 x.path显然被认为是原子数组,并给出了 $ 无法与原子数组一起使用的错误。根据较早的 StackOverflow 问答,我将引用该列的方式更改为 path[,18] (被评估为 x.path[,18],因此与 x[,18] 相同)。

然后我做了一些检查以确保 x.path[,18]具有与 x.path$email 相同的信息,它做到了。但是,当我尝试运行代码时,我在 get.msg(x.path) 上收到一条错误消息。 ,即:
Error in path[,18] : incorrect number of dimensions.

我试过 path[,'email'] ,然后 path[18,]然后只是 path本身和所有三个都导致了同样的错误。我试过 path[[1]][[18]]这给了我一个下标越界错误。

有什么想法吗?

三、使这个问题好起来的步骤

为了避免惹恼任何人并获得任何反对票,我确认该主题与 StackOverflow 相关,并且我认为它可能与将来处理此或类似编程问题的其他人相关。我还花了将近一个小时在线研究这个问题,并尝试在 R 中解决它。

有很多关于此错误消息的引用,但是原因似乎非常多样化且完全不相关(例如网络故障等)。最后,我花了大量时间编辑这个问题,以使其可读且格式正确(我希望我做得很好,我知道它有很多信息)。

四、 head()的输出和 dput()

你们中的一些非常有帮助的人要求查看 head(x.path) 的输出或 dput(x.path) .我不介意,只是它是公司的 secret 电子邮件数据,如果我发布它,我将失业并被起诉。 ;-)

我把它贴在这里并用假信息替换了真实信息。我希望这没问题。我尝试使用 dput()起初,如果您愿意,我可以这样做,但这确实是大量数据。这是head(x.path) :
> head(x.path)
[1] "c(\"Z12e3317e4b1jZbbajZ9Zdd6\", \"Z12e3317e4b1jZbbajZ99124\", \"Z12e331Ze4b1jZbbajZ996dd\", \"Z12e3319e4b1jZbbajZ9acb6\", \"Z12e3319e4b1jZbbajZ9ad3b\", \"Z12e3319e4b1jZbbajZ9adjd\", \"Z12e3319e4b1jZbbajZ9aebZ\", \"Z12e3319e4b1jZbbajZ9aj23\", \"Z12e3319e4b1jZbbajZ9b22b\", \"Z12e3319e4b1jZbbajZ9b42a\", \"Z12e3319e4b1jZbbajZ9b49a\", \"Z12e331ae4b1jZbbajZ9bZ11\", \"Z12e331ae4b1jZbbajZ9bZZ4\", \"Z12e331ae4b1jZbbajZ9c237\", \"Z12e331ae4b1jZbbajZ9c2e4\", \"Z12e331ae4b1jZbbajZ9c3bZ\", \"Z12e331ae4b1jZbbajZ9c3cZ\", \"Z12e331ae4b1jZbbajZ9cZ31\", \n\"Z12e331be4b1jZbbajZ9cddd\", \"Z12e331be4b1jZbbajZ9cja6\", \"Z12e331ce4b1jZbbajZ9da1j\", \"Z12e331de4b1jZbbajZ9e649\", \"Z12e331de4b1jZbbajZ9j669\", \"Z12e331de4b1jZbbajZ9jZZZ\", \"Z12e331ee4b1jZbbajZ9j944\", \"Z12e331ee4b1jZbbajZ9jcZa\", \"Z12e331ee4b1jZbbajZ9jd4c\", \"Z12e331ee4b1jZbbajZa11e2\", \"Z12e331ee4b1jZbbajZa1291\", \"Z12e331ee4b1jZbbajZa1344\", \"Z12e3311e4b1jZbbajZa1j73\", \"Z12e3311e4b1jZbbajZa1131\", \"Z12e3311e4b1jZbbajZa11Z6\", \"Z12e3311e4b1jZbbajZa124c\", \"Z12e3311e4b1jZbbajZa1Zbc\", \"Z12e3311e4b1jZbbajZa19a9\", \n\"Z12e3311e4b1jZbbajZa1ac2\", \"Z12e3311e4b1jZbbajZa1b79\", \"Z12e3311e4b1jZbbajZa1db2\", \"Z12e3311e4b1jZbbajZa1ejb\", \"Z12e3312e4b1jZbbajZa2333\", \"Z12e3312e4b1jZbbajZa23aZ\", \"Z12e3312e4b1jZbbajZa24bb\", \"Z12e3312e4b1jZbbajZa2Z79\", \"Z12e3312e4b1jZbbajZa2Zea\", \"Z12e3312e4b1jZbbajZa2ba9\", \"Z12e3312e4b1jZbbajZa2cZa\", \"Z12e3313e4b1jZbbajZa3bc1\", \"Z12e3313e4b1jZbbajZa3ca9\", \"Z12e3313e4b1jZbbajZa3e71\", \"Z12e3ajbe4b1j66Zbcja4eZc\", \"Z12e3ajbe4b1j66Zbcja4ja4\", \"Z12e3c79e4b1j66ZbcjaZc36\", \"Z12e3e1ce4b1j66Zbcja64bd\", \n\"Z12e4117e4b1j66Zbcja6Zj1\", \"Z12e41bae4b1j66Zbcja734Z\", \"Z12e4226e4b1j66Zbcja7b13\", \"Z12e4226e4b1j66Zbcja7cbZ\", \"Z12e4ajee4b1j66Zbcjaa916\", \"Z12e4e61e4b1j66Zbcjab1c2\", \"Z12e4e61e4b1j66Zbcjab2da\", \"Z12eZ226e4b1j66ZbcjacZea\", \"Z12e6141e4b1j66Zbcjb19Z9\", \"Z12e6141e4b1j66Zbcjb19jd\", \"Z12e61Z9e4b1j66Zbcjb1acb\", \"Z12e61Z9e4b1j66Zbcjb1acj\", \"Z12j9713e4b1j66Zbcjc34db\", \"Z12j9713e4b1j66Zbcjc3ZZa\", \"Z12j9713e4b1j66Zbcjc3Za7\", \"Z12j9713e4b1j66Zbcjc3Zd2\", \"Z12j9713e4b1j66Zbcjc36c2\", \"Z12j973ce4b1j66Zbcjc396b\"\n)"
[2] "c(\"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \n\"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\", \"Something\")"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
[3] "c(61Z7, 674Z, Z462, 692, Z26, 1121, 1213, 1317, 21ZZ, 2Z9Z, 2711, 3612, 3717, 4774, 4Z93, Z117, Z113, Z197, Z77Z, 61Z3, Z16Z, 11771, 12923, 13374, 13Z93, 14277, 1446Z, 1Z3ZZ, 1ZZ16, 1Z993, 164Z2, 16664, 1711Z, 171Z6, 1Z6ZZ, 1Z921, 19211, 193ZZ, 19931, 21117, 21164, 21177, 21371, 21Z61, 21673, 22ZZ7, 23137, 2ZZ44, 26166, 26Z1Z, 173Z6, 17661, 21Z74, 23119, 232ZZ, 249Z3, 2ZZ31, 261Z9, 31211, 33414, 336Z6, 37941, 1743, 1Z61, 216Z, 2171, 1ZZ3, 2119, 21Z4, 2129, 2334, 2ZZZ)"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
[4] "c(\"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \n\"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\", \"Booty\")"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
[5] "c(Z6, 93Z, 1314, 3, 4, Z, 6, 7, 9, 11, 11, 13, 14, 2Z, 26, 27, 2Z, 29, 33, 34, ZZ, Z3, 122, 12Z, 133, 139, 142, 147, 1Z2, 1Z3, 16Z, 169, 171, 171, 219, 221, 221, 222, 22Z, 226, 244, 246, 247, 24Z, 249, 2637, 264, 2Z9, 292, 296, 49, Z1, 76, 93, 9Z, 112, 111, 114, 1Z7, 211, 214, 263, 6, 7, 11, 11, 11, 11, 12, 13, 14, 1Z)"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
[6] "c(3Z11, 3Z11, 3Z11, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, 691Z, Z664, Z664, Z664, Z664, Z664, Z664, Z664, Z664, Z664, Z664, Z664, Z664, 66Z1, 66Z1, 66Z1, 66Z1, 4ZZ4, 4ZZ4, 4ZZ4, 4ZZ4, 4ZZ4, 4ZZ4)"       

如果这是为了向您展示更多信息,那么您会看到 [18] 的消息正文。

最佳答案

您的示例对我来说运行起来有点复杂,但是我多次遇到此错误,并且问题最终始终是由于提取函数(即 [])的默认行为强制到尽可能低的数量方面。正如 BondedDust 所观察到的那样,如果您从数据框中提取单个列,则无法再选择具有相同语法的帧子集,因为您不再拥有数据框。

如果在任何将数据框缩减为单个列的操作中,在提取操作中设置参数 drop=FALSE,这些问题通常会消失。我建议您不仅要仔细查看产生错误的行,还要仔细查看在问题数据帧上使用“[]”操作的任何前面的行。看extract函数的数据框方法的帮助,“extract.data.frame”
相信问题可能是当您将数据框子集到单个列时,它被强制转换为单个维度,并且不能再按列号或行号进行索引。

关于r - "Incorrect number of dimensions"错误,帮我理解为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15713222/

相关文章:

html - 使用 CSS 将图像尺寸设置为 "good"是否与在 HTML 中设置它们一样?

android -/values-normal-mdpi 中的值覆盖/values 中的值

r - 什么相当于 do.call(rbind, list)?

r - 将 Rstudio 与 Docker 组合使用并将环境变量传递给 rstudio 用户

r - 从 CSV 创建 xts 对象

android - android中的不同值文件夹

r - 如何将输入变量传递给 R 中的 SQL 语句?

r - 使用命名列表转换变量

android - 在我的手机上调整大小的图像在另一部手机上的大小是否相同?

android - 调整默认 android 标签问题