[英]Why is levels() in R not assigning the wrong level to my data?
我正在創建一個 function ,它要求用戶上傳具有特定字符向量的數據集。 在引擎蓋下,我需要一個具有矢量保持字符的列,但我還需要一個相同的單獨列,除了它是具有特定級別的因素。
當我嘗試使用 levels() 分配級別時,我假設 R 會匹配字符串,但它是隨機分配級別的順序。 我該如何糾正這種行為? 雖然具體的字符值總是一樣的,但我不知道用戶上傳它們的順序。
#Data to recreate the issue (note: The group and count columns are not relevant, but I kept them in case they may be related to the issue for some reason)
library(dplyr)
data <- tibble(group = factor(c(rep("A", 10), rep("B", 10), rep("C", 10), rep("D", 10)), levels = c("A", "B", "C", "D")),
state = c(rep(c("Not Started", "Just Beginning",
"25% Complete", "40% Complete", "Halfway Done",
"75% Complete", "Mostly Done", "Completed",
"Follow Up", "Final Follow Up"), 4)),
count = c(100, 5, 4, 445, 67, 44, 25, 877, 240, 353,
48, 51, 48, 40, 141, 34, 50, 45, 34, 35,
140, 5, 8, 0, 17, 42, 0, 5, 3, 75,
477, 20, 59, 13, 1065, 1, 50, 353, 73, 104))
data$state_factor <- as.factor(data$state)
levels(data$state_factor) <- c("Not Started", "Just Beginning",
"25% Complete", "40% Complete", "Halfway Done",
"75% Complete", "Mostly Done", "Completed",
"Follow Up", "Final Follow Up")
head(data, 20) #Note how the state and state_factor columns are not identical
我很靈活,我可以如何實現這一點(即,我缺少的 forcats 中是否有 function?),但它需要在這些訂單中有這些級別。
更新:
好的,那么您可以使用factor
而不是as.factor
並直接設置級別:
data$state_factor <- factor(data$state, levels=c("Not Started", "Just Beginning",
"25% Complete", "40% Complete", "Halfway Done",
"75% Complete", "Mostly Done", "Completed",
"Follow Up", "Final Follow Up"))
Output:
> head(data, 20)
# A tibble: 20 × 4
group state count state_factor
<fct> <chr> <dbl> <fct>
1 A Not Started 100 Not Started
2 A Just Beginning 5 Just Beginning
3 A 25% Complete 4 25% Complete
4 A 40% Complete 445 40% Complete
5 A Halfway Done 67 Halfway Done
6 A 75% Complete 44 75% Complete
7 A Mostly Done 25 Mostly Done
8 A Completed 877 Completed
9 A Follow Up 240 Follow Up
10 A Final Follow Up 353 Final Follow Up
11 B Not Started 48 Not Started
12 B Just Beginning 51 Just Beginning
13 B 25% Complete 48 25% Complete
14 B 40% Complete 40 40% Complete
15 B Halfway Done 141 Halfway Done
16 B 75% Complete 34 75% Complete
17 B Mostly Done 50 Mostly Done
18 B Completed 45 Completed
19 B Follow Up 34 Follow Up
20 B Final Follow Up 35 Final Follow Up
現在它們不是按字母順序排列的:
> levels(data$state_factor)
[1] "Not Started" "Just Beginning" "25% Complete" "40% Complete" "Halfway Done" "75% Complete" "Mostly Done" "Completed"
[9] "Follow Up" "Final Follow Up"
試試下面的。
library(dplyr)
data <- tibble(group = factor(c(rep("A", 10), rep("B", 10), rep("C", 10), rep("D", 10)), levels = c("A", "B", "C", "D")),
state = c(rep(c("Not Started", "Just Beginning",
"25% Complete", "40% Complete", "Halfway Done",
"75% Complete", "Mostly Done", "Completed",
"Follow Up", "Final Follow Up"), 4)),
count = c(100, 5, 4, 445, 67, 44, 25, 877, 240, 353,
48, 51, 48, 40, 141, 34, 50, 45, 34, 35,
140, 5, 8, 0, 17, 42, 0, 5, 3, 75,
477, 20, 59, 13, 1065, 1, 50, 353, 73, 104))
data$state_factor <- factor(data$state, levels = c("Not Started", "Just Beginning",
"25% Complete", "40% Complete", "Halfway Done",
"75% Complete", "Mostly Done", "Completed",
"Follow Up", "Final Follow Up"))
head(data, 20)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.