Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable stringsAsFactors=TRUE in data.frame permanently?

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.

like image 430
lokheart Avatar asked Nov 18 '11 04:11

lokheart


People also ask

What is stringsAsFactors true?

The argument 'stringsAsFactors' is an argument to the 'data. frame()' function in R. It is a logical that indicates whether strings in a data frame should be treated as factor variables or as just plain strings. The argument also appears in 'read.

What is stringsAsFactors false?

Please note that stringsAsFactors = FALSE is the default specification of the data. frame function, in case you are using R version 4.0 or newer. In older versions, the default specification was stringsAsFactors = TRUE. logical: should character vectors be converted to factors?


1 Answers

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.

like image 77
joran Avatar answered Sep 29 '22 05:09

joran