how do i manipulate factor levels of a data frame in r?

With base R, use factor():

Vitamin_C$size = factor(
  levels = c("(1,20]", "(20, 35]"),
  labels = c("Reduced", "Normal")

You could also directly modify the levels (make sure they start in the order you think they are!)

levels(Vitamin_C$size) = c("Reduced", "Normal")

As mentioned in comments, if you are creating this factor with cut, you can specify the labels you want directly in the cut command, which is best of all.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top