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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:46:18+00:00 2026-05-21T07:46:18+00:00

I have a formula and a data frame, and I want to extract the

  • 0

I have a formula and a data frame, and I want to extract the model.matrix(). However, I need the resulting matrix to include the NAs that were found in the original dataset. If I were to use model.frame() to do this, I would simply pass it na.action=NULL. However, the output I need is of the model.matrix() format. Specifically, I need only the right-hand side variables, I need the output to be a matrix (not a data frame), and I need factors to be converted to a series of dummy variables.

I’m sure I could hack something together using loops or something, but I was wondering if anyone could suggest a cleaner and more efficient workaround. Thanks a lot for your time!

And here’s an example:

dat <- data.frame(matrix(rnorm(20),5,4), gl(5,2))
dat[3,5] <- NA
names(dat) <- c(letters[1:4], 'fact')
ff <- a ~ b + fact

# This omits the row with a missing observation on the factor
model.matrix(ff, dat) 

# This keeps the NA, but it gives me a data frame and does not dichotomize the factor
model.frame(ff, dat, na.action=NULL) 

Here is what I would like to obtain:

   (Intercept)          b fact2 fact3 fact4 fact5
1            1  0.7266086     0     0     0     0
2            1 -0.6088697     0     0     0     0
3            NA 0.4643360     NA    NA    NA    NA
4            1 -1.1666248     1     0     0     0
5            1 -0.7577394     0     1     0     0
6            1  0.7266086     0     1     0     0
7            1 -0.6088697     0     0     1     0
8            1  0.4643360     0     0     1     0
9            1 -1.1666248     0     0     0     1
10           1 -0.7577394     0     0     0     1
  • 1 1 Answer
  • 1 View
  • 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-21T07:46:19+00:00Added an answer on May 21, 2026 at 7:46 am

    You can mess around a little with the model.matrix object, based on the rownames :

    MM <- model.matrix(ff,dat)
    MM <- MM[match(rownames(dat),rownames(MM)),]
    MM[,"b"] <- dat$b
    rownames(MM) <- rownames(dat)
    

    which gives :

    > MM
         (Intercept)         b fact2 fact3 fact4 fact5
    1              1 0.9583010     0     0     0     0
    2              1 0.3266986     0     0     0     0
    3             NA 1.4992358    NA    NA    NA    NA
    4              1 1.2867461     1     0     0     0
    5              1 0.5024700     0     1     0     0
    6              1 0.9583010     0     1     0     0
    7              1 0.3266986     0     0     1     0
    8              1 1.4992358     0     0     1     0
    9              1 1.2867461     0     0     0     1
    10             1 0.5024700     0     0     0     1
    

    Alternatively, you can use contrasts() to do the work for you. Constructing the matrix by hand would be :

    cont <- contrasts(dat$fact)[as.numeric(dat$fact),]
    colnames(cont) <- paste("fact",colnames(cont),sep="")
    out <- cbind(1,dat$b,cont)
    out[is.na(dat$fact),1] <- NA
    colnames(out)[1:2]<- c("Intercept","b")
    rownames(out) <- rownames(dat)
    

    which gives :

    > out
         Intercept          b fact2 fact3 fact4 fact5
    1            1  0.2534288     0     0     0     0
    2            1  0.2697760     0     0     0     0
    3           NA -0.8236879    NA    NA    NA    NA
    4            1 -0.6053445     1     0     0     0
    5            1  0.4608907     0     1     0     0
    6            1  0.2534288     0     1     0     0
    7            1  0.2697760     0     0     1     0
    8            1 -0.8236879     0     0     1     0
    9            1 -0.6053445     0     0     0     1
    10           1  0.4608907     0     0     0     1
    

    In any case, both methods can be incorporated in a function that can deal with more complex formulae. I leave the exercise to the reader (what do I loath that sentence when I meet it in a paper 😉 )

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

Sidebar

Related Questions

I have some data that needs to be validated, and while I have found
i want to show some data in percent. i have a mathematics formula like:
I have an Excel formula reading data from a column. The data in that
I have a formula that distribute reptitions (payments) into columns that represents months. I
I need to have a formula produce a null in some cases, numeric in
is there a way in Excel to have a formula that does something like
I have the following formula float mean = (r+b+g)/3/255.0f; I want to speed it
I have been trying to use a formula that is used to work out
How would I convert a column of R formulas to include the data frame
i have a column with this data: IT_AMPH IT_BARB IT_BENZ IT_BUP SOMA i want

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.