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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:29:25+00:00 2026-06-12T13:29:25+00:00

I have a variable that is a factor : $ year : Factor w/

  • 0

I have a variable that is a factor :

 $ year           : Factor w/ 8 levels "2003","2004",..: 4 6 4 2 4 1 3 3 7 2 ...

I would like to create 8 dummy variables, named “2003”, “2004” etc that take the value 0 or 1 depending on the value that the variable “year” takes. The nearest I could come up with is

dt1 <- cbind (dt1, model.matrix(~dt1$year - 1) )

But this has the unfortunate consequences of

  1. The dummy variables are named dt1$year2003, not just “2003”, “2004” etc
  2. It seems that NA rows are omitted altogether by model.matrix (so the above command fails due to different lengths when NA is present in the year variable).

Of course I can get around these problems with more code, but I like my code to be as concise as possible (within reason) so if anyone can suggest better ways to make the dummy variables I would be obliged.

  • 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-12T13:29:26+00:00Added an answer on June 12, 2026 at 1:29 pm

    This is as concise as I could get. The na.action option takes care of the NA values (I would rather do this with an argument than with a global options setting, but I can’t see how). The naming of columns is pretty deeply hard-coded, don’t see any way to override it within model.matrix …

    options(na.action=na.pass)
    dt1 <- data.frame(year=factor(c(NA,2003:2005)))
    dt2 <- setNames(cbind(dt1,model.matrix(~year-1,data=dt1)),
                  c("year",levels(dt1$year)))
    

    As pointed out above, you may run into trouble in some contexts with column names that are not legal R variable names.

      year 2003 2004 2005
    1 <NA>   NA   NA   NA
    2 2003    1    0    0
    3 2004    0    1    0
    4 2005    0    0    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column of data that is a factor with levels A, B
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
Does plyr skip missing levels of a factor [that is the grouping variable]? It's
I have a class that has overloaded constructors. I'd like to re-factor the code
Suppose I have a variable that lasts for several periods. Like the amount of
I have a column that's a factor variable. I need to change the value
I'm studying javascript these days and I have question. I have variable that contain
I have a variable that may contain objects or may be undefined. I wish
I have a variable that contains the following JSON string: { 0 : Jun
I have a variable that im define with another variable and some text. $title

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.