我有一个字段,其中的级别细分如下:
levels(demo$age)
"18 to 24 years old" "25 to 34 years old" "35 to 44 years old" "45 to 54 years old" "55 to 64 years old" "65 to 74 years old" "75 years old or older"
如何将等级更改为
"Total " "18 to 24 years old" "25 plus".
答案 0 :(得分:0)
我们创建了需要更改的vector
levels
v1 <- c("25 to 34 years old", "35 to 44 years old", "45 to 54 years old",
"55 to 64 years old", "65 to 74 years old" , "75 years old or older")
然后,将这些分配给新级别
levels(demo$age)[levels(demo$age) %in% v1] <- "25 plus"
如果我们需要总计&#39;等级
levels(demo$age) <- c("Total", levels(demo$age))
levels(demo$age)
#[1] "Total" "18 to 24 years old" "25 plus"
set.seed(24)
demo <- data.frame(age = sample(c("18 to 24 years old", v1), 100, replace = TRUE))