I have been reading about stringAsFactors and how despite the default being TRUE, in many instances you have to set it to FALSE for certain commands. From what I understand:

  • Factors are the default way in which R stores text so if you have a df with a column containing text, R will (always?) convert it to factors when you import your data.

  • If you are intending to do statistical tests (regression etc) using this particular text column, then this is fine, because many statistical functions (like lm() or glm() ) will require for you to have your text variables set as factors, so that it can treat them as categorical data.

  • However, if you are intending to do things other than statistical testing (i.e. manipulating your df, merging it etc), then you might want to set stringAsFactors to FALSE because some functions could give you errors if you have your text variables set as factors.

Is this correct? I think I understand the main concept of stringAsFactors, but I was wondering if there was any "rule" that people use? In other words, when you are starting a new script, it is "normal" not to be entirely sure whether you should set stringAsFactors to TRUE or FALSE? Is there a list of (non-statistical) packages that are particularly known for wanting stringAsFactors to be set to FALSE?

In other words, is it considered "good practise" to set stringAsFactors to FALSE, if one is fairly sure you are not going to use a statistical function on your data?

I appreciate my question is quite vague, but I was just wondering if there was some kind of "trick"/"rule" that people use, when "deciding" whether to set the parameter to TRUE or FALSE. Thanks

1

1 Answer

There was one major point you omitted from your discussion, which all looks completely accurate and correct to me. One reason why factors were created is that they enable a potentially massive reduction in the amount of storage space required for a variable. Consider, for example a column of a data frame with very low cardinality (uniqueness of values). When storing this information as character data, it would require whatever storage is needed to store each string, across the entire column. However, with factors, the storage requirement is massively reduced. As a factor, R only needs to store the actual string values once, and can then represent all the values using numerical levels. And, in the case of a column with low cardinality, only a few strings would actually need to be stored, as compared to the large size of the column.

Given that R is mainly an in-memory tool, and memory is precious, factors represent a good opportunity to optimize any R script. Speaking purely from the point of view of storage/memory, leaving stringsAsFactors set to TRUE would make good sense. Obviously, if you have an API or need which requires otherwise, then you need to make a decision.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.