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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:34:08+00:00 2026-05-16T02:34:08+00:00

I have a several data sets with 75,000 observations and a type variable that

  • 0

I have a several data sets with 75,000 observations and a type variable that can take on a value 0-4. I want to add five new dummy variables to each data set for all types. The best way I could come up with to do this is as follows:

# For the 'binom' data set create dummy variables for all types in all data sets
binom.dummy.list<-list()
for(i in 0:4){
    binom.dummy.list[[i+1]]<-sapply(binom$type,function(t) ifelse(t==i,1,0))
}

# Add and merge data
binom.dummy.df<-as.data.frame(do.call("cbind",binom.dummy.list))
binom.dummy.df<-transform(binom.dummy.df,id=1:nrow(binom))
binom<-merge(binom,binom.dummy.df,by="id")

While this works, it is incredibly slow (the merge function has even crashed a few times). Is there a more efficient way to do this? Perhaps this functionality is part of a package that I am not familiar with?

  • 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-16T02:34:09+00:00Added an answer on May 16, 2026 at 2:34 am

    R has a “sub-language” to translate formulas into design matrix, and in the spirit of the language you can take advantage of it. It’s fast and concise. Example: you have a cardinal predictor x, a categorical predictor catVar, and a response y.

    > binom <- data.frame(y=runif(1e5), x=runif(1e5), catVar=as.factor(sample(0:4,1e5,TRUE)))
    > head(binom)
              y          x catVar
    1 0.5051653 0.34888390      2
    2 0.4868774 0.85005067      2
    3 0.3324482 0.58467798      2
    4 0.2966733 0.05510749      3
    5 0.5695851 0.96237936      1
    6 0.8358417 0.06367418      2
    

    You just do

    > A <- model.matrix(y ~ x + catVar,binom) 
    > head(A)
      (Intercept)          x catVar1 catVar2 catVar3 catVar4
    1           1 0.34888390       0       1       0       0
    2           1 0.85005067       0       1       0       0
    3           1 0.58467798       0       1       0       0
    4           1 0.05510749       0       0       1       0
    5           1 0.96237936       1       0       0       0
    6           1 0.06367418       0       1       0       0
    

    Done.

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

Sidebar

Related Questions

I have several medium-sized data sets in-memory that I need to be able to
I have several listboxes that get each of their data from a separate stored
I have several identical elements with different attributes that I'm accessing with SimpleXML: <data>
I have a long running insert transaction that inserts data into several related tables.
I have several models whose records AND associations can have two states that must
I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I currently have a list view which has several rows of data and I
I have a ~23000 line SQL dump containing several databases worth of data. I
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on

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.