notes.ludic.tech

This Week in R Learning

Factors and lev­els #

If you’re not care­ful you can just re­name the lev­els of a fac­tor rather than re­order­ing them #

I did:

levels(df$factor) <- levels(df$factor)[c(5,4,1,3,2)]

or sim­i­lar and then con­tin­ued mer­rily on with my analy­sis. You can even check to see that your reordering” has worked!:

levels(df$factor)

Everything looks fine and the lev­els are in the right or­der! Except… you haven’t ac­tu­ally re­ordered the lev­els. It just looks like you have. You have ac­tu­ally just re-al­lo­cated the names of the lev­els. And your sur­vey data that was orig­i­nally tagged Very un­happy” might now be tagged Very happy”…

How to do it prop­erly #

df$factor <- factor(df$factor, levels(df$factor)[c(5,4,3,2,1)])
Subtle dif­fer­ence… but rather im­por­tant!

PS
A per­haps bet­ter way of do­ing it is:

opinion_levels <- c(
"Really unhappy",
"Slightly unhappy",
"Neutral",
"Slightly happy",
"Really happy")

phe_data_opinions$channel_switch_sentiment <- factor(phe_data_opinions$channel_switch_sentiment,
levels = opinion_levels)

You then have opinion_levels as an ob­ject to use again if you need it.

Then won­der­ing if

phe_data_opinions$channel_switch_sentiment %<>% factor(.,
levels = opinion_levels)

might work as a slightly more con­cise ver­sion of the above? Using {magrittr}’s as­sign­ment pipe (light­bulb mo­ment!)

⇖ home