Organizzazione di questa domanda:R: "Numero errato di dimensioni" errore nella R - si prega di aiutarmi a capire il motivo per cui
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)
I. Contesto
sto Personalizzazione/adattamento del codice di classificazione e-mail dal libro O'Reilly "Machine Learning for Hackers" (capitolo 3). Questo codice e i relativi dati di accompagnamento possono essere trovati qui: https://github.com/johnmyleswhite/ML_for_Hackers/tree/master/03-Classification
II. The Problem/Question
Una delle funzioni principali in tale codice è denominata get.msg()
. La funzione originaria è
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"))
}
miei dati è differente in vari modi, però, quindi devo modificare questo un bel po '. I miei dati vengono letti in precedenza da un DB relazionale, quindi non devo leggere e pulire un file di testo. Invece, i miei dati del corpo dell'email sono la diciottesima colonna di un dataframe, che possiamo chiamare x
. Ecco la mia versione di get.msg()
:
get.msg <- function(path) {
bodyvector <- path[!(is.na(path[,18]) | path[,18]==""), ]
return(paste(bodyvector))
}
Originariamente ho fatto riferimento ad esso come x$email
e questo ha funzionato attraverso la maggior parte del codice, ma in una fase successiva la funzione get.msg()
è stato utilizzato su x.path
, dove x.path
indicò x
ed era utilizzato all'interno di un'altra funzione in combinazione con la funzione paste()
, come per esempio del codice autori:
z.spam <- sapply(spam.docs, function(p) count.word(paste(x.path,p,sep = ""), "keyword"))
Qui, la funzione è una funzione count.word()
contenente 012.391.861,772 mila. Quindi, la funzione paste()
causava problemi perché causava il fatto che x.path
appariva come una matrice atomica apparentemente e dava l'errore che $ non potesse essere usato con una matrice atomica. Come per un precedente StackOverflow Q & A, ho cambiato il modo in cui ho fatto riferimento alla colonna in path[,18]
(che è valutata come x.path[,18]
e quindi è la stessa di x[,18]
).
Quindi ho fatto qualche controllo per garantire che lo x.path[,18]
avesse le stesse informazioni di x.path$email
, cosa che ha fatto. Tuttavia, quando si tenta di eseguire il codice ottengo un messaggio di errore sul get.msg(x.path)
, che è:
Error in path[,18] : incorrect number of dimensions.
ho provato path[,'email']
, quindi path[18,]
e poi basta path
per sé e tutti e tre hanno portato alla lo stesso errore. Ho provato path[[1]][[18]]
e questo mi ha dato un errore di pedice fuori limite.
Qualche idea?
III. Passi per rendere questa domanda buona
Per evitare di annoiare nessuno e ottenere voti bassi, ho confermato che l'argomento era rilevante per StackOverflow e ritengo che possa essere rilevante per altre persone che si occupano di questo o di problemi di programmazione simili in il futuro. Ho anche trascorso quasi un'ora a cercare questo problema online e provare cose in R per risolverlo.
C'erano molti riferimenti a questo messaggio di errore, tuttavia le cause sembravano molto diverse e completamente estranee (come problemi di rete, ecc.). Infine, ho dedicato molto tempo a modificare questa domanda per cercare di renderla leggibile e formattata correttamente (spero di averlo fatto bene, so che sono molte informazioni).
IV. L'uscita del head()
e dput()
Alcuni di voi estremamente utile persone hanno chiesto di vedere il risultato del head(x.path)
o dput(x.path)
. Non mi interessa se non che sono dati riservati delle e-mail aziendali e che non avrò un lavoro e non farò causa se lo pubblicherò. ;-)
Ho incollato qui e sostituito le informazioni reali con informazioni false. Spero che vada bene. Ho provato ad usare dput()
all'inizio e posso farlo se vuoi, ma è stata davvero una quantità enorme di dati. Ecco 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)"
Se questo dovesse visualizzare più allora si vedrebbe corpi dei messaggi per [18].
Sarà molto più facile se ci mostri il tuo oggetto (testa, str) e la linea di codice incriminata. Un esempio riproducibile potrebbe andare ancora più lontano. –
Si suppone che 'path' debba essere un oggetto bidimensionale (ad esempio' data.frame' o 'matrix') in modo da poter eseguire' path [, 18] '; il tuo 'x.path' non lo è. Basta fare 'class (x.path)' e dovresti vederlo. Aggiornamento – flodel
: Ho provato anche a sostituire [, 18] con [, 'email'] ma ho ricevuto lo stesso messaggio.Vedo due commenti spuntati mentre sto modificando questo, quindi permettimi di salvare il mio elogio quindi seguirò il tuo (e grazie btw!). Vi darei l'output di head() ma è un corpo di posta confidenziale:/ – user2225772