Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat are the restrictions for the column classes in data.table?
    primarykey
    data
    text
    <p><strong>UPDATE</strong> This problem is not relevant anymore for <code>data.table</code> versions 1.8.0 and higher. From the NEWS file:</p> <blockquote> <p>character columns are now allowed in keys and are preferred to factor. data.table() and setkey() no longer coerce character to factor. Factors are still supported. Implements FR#1493, FR#1224 and (partially) FR#951.</p> </blockquote> <p><strong>Original question</strong></p> <p>I try to join two data.tables. However, the success of the join is dependent on the classes of the columns I use to match the data.tables. More precisely, it seems that the columns should not have the class "character". I don't quite understand the reason, but I'm sure I'm missing something obvious here. So help is really appreciated. </p> <p>Here is an example:</p> <pre><code>#Objective: Select all rows from DT for which Region=="US", Year &gt;= 5 &amp; Year&lt;=8, Cat="A" library(data.table) #Set-up data.table DT DT &lt;- data.table(Year=1:20, value=rnorm(20), Region=c(rep("US", 10), rep("EU", 10)), Cat=c(rep("A", 7), rep("B", 7), rep("C", 6))) setkey(DT, Region, Cat, Year) #Set-up data.table int_DT to join with DT years &lt;- 5:8 df &lt;- data.frame(Region=c("US", "EU"), Categ=c("A", "B")) int_DT &lt;- J(cbind(df[1, ], years)) #Join them: Works like a charm! DT[int_DT] #Let's assume that for any reason the columns in df are of class "character" df$Region &lt;- as.character(df$Region) df$Categ &lt;- as.character(df$Categ) #Rebuild int_DT int_DT &lt;- J(cbind(df[1, ], years)) DT[int_DT] #Error in `[.data.table`(DT, int_DT) : # unsorted column Region of i is not internally type integer. #OK, maybe the problem is that the column classes in DT are factors, so change those: DT[, Cat:=as.character(Cat)] DT[, Region:=as.character(Region)] DT[int_DT] #Error in `[.data.table`(DT, int_DT) : # When i is a data.table, x must be sorted to avoid a vector scan of x per row of i </code></pre> <p>Still doesn't work. Why? What is the restriction? What do I miss? Additionally information: I'm using data.table 1.6.6 and R version 2.13.2 (2011-09-30) on Platform: x86_64-pc-linux-gnu (64-bit).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload