R

Asked • 10/17/19

Reorder levels of a factor without changing order of values?

I have data frame with some numerical variables and some categorical `factor` variables. The order of levels for those factors is not the way I want them to be. numbers <- 1:4 letters <- factor(c("a", "b", "c", "d")) df <- data.frame(numbers, letters) df # numbers letters # 1 1 a # 2 2 b # 3 3 c # 4 4 dIf I change the order of the levels, the letters no longer are with their corresponding numbers (my data is total nonsense from this point on). levels(df$letters) <- c("d", "c", "b", "a") df # numbers letters # 1 1 d # 2 2 c # 3 3 b # 4 4 aI simply want to change the _level_ order, so when plotting, the bars are shown in the desired order - which may differ from default alphabetical order.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.