39

See title. Frankly I am a bit sick of manually doing the adjustment all the time.

This should be a simple question, but I just can't figure out how to fix it. Thanks.

Wouter Thielen
  • 1,016
  • 9
  • 21
lokheart
  • 23,743
  • 39
  • 98
  • 169

1 Answers1

59

Set options(stringsAsFactors = FALSE) at the beginning of your R session, or in your .RProfile.

As the comments below may suggest, stringsAsFactors is a bit of a controversial topic within the R community. How irritating you find this default value may depend somewhat on how much time you spend using R to fit many "standard" statistical models (lm, glm, etc). Many of those model fitting and related functions are built around using the factor data type.

If you spend most of your time doing other more "generic" types of data analysis, you might find this default more irritating.

It is widely considered dangerous to globally set stringsAsFactors = FALSE for the reasons mentioned below: it can cause significant confusion when sharing code. Indeed, even if you work mainly alone, participating in online communities like StackOverflow can be tricky if you insist on running R with stringsAsFactors = FALSE: your answer to a question may not work for the OP, or you may not be able to replicate errors others are seeing!

Of course, everyone can make their own choices about how best to manage these risks for themselves.

joran
  • 169,992
  • 32
  • 429
  • 468
  • 25
    Having this set in your `.Rprofile` can be a bit risky, as your code is no longer portable. Since I do a lot of teaching, I decided that it would be too easy to forget about this option when passing on code. – csgillespie Nov 18 '11 at 09:08
  • 15
    it really should be default. kills my productivity when extremely strange, hard to debug bugs occur because of it. just now i read plotting parameters from a file, forgot it, and the plots were wrong while still displaying the right title and axis descriptions… – flying sheep Mar 26 '14 at 18:26
  • 2
    it should be default in that conversion such as as.data.frame() should maintain the underlying data structure of the objects they are inserting into dataframe. – Three Diag Apr 30 '16 at 18:12
  • 3
    As of `R 4.0.0` `stringsAsFactors = FALSE` is the default. (Finally!). – Ritchie Sacramento Apr 24 '20 at 12:09