# When to choose character instead of factor in R?

I am currently working on a dataset which contains a name attribute, which stands for a person's first name. After reading the csv file with read.csv, the variable is a factor by default (stringsAsFactors=TRUE) with ~10k levels. Since name does not reflect any group membership, I am uncertain to leave it as factor.

Is it necessary to convert name to character? Are there some advantages in doing (or not doing) this? Does it even matter?

• Character is a string form while factor is a nominal label.
– user19196
Jun 1 '16 at 18:21

Factors are stored as numbers and a table of levels. If you have categorical data, storing it as a factor may save lots of memory.

For example, if you have a vector of length 1,000 stored as character and the strings are all 100 characters long, it will take about 100,000 bytes. If you store it as a factor, it will take about 8,000 bytes plus the sum of the lengths of the different factors.

Comparisons with factors should be quicker too because equality is tested by comparing the numbers, not the character values.

The advantage of keeping it as character comes when you want to add new items, since you are now changing the levels.

Store them as whatever makes the most sense for what the data represent. If name is not categorical, and it sounds like it isn't, then use character.

• Thank you for this helpful answer. The name attribute describes a person's first name. So I think, it makes more sense to convert it to character. For taking efficiency into account, maybe it is interesting, that the ratio between the unified vector's length and the original vector's length is approximately 0.5. Jun 1 '16 at 17:24
• I would also add that if/when the character value itself (string) is of interest to you, for instance if you want to perform substring or regular expressions, you should consider text to avoir multiply calls to as.character(). Jun 2 '16 at 6:31
• Does anyone have a reference for where I can read about this more in depth? Issues with this come up in my work frequently and I would like to understand the difference on a fundamental level. (I am not saying your answer isn't good enough, I might just not have the background for it to truly hit home for me). Mar 19 '20 at 22:12

A few thoughts on the question above: