0
$\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$
  • $\begingroup$ Character is a string form while factor is a nominal label. $\endgroup$ – user19196 Jun 1 '16 at 18:21
0
$\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$
  • $\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 Jun 1 '16 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$ – Eric Lecoutre Jun 2 '16 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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