Silvi
Silvi

Reputation: 25

How to "relevel" the levels in a column of a data.frame in R?

I have a similar question as to Relevel a factor:

I have a data.frame, df, consisting of n=200 row entires for df$id, df$time, df$age where 21 individual id are sampled numerous times. Current levels of df$id is:

> levels(df$id)

 [1] "boot" "clus" "cokl" "embr" "julp" "lanc" "lole" "mdrk" "mnst" "mrti"
[11] "omly" "puff" "rmmy" "savy" "smag" "spar" "sqrk" "tbmn" "thls" "vin" 
[21] "wino"

However,I would like to order of this to instead be:

> level.order$id
 [1] lanc mnst clus lole puff smag boot savy thls omly julp mrti vin  embr
[15] spar sqrk wino rmmy cokl mdrk tbmn

I understand there are a lot of similar inquiries about this, but I cannot figure it out with the examples at hand...

Upvotes: 1

Views: 2379

Answers (1)

Jake Bernards
Jake Bernards

Reputation: 95

You can reorder the levels by using the factor function rather than the relevel function.

df$id <- factor(df$id, levels = level.order$id)

Upvotes: 1

Related Questions