Sto cercando di capire la logica di J() ricerca quando ci sei chiavi duplicate in un data.table in R.Perché è richiesto allow.cartesian quando si uniscono dati.tables con chiavi duplicate?
Ecco un piccolo esperimento ho provato:
library(data.table)
options(stringsAsFactors = FALSE)
x <- data.table(keyVar = c("a", "b", "c", "c"),
value = c( 1, 2, 3, 4))
setkey(x, keyVar)
y1 <- data.frame(name = c("d", "c", "a"))
x[J(y1$name), ]
## OK
y2 <- data.frame(name = c("d", "c", "a", "b"))
x[J(y2$name), ]
## Error: see below
x2 <- data.table(keyVar = c("a", "b", "c"),
value = c( 1, 2, 3))
setkey(x2, keyVar)
x2[J(y2$name), ]
## OK
Il messaggio di errore Sto ottenendo è:
Error in vecseq(f__, len__, if (allow.cartesian) NULL else as.integer(max(nrow(x), :
Join results in 5 rows; more than 4 = max(nrow(x),nrow(i)). Check for duplicate key
values in i, each of which join to the same group in x over and over again. If that's
ok, try including `j` and dropping `by` (by-without-by) so that j runs for each group
to avoid the large allocation. If you are sure you wish to proceed, rerun with
allow.cartesian=TRUE. Otherwise, please search for this error message in the FAQ, Wiki,
Stack Overflow and datatable-help for advice.
Non lo capisco davvero. So che dovrei evitare chiavi duplicate in una funzione di ricerca, voglio solo ottenere alcune informazioni quindi non farò alcun errore in futuro.
Grazie mille per l'aiuto. Questo è un grande strumento.
'allow.cartesian' è stato implementato dopo che ho fatto qualcosa di stupido quando ho iniziato a utilizzare data.table: http://stackoverflow.com/q/11610562/1412059 – Roland