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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:12:37+00:00 2026-05-27T20:12:37+00:00

I have some data: transaction <- c(1,2,3); date <- c(2010-01-31,2010-02-28,2010-03-31); type <- c(debit, debit,

  • 0

I have some data:

transaction <- c(1,2,3);
date <- c("2010-01-31","2010-02-28","2010-03-31");
type <- c("debit", "debit", "credit");
amount <- c(-500, -1000.97, 12500.81);
oldbalance <- c(5000, 4500, 17000.81)
evolution <- data.frame(transaction, date, type, amount, oldbalance, row.names=transaction,  stringsAsFactors=FALSE);
evolution$date <- as.Date(evolution$date, "%Y-%m-%d");
evolution <- transform(evolution, newbalance = oldbalance + amount);
evolution

If I enter the command:

type <- factor(type) 

where type is nominal (categorical) variable,then what difference does it make to my data?

Thanks

  • 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-05-27T20:12:38+00:00Added an answer on May 27, 2026 at 8:12 pm

    Factors vs character vectors when doing stats:
    In terms of doing statistics, there’s no difference in how R treats factors and character vectors. In fact, its often easier to leave factor variables as character vectors.

    If you do a regression or ANOVA with lm() with a character vector as a categorical
    variable you’ll get normal model output but with the message:

    Warning message:
    In model.matrix.default(mt, mf, contrasts) :
      variable 'character_x' converted to a factor
    

    Factors vs character vectors when manipulating dataframes:
    When manipulating dataframes, however, character vectors and factors are treated very differently. Some information on the annoyances of R & factors can be found on the Quantum Forest blog, R pitfall #3: friggin’ factors.

    Its useful to use stringsAsFactors = FALSE when reading data in from a .csv or .txt using read.table or read.csv. As noted in another reply you have to make sure that everything in your character vector is consistent, or else every typo will be designated as a different factor. You can use the function gsub() to fix typos.

    Here is a worked example showing how lm() gives you the same results with
    a character vector and a factor.

    A random independent variable:

    continuous_x <- rnorm(10,10,3)
    

    A random categorical variable as a character vector:

    character_x  <- (rep(c("dog","cat"),5))
    

    Convert the character vector to a factor variable.
    factor_x <- as.factor(character_x)

    Give the two categories random values:

    character_x_value <- ifelse(character_x == "dog", 5*rnorm(1,0,1), rnorm(1,0,2))
    

    Create a random relationship between the indepdent variables and a dependent variable

    continuous_y <- continuous_x*10*rnorm(1,0) + character_x_value
    

    Compare the output of a linear model with the factor variable and the character
    vector. Note the warning that is given with the character vector.

    summary(lm(continuous_y ~ continuous_x + factor_x))
    summary(lm(continuous_y ~ continuous_x + character_x))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data: transaction <- c(1,2,3); date <- c(2010-01-31,2010-02-28,2010-03-31); type <- c(debit,
My scenario is that I have some payment transaction data in MySQL and some
I have some data for my sites statistics in a SQL DB: date: visits:
I have some data structures that stay unchanged if you try to add something
i have some data to write in data base (three strings); i have a
I have some data with messed-up accented characters. For example in the data we
I have some data grouped in a table by a certain criteria, and for
I have some data of the form Key ID Link 1 MASTER 123 2
I have some data. I want to go through that data and change cells
I have some data that I want to store somewhere in my Rails app

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.