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 8367197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:05:53+00:00 2026-06-09T13:05:53+00:00

I have a data table ( DatosMex ) in R and would like to

  • 0

I have a data table (DatosMex) in R and would like to recode a column within it named industry. The distinct categories for this variable are:

  Agricultura,Ganaderia,Pesca,Caza Forestal                      
  Asociaciones                                                       
  Comercio                                                       
  Construccion                                                   
  Energia,Petroleo,Gas,Mineria                                   
  Gobierno                                                       
  Industria                                                      
  N/A                                                            
  NULL                                                           
  Servicios                      

I want to create a new variable, say gr_industry, that groups some categories. For instance, my new variable must group the categories Agricultura,Ganaderia,Pesca,Caza Forestal, Asociaciones,Energia,Petroleo,Gas,Mineria and Gobienro and assign them the code 1.

How would you do this using the data.table package syntax?

My approach was this:

 #Create an id for each industry
 DatosMex[,cod_industria:=as.numeric(DatosMex$industry)] 
 #Create a new data table
 ind =data.table(cod_industria=c(1:10),gr_industry=c(1,1,2,3,1,1,4,6,6,5))
 setkey(DatosMex,cod_industria)
 setkey(ind,cod_industria)
 DatosMex[ind] 

So, as you can see, I had to create a new data table ind and then do the inner join. My question is: is there another way of doing this using the data.table way? I don’t want to create a table each time I need to do something similar. Also, I’d like to avoid using if statements.

  • 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-09T13:05:55+00:00Added an answer on June 9, 2026 at 1:05 pm

    I’m guessing one does not need to set a key or create a new data.table. The [ function is generally very fast, especially in datatable-objects:

     DatosMex[, gr_industry := c(1,1,2,3,1,1,4,6,6,5)[cod_industria] ]
    

    If that grouping translation vector is large then you can refer to it by name, even if it is outside the data.table.

     dta <- data.table(a=sample(1:10, 20, repl=TRUE))
     g6<- c(1,1,2,3,1,1,4,6,6,5)
     dta[ , ind := g6[a] ]
     #-------------------
         a ind
     1:  8   6
     2:  4   3
     3: 10   5
     4:  8   6
     snipped output
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
If i have data in my MySQL like this table: data(TEXT) foo (hal) foo
I have a data.table DT with a column named RF and many columns with
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a data.table object similar to this one library(data.table) c <- data.table(CO =
I have data in data table like below. I am trying to make graph
I have a data table. After creating this table, I use data grid view
I have a data.table with two columns: one ID column and one value column.
I need function, that returns list of strings. I have data in table like
I have a data table and I want to select all distinct names from

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.