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

  • Home
  • SEARCH
  • 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 8326717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:51:30+00:00 2026-06-09T00:51:30+00:00

I have a dataframe which contains both numeric variables and factors. When moving data

  • 0

I have a dataframe which contains both numeric variables and factors.

When moving data from one dataframe to another, everything is kept as I would like it:

copy_data<-as.data.frame(original_data)

This creates a copy of ‘original_data’ with factors remaining factors.

When I try a more complex version, the end result is a dataframe of numeric values, when I want the factors to still be factors:

model_data<-with(subset(copy_data, copy_data$var1<0), 
as.data.frame(cbind(var1, var2, var3, factor1, factor2, factor3)))

So factor1, factor2, and factor3 all end up numeric rather than factors. What am I missing? I’ve tried with and without as.data.frame and defining model_data as a dataframe before populating it.

My searches of the StackExchange archive return mostly results about deliberately changing factors to variables, and haven’t help me much. The slightly clunky title isto differentiate my question from those.

  • 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-09T00:51:31+00:00Added an answer on June 9, 2026 at 12:51 am

    ?cbind says that cbind returns a matrix if all the inputs are vectors (which they are in your case). A a matrix can can only contain a single atomic type (character, numeric, logical, etc.). Factors are not an atomic type, so they get converted.

    The “Data frame methods” section says that cbind data.frame method just wraps data.frame(..., check.names=FALSE), so you could just call data.frame directly (the call to cbind is redundant).

    model_data <- with(subset(copy_data, copy_data$var1<0), 
      data.frame(var1, var2, var3, factor1, factor2, factor3))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with two qualitative variables (Q1, Q2) which are both
Let's assume that we have a data frame x which contains the columns job
I have a data.frame (which I melted using the melt function), from which I
I have a dataframe in R that I loaded from a CSV file. One
I have a large file which contains lots of data, and I'd like to
I have a DataFrame with a few columns. One columns contains a symbol for
I have a small dataframe from which I am plotting 3 columns in order
Possible Duplicate: removing specific rows from a dataframe Let's say I have a data
I have a data.frame which has multiple columns. One of the columns is time
I'd like to add a new column to my data.table, which contains data from

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.