"维数不正确"错误,帮助我理解原因

use*_*772 13 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)
Run Code Online (Sandbox Code Playgroud)

一,背景

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

II.问题/问题

调用该代码中的一个主要功能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"))
}
Run Code Online (Sandbox Code Playgroud)

我的数据在很多方面都有所不同,所以我必须对此进行相当多的编辑.我之前从关系数据库中读取了我的数据,因此我不必读入并清理文本文件.相反,我的电子邮件正文数据是数据框的第18列,我们可以调用它x.这是我的版本get.msg():

get.msg <- function(path) {
  bodyvector <- path[!(is.na(path[,18]) | path[,18]==""), ]
  return(paste(bodyvector))
}
Run Code Online (Sandbox Code Playgroud)

本来我提到了它作为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"))
Run Code Online (Sandbox Code Playgroud)

这里,count.word()函数是一个包含的函数get.msg().因此,该paste()函数导致了问题,因为它x.path显然被认为是原子数组,并且给出了$不能与原子数组一起使用的错误.根据较旧的StackOverflow Q&A,我改变了我引用列的方式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.
Run Code Online (Sandbox Code Playgroud)

我试过path[,'email'],然后path[18,]只是path自己,所有三个导致了同样的错误.我试过path[[1]][[18]],这给了我一个下标超出界限的错误.

有什么想法吗?

III.采取措施使这个问题变得更好

为了避免让任何人烦恼并获得任何投票,我确认该主题与StackOverflow相关,我觉得它可能与将来处理此类或类似编程问题的其他人有关.我也花了将近一个小时在线研究这个问题并在R中尝试修复它.

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

IV.的输出head()dput()

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

我把它贴在这里,并用假信息取代了真实的信息.我希望这没关系.我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)"       
Run Code Online (Sandbox Code Playgroud)

如果这是为了向您展示更多,那么您将看到[18]的消息体.

and*_*ewH 5

你的例子对我来说有点复杂,但是我已经多次出现这个错误了,问题一直是由于提取函数的默认行为(即[])强制转换到最低可能的数量尺寸.正如BondedDust观察到的那样,如果从数据框中提取单个列,则无法再使用相同的语法选择帧的子集,因为您不再拥有数据框.

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


42-*_*42- 2

这可能值得发表评论,但不合适,如果有必要,我准备删除。你说

“因此,粘贴函数导致了问题,因为它导致 x.path 显然被视为原子数组,并给出了 $ 无法与原子数组一起使用的错误。根据旧的 StackOverflow 问答,我改变了我的方式将列引用到 path[,18](其计算结果为 x.path[,18],因此与 x[,18] 相同)。”

如果 x.path 是原子数组,那么您不能使用x.path[ , 18],但需要使用x.path[18]

您可以使用 str(x.path) 检查 x.path ,您的输出表明这确实是一个字符向量。在 R 中,只有二维对象(矩阵和 data.frames)可以通过 object[ , n] 引用来引用。