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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:35:24+00:00 2026-06-15T12:35:24+00:00

I’m trying to use multiple columns from a data.frame in a model.matrix. The data

  • 0

I’m trying to use multiple columns from a data.frame in a model.matrix.

The data frame looks like this:

    df1 <- data.frame(id=seq(1,10,1), zip1=(round(runif(10)*100000,0)), zip2=(round(runif(10)*100000,0))
    ,var1=round(runif(10)*100,1),Sales=round(runif(10)* 10000,2))
df1$zip1 <- ifelse(nchar(as.character(df1$zip1)) < 5,paste("0",as.character(df1$zip1),sep=""),df1$zip1)
df1$zip2 <- ifelse(nchar(as.character(df1$zip2)) < 5,paste("0",as.character(df1$zip2),sep=""),df1$zip2)

df1$zip1 <- factor(df1$zip1)
df1$zip2 <- factor(df1$zip2)
dput(df1)


> dput(df1)
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), zip1 = structure(c(5L, 
1L, 8L, 3L, 7L, 9L, 2L, 6L, 10L, 4L), .Label = c("16667", "21922", 
"29100", "54398", "55447", "72607", "84667", "96562", "97012", 
"99125"), class = "factor"), zip2 = structure(c(7L, 4L, 2L, 6L, 
3L, 8L, 5L, 9L, 1L, 10L), .Label = c("0451", "0644", "14040", 
"17184", "18838", "42308", "48507", "50496", "64851", "89748"
), class = "factor"), var1 = c(94.4, 39.6, 47.1, 74, 67.3, 53.4, 
28.7, 91.6, 47.1, 44.8), Sales = c(6394.03, 5575.65, 773.58, 
3181.43, 4992.56, 6627.01, 3313.13, 1585.1, 5080.98, 499.86)), .Names = c("id", 
"zip1", "zip2", "var1", "Sales"), row.names = c(NA, -10L), class = "data.frame")

and looks like this:

    > df1
   id  zip1  zip2 var1   Sales
1   1 55447 48507 94.4 6394.03
2   2 16667 17184 39.6 5575.65
3   3 96562  0644 47.1  773.58
4   4 29100 42308 74.0 3181.43
5   5 84667 14040 67.3 4992.56
6   6 97012 50496 53.4 6627.01
7   7 21922 18838 28.7 3313.13
8   8 72607 64851 91.6 1585.10
9   9 99125  0451 47.1 5080.98
10 10 54398 89748 44.8  499.86

I’d like to do something like:

df2 <- model.matrix(~df1[,-c(1,5)])
glmnet1 <- cv.glmnet(df2, df1[,"Sales"]
    , family="gaussian", alpha=.95, nfolds=10, standardize = FALSE, type.measure="deviance")

However I can’t get the model.matrix to take more than one variable:

> f1 <- formula(df1$Sales ~ df1[,c("zip1","zip2")])
> df2 <- model.matrix(f1)
  Error in model.frame.default(object, data, xlev = xlev) : 
  invalid type (list) for variable 'df1[, c("zip1", "zip2")]'

Any suggestions?

EDITS:

> f1 <- formula(Sales ~., data= df1[,-c(1,5)])
> df2 <- model.matrix(f1)
Error in terms.formula(object) : '.' in formula and no 'data' argument
  • 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-15T12:35:25+00:00Added an answer on June 15, 2026 at 12:35 pm

    You can specify multiple variables to your formula like:

    model.matrix(Sales ~ zip1 + zip2, data=df1)
    

    If you want to save typing things out, try:

    formdf1 <- as.formula(paste("Sales ~ ", paste(names(df1)[2:3],collapse="+")))
    formdf1
    #Sales ~ zip1 + zip2
    

    and then run:

    model.matrix(formdf1,data=df1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I

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.