rr-factor

Why do my characters transform when I append to a vector?


I'm learning R right now, and realised when I append to a factor it becomes a character. Even when I don't specify factor level this happens. When I look at the order of the numbers, I see that they correspond to the "factor levels" e.g., small is 1. But, I'm wondering why this happens when using this method of appending to vectors.

size_order <- factor(c("small", "medium", "large", "medium", "small", "small"), levels = c("small", "medium", "large"))

size order
> [1] small  medium large  medium small  small 
> Levels: small medium large huge

typeof(size_order)
> integer

But then when I append to it, it becomes a character:

size_order <- append(size_order, "small")

size order
> "1"     "2"     "3"     "2"     "1"     "1"     "small"

typeof(size_order)
> [1] "character"

Solution

  • The documentation for ?c states that:

    Combine Values into a Vector or List Description This is a generic function which combines its arguments.

    The default method combines its arguments to form a vector. All arguments are coerced to a common type which is the type of the returned value, and all attributes except names are removed.

    Notice that the all the attributes are stripped off. You are concatenating a factor and a character. A factor is internally stored as an integer. This will be coerced to a character in order to combine the two.

    To get the results you want, consider coercing the second string to factor before concatenation. ie

    size_order <- factor(c("small", "medium", "large", "medium", "small", "small"), levels = c("small", "medium", "large"))
    size_order <- c(size_order, factor('small'))
    

    Which is the same as:

    append(size_order, factor('small'))
    [1] small  medium large  medium small  small  small 
    Levels: small medium large