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

  • Home
  • SEARCH
  • 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 8924443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:25:07+00:00 2026-06-15T07:25:07+00:00

I have a data.table X that I would like to create a variable based

  • 0

I have a data.table X that I would like to create a variable based on 2 character variables

   X[, varC :=((VarA =="A" & !is.na(VarA)) 
               | (VarA == "AB" & VarB =="B" & !is.na(VarA) & !is.na(VarB))
                )
      ]

This code works but it is very slow, because it does vector scan on 2 char variables. Note that I don’t setkey claims4 table by VarA and VarB. Is there a “right” way to do this in data.table?

Update 1: I don’t use setkey for this transformation because I already use setkey(X, Year, ID) for other variable transformations. If I do, I need to reset keys back to Year, ID after this transformation.

Update 2: I did benchmark my approach with Matthew’s approach, and his is much faster:

          test replications elapsed relative user.self sys.self user.child sys.child
2 Matthew               100   3.377    1.000     2.596    0.605          0         0
1 vectorSearch          100 200.437   59.354    76.628   40.260          0         0

The only minor thing is setkey then re-setkey again is somewhat verbose 🙂

  • 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-06-15T07:25:08+00:00Added an answer on June 15, 2026 at 7:25 am

    How about :

    setkey(X,VarA,VarB)
    X[,varC:=FALSE]
    X["A",varC:=TRUE]
    X[J("A","AB"),varC:=TRUE]
    

    or, in one line (to save repetitions of the variable X and to demonstrate) :

    X[,varC:=FALSE]["A",varC:=TRUE][J("A","AB"),varC:=TRUE]
    

    To avoid setting the key, as requested, how about a manual secondary key :

    S = setkey(X[,list(VarA,VarB,i=seq_len(.N))],VarA,VarB)
    X[,varC:=FALSE]
    X[S["A",i][[2]],varC:=TRUE]
    X[S[J("A","AB"),i][[3]],varC:=TRUE]
    

    Now clearly, that syntax is ugly. So FR#1007 Build in secondary keys is to build that into the syntax; e.g.,

    set2key(X,varA,varB)
    X[...some way to specify which key to join to..., varC:=TRUE]
    

    In the meantime it’s possible, just manually, as shown above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel table with some data that I would like to export
I have some data in a table that looks roughly like the following: table
I have a data.frame and I'm trying to create a frequency table that shows
I have a data table ( DatosMex ) in R and would like to
I have an insert statement that pulls some data into a few table variables
If I have a table that has data like: Test1 Test2 Test3 Test4 Test5
Hello I have a datatable that I would like to filter with a single
I have a Datatable in my C# program that I would like to INSERT
I have the data table from the jquery plugin dataTables (http://datatables.net/) that I want
I have table that I insert data with following query (from c# code): INSERT

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.