4
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Character is a string form while factor is a nominal label. $\endgroup$
    – user19196
    Commented Jun 1, 2016 at 18:21

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – lupi5
    Commented Jun 1, 2016 at 17:24
  • $\begingroup$ 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(). $\endgroup$ Commented Jun 2, 2016 at 6:31
  • $\begingroup$ 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). $\endgroup$
    – Prince M
    Commented Mar 19, 2020 at 22:12
0
$\begingroup$

A few thoughts on the question above:

  • I find this link about Factors in R very useful.
  • If you want to create a classification model or if you like to convert the character to numeric you have to convert the character to a factor first: as.numeric(as.factor(name)). In your case that could be named with more or less than 4 letters or names starting with a specific letter.
  • As mentioned before, converting the character to a factor saves memory!

Happy coding!

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.