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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:51:22+00:00 2026-06-15T23:51:22+00:00

I would like to convert my dataframe into a matrix that expands a single

  • 0

I would like to convert my dataframe into a matrix that expands a single factor column into multiple ones and assigns a 1/0 depending on the factor. For example

C1 C2 C3
A  3  5
B  3  4
A  1  1

Should turn into something like

C1_A C1_B C2 C3
1      0  3  5
0      1  3  4
1      0  1  1

How can I do this in R? I tried data.matrix, as.matrix which did not return what I wanted. They assign an “integer” value to a single factor column, there is no expansion.

  • 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-15T23:51:23+00:00Added an answer on June 15, 2026 at 11:51 pm

    Assuming dat is your data frame:

    cbind(dat, model.matrix( ~ 0 + C1, dat))
    
      C1 C2 C3 C1A C1B
    1  A  3  5   1   0
    2  B  3  4   0   1
    3  A  1  1   1   0
    

    This solution works with any number of factor levels and without manually specifying column names.

    If you want to exclude the column C1, you could use this command:

    cbind(dat[-1], model.matrix( ~ 0 + C1, dat))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column in a pandas DataFrame that I would like to split
I would like to convert a string that I pass in a function into
I would like to convert a vector into a transitions matrix first (which I
I would like convert the below text into a nested array, something like you
I would like to convert the following string into an array/nested array and iterate
I would like to convert this piece of logic with purely matrix operations instead
I would like to convert the below foreach statement to a LINQ query that
I would like to convert an Int32 in the range 0-15 into a the
I would like to convert this /bin/sh syntax into a widely compatible Windows batch
I would like to convert a list of User's properties into strings array (for

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.