Base R’s stringsAsFactors
default setting is supposedly the most often complained about piece of code in the whole R infrastructure. A search through the source code of all CRAN packages in December 2015 (Link) resulted in 3,492 mentions of stringsAsFactors
. Most of the time these explicit mentions where found within calls to data.frame()
or as.data.frame()
and simply set the value to FALSE
.
The hellno package provides an explicit solution to the problem without changing R itself or having to mess around with options. One could use e.g.: options("stringsAsFactors" = FALSE)
to re-set the global default behavior. Instead hellno tackles the problem from another direction, namely by providing alternative implementations of data.frame()
and as.data.frame()
. Those re-implementations are in fact simple wrappers around base R’s very own data.frame()
and as.data.frame()
with stringAsFactors
option set to HELLNO
– which in turn equals to FALSE
and gives the package its name.
Some info material and crediting for ‘hellno’ as catch phrase – thanks Clint?:
Using the package is simple – load it, note the message indicating masking two base functions and code on – from now on no type conversion will take place within data.frame()
and as.data.frame()
:
# options(repos = c(CRAN = "https://cran.rstudio.com"))
# install.packages("hellno")
library(hellno)
##
## Attaching package: 'hellno'
##
## Die folgenden Objekte sind maskiert von 'package:base':
##
## as.data.frame, data.frame
df2
## [1] "character"
While using hellno in interactive R is nice, in fact its real strength is that it can be imported when writing packages. Once imported stringsAsFactors=FALSE
will be the default for all uses of data.frame()
and as.data.frame()
within all package functions BUT NOT OUTSIDE OF IT.
Thus it provides a way to ease programming while also ensuring that package users can still choose which flavor of stringsAsFactors
they like best.
Let us see how this works …read more
Source:: r-bloggers.com