Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:51:57+00:00 2026-05-26T06:51:57+00:00

UPDATE This problem is not relevant anymore for data.table versions 1.8.0 and higher. From

  • 0

UPDATE This problem is not relevant anymore for data.table versions 1.8.0 and higher. From the NEWS file:

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.

Original question

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.

Here is an example:

#Objective: Select all rows from DT for which Region=="US", Year >= 5 & Year<=8, Cat="A"                 
library(data.table)
#Set-up data.table DT
DT <- 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   <- 5:8
df      <- data.frame(Region=c("US", "EU"), Categ=c("A", "B"))
int_DT <- 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 <- as.character(df$Region)
df$Categ  <- as.character(df$Categ)
#Rebuild int_DT
int_DT    <- 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

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:51:58+00:00Added an answer on May 26, 2026 at 6:51 am

    You don’t need a join operation to get your desired results. You said:
    ‘Objective: Select all rows from DT for which Region==”US”, Year >= 5 & Year<=8, Cat=”A”‘

    DT[Region=="US" & Year>=5 & Year <= 8 & Categ=="A"]
         Year       value Region Categ
    [1,]    5 -0.18631697     US     A
    [2,]    6  1.40059083     US     A
    [3,]    7  0.01848557     US     A
    

    But to answer your question about column classes. I managed to get this code to work, which essentially mirrors your code above:

    > setkey(DT, Region, Categ, Year)
    > df      <- data.frame(Region=c("US", "EU"), Categ=c("A", "B"))
    > dt2 <- data.table(data.frame(df[1, ], Year=5:8))
    Warning message:
    In data.frame(df[1, ], Year = 5:8) :
      row names were found from a short variable and have been discarded
    > dt1[dt2]
         Region Categ Year      value
    [1,]     US     A    5 -0.5565422
    [2,]     US     A    6 -0.1805841
    [3,]     US     A    7  1.4474403
    [4,]     US     A    8         NA
    

    The same, with column classes of character:

    df$Region <- as.character(df$Region)
    df$Categ  <- as.character(df$Categ)
    #Rebuild int_DT
    dt2    <- J(cbind(df[1, ], Year=5:8))
    
    Warning message:
    In data.frame(..., check.names = FALSE) :
      row names were found from a short variable and have been discarded
    
    setkey(dt2, Region)
    dt1[dt2]
       Region Year       value Categ Categ.1 Year.1
           US    1  1.20152558     A       A      5
           US    2  1.89391079     A       A      5
           US    3 -1.76022634     A       A      5
           US    4  0.92454680     A       A      5
           US    5 -0.55654217     A       A      5
           ...
           snip 
           ...
           US    9  0.67936243     B       A      8
           US   10 -0.09355764     B       A      8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE It looks like this problem has been quietly fixed in iOS 4.3. Up
Update: After some more reading I see that this problem is totally general, you
Update: This is, as I was told, no principle Python related problem, but seems
I have a problem when trying to execute this update statement (below) using C#
I am having virtually the same problem as this: C# Update combobox bound to
Update II Problem Solved but Why? This has been the biggest headache ever. My
UPDATE: First problem solved, second one described at the bottom of this post. UPDATE2:
I have this code, but I have a problem. When I update but do
i have problem use link_to_remote link_to_remote document example say link_to_remote Delete this post, :update
UPDATE: Perhaps this wasn't clear from my original post, but I'm mainly interested in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.