简体   繁体   English

如何操作 R 中数据框的因子级别?

[英]How to manipulate factor levels of a data frame in R?

I have in a data frame (Vitamin_C) one column (size) which only has two factor levels : "(1,20]" and "(20,35]". I want to change "(1,20]" to "reduce" and "(20,35]" to "normal".我在数据框 (Vitamin_C) 中有一个列(大小),它只有两个因子级别:“(1,20]”和“(20,35]”。我想将“(1,20]”更改为“减少”和“(20,35]”到“正常”。

I tried as follow:我试过如下:

Vitamin_C$size[Vitamin_C$size== "(1,20]"]<-c("Reduced")

Vitamin_C$size[Vitamin_C$size== "(20,35]"]<-c("Normal")

Error message:错误信息:

Warning message:
In `[<-.factor`(`*tmp*`, Vitamin_C$size == "(1,20]", value = c(NA,  :
  invalid factor level, NA generated

With base R, use factor() :使用基础 R,使用factor()

Vitamin_C$size = factor(
  Vitamin_C$size,
  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.正如评论中提到的,如果你使用cut创建这个因子,你可以直接在cut命令中指定你想要的标签,这是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM