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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:46:29+00:00 2026-06-07T02:46:29+00:00

Given data that looks like: library(data.table) DT <- data.table(x=rep(1:5, 2)) I would like to

  • 0

Given data that looks like:

library(data.table)
DT <- data.table(x=rep(1:5, 2))

I would like to split this data into 5 boolean columns that indicate the presence of each number.

I can do this like this:

new.names <- sort(unique(DT$x))

DT[, paste0('col', new.names) := lapply(new.names, function(i) DT$x==i), with=FALSE]

But this uses a pesky lapply which is probably slower than the data.table alternative and this solutions strikes me as not very “data.table-ish”.

Is there a better and/or faster way to create these new columns?

  • 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-07T02:46:32+00:00Added an answer on June 7, 2026 at 2:46 am

    How about model.matrix?

    model.matrix(~factor(x)-1,data=DT)
    
       factor(x)1 factor(x)2 factor(x)3 factor(x)4 factor(x)5
    1           1          0          0          0          0
    2           0          1          0          0          0
    3           0          0          1          0          0
    4           0          0          0          1          0
    5           0          0          0          0          1
    6           1          0          0          0          0
    7           0          1          0          0          0
    8           0          0          1          0          0
    9           0          0          0          1          0
    10          0          0          0          0          1
    attr(,"assign")
    [1] 1 1 1 1 1
    attr(,"contrasts")
    attr(,"contrasts")$`factor(x)`
    [1] "contr.treatment"
    

    Apparently, you can put model.matrix into [.data.table to give the same results. Not sure if it would be faster:

    DT[,model.matrix(~factor(x)-1)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Table that looks like this MAC-ADDRESS | ACCESSPOINT | TIMESTAMP Data
Given data that looks similar to this: +---------+-----------+----------+ | country | city | district
Basically I have an XML file that looks like this: <?xml version=1.0 encoding=UTF-8?> <data>
So I have a table that looks like this: Name ID TaskID HoursAssigned ----------------------------------------------------------------
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a data that looks like this #Status value TP 5.000 TP 3.000
What algorithm would you use to create an application that given appropriate data (list
Given a data set like this; +-----+---------------------+--------+ | id | date | result |
I have data that looks like the following: ID | Location | Attendees |
I have created a class Person that looks like this: public class Person {

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.