R语言 带有三个选项的ifelse语句的问题

kdfy810k  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(117)

我在转换一个变量时遇到了麻烦。
我有一个名为Originating_body的变量(目前是一个因素,但我也尝试了.character,但没有用),它由13种类型组成,我想减少到3种类型,称为origin. simple。
| 发起机构| origin.simple |
| --|--|
| “法庭(第二分庭)”|室|
| “法庭(第二分庭)”|室|
| “法庭(第二分庭)”|室|
| “法庭(第四分庭)”|室|
| “法庭(第三庭)”|室|
| “法院(第一组委员会)”|委员会|
| “法院(第三部分委员会)”|委员会|
| “法院(第五组委员会)”|委员会|
| “法院(分庭)”|室|
| “法院(全体会议)”|大法庭|
| “法院(大法庭)”|大法庭|
变量转换的完整列表是:
新类别:由“法院(大分庭)”“法院(全体会议)”组成的“大分庭”)
新类别“分庭”,由“法庭(分庭)”“法庭(第五分庭)”“法庭(第一分庭)”“法庭(第四分庭)”“法庭(第二分庭)”“法庭(第三分庭)”组成
新类别“委员会”由以下人士组成:“法院第五组委员会”“法院(第一组委员会)”“法院(第四组委员会)”“法院(第二组委员会)”“法院(第三组委员会)”
没有缺失值
示例数据:

df<-data.frame(Originating_body) 
Originating_body <- c("Court (Fourth Section)", "Court (Fourth Section)", "Court (Fourth Section)", 
                    "Court (Third Section)", "Court (Grand Chamber)", "Court (Second Section)", 
                    "Court (Second Section)", "Court (Third Section)", "Court (Third Section)", 
                    "Court (Fourth Section Committee)", "Court (Chamber)", "Court (Second Section)", 
                    "Court (Third Section)", "Court (Third Section)", "Court (Second Section)", 
                    "Court (Second Section)", "Court (Second Section)", "Court (Second Section)", 
                    "Court (Third Section)", "Court (Third Section)", "Court (Third Section)", 
                    "Court (Second Section)", "Court (Grand Chamber)", "Court (First Section)", 
                    "Court (Plenary)", "Court (First Section)", "Court (Third Section)", 
                    "Court (Second Section)", "Court (First Section)", "Court (First Section)", 
                    "Court (First Section)", "Court (First Section)", "Court (First Section)", 
                    "Court (First Section)", "Court (Fifth Section)", "Court (Fifth Section)", 
                    "Court (Fifth Section)", "Court (Grand Chamber)", "Court (First Section)", 
                    "Court (First Section)", "Court (Third Section)", "Court (Third Section)", 
                    "Court (Second Section Committee)", "Court (Second Section)", "Court (Second Section)", 
                    "Court (Second Section)", "Court (Fourth Section)", "Court (Third Section)", 
                    "Court (Fourth Section)", "Court (Fourth Section)", "Court (Third Section)", 
                    "Court (Second Section)", "Court (Second Section)", "Court (Second Section)", 
                    "Court (Second Section)", "Court (Second Section)", "Court (Second Section)", 
                    "Court (Third Section)", "Court (Second Section)", "Court (Second Section)", 
                    "Court (Second Section)", "Court (Second Section)", "Court (First Section)", 
                    "Court (First Section)", "Court (First Section)", "Court (First Section)", 
                    "Court (First Section Committee)", "Court (First Section)", "Court (Third Section)", 
                    "Court (First Section)", "Court (Third Section)", "Court (Second Section)", 
                    "Court (Third Section)", "Court (Third Section)", "Court (Third Section)", 
                    "Court (Second Section)", "Court (Fifth Section)", "Court (Third Section)", 
                    "Court (Fifth Section)", "Court (Third Section)", "Court (Fifth Section)", 
                    "Court (Third Section)", "Court (Third Section Committee)", "Court (Third Section)", 
                    "Court (Third Section)", "Court (Fifth Section)", "Court (Fifth Section Committee)", 
                    "Court (First Section)", "Court (First Section)", "Court (First Section)", 
                    "Court (Second Section)", "Court (Second Section)", "Court (First Section)", 
                    "Court (First Section)", "Court (First Section)", "Court (First Section)", 
                    "Court (First Section)", "Court (First Section Committee)", "Court (First Section)", 
                    "Court (First Section)", "Court (First Section)", "Court (First Section)", 
                    "Court (First Section)", "Court (First Section)", "Court (First Section)", 
                    "Court (First Section)", "Court (Third Section)", "Court (Third Section)", 
                    "Court (Third Section)", "Court (Second Section)", "Court (Fourth Section Committee)", 
                    "Court (Third Section)", "Court (Third Section)", "Court (Third Section)", 
                    "Court (Second Section)", "Court (Second Section)", "Court (Second Section)", 
                    "Court (Second Section)", "Court (Second Section Committee)", "Court (Second Section)", 
                    "Court (Second Section)", "Court (Second Section)", "Court (Second Section)")

字符串
我试着用ifelse语句来做,

df$origin.simple <- ifelse(df$Originating_body =="Court (Grand Chamber)"|
                         df$Originating_body == "Court (Plenary)", "Grand Chamber", 
                ifelse(df$Originating_body =="Court(Fifth Section)"|
                          df$Originating_body == "Court(Fourth Section)"|
                          df$Originating_body == "Court(Third Section)"|
                          df$Originating_body == "Court(Second Section)"|
                          df$Originating_body == "Court(First Section)"|
                          df$Originating_body == "Court(Chamber)", "Chamber", "Committee"))


但是出了问题,“Grand Chamber”似乎是正确的,但是我在我想要“Chamber”的地方结束了“Committee”,所以R忽略了ifelse语句的第二部分,并将“Court(Fifth Section)",“Court(Fourth Section)",“Court(Third Section)",“Court(Second Section)",“Court(First Section)”和“Court(Chamber)”转换为“Committee”而不是“Court”。
我确信我犯了一个明显的错误,但我似乎找不到它。我也尝试使用grepl(ifelse())而不是ifelse(),但没有任何成功。非常感谢帮助,提前感谢。

ecbunoof

ecbunoof1#

将您的数据框复制到对象df

df <- read.table("clipboard", sep = "\t", header = TRUE)

字符串
准备分类,请注意,这里的错别字可能是问题的主要来源:

GrandChamber <- c("Court (Grand Chamber)", 
                  "Court (Plenary)")

Chamber <- c("Court (Chamber)", 
             "Court (Fifth Section)", 
             "Court (First Section)", 
             "Court (Fourth Section)", 
             "Court (Second Section)", 
             "Court (Third Section)")

Committee <- c("Court (Fifth Section Committee)",# fixed missing brace
               "Court (First Section Committee)",
               "Court (Fourth Section Committee)", # fixed missing brace
               "Court (Second Section Committee)", # fixed missing brace
               "Court (Third Section Committee)") # fixed missing brace


嵌套ifelse()版本:

df$new <- ifelse(df$Originating_body %in% GrandChamber, "Grand Chamber",
                 ifelse(df$Originating_body %in% Chamber, "Chamber",
                 ifelse(df$Originating_body %in% Committee,"Committee", "ERROR")))


我使用dplyrcase_when()的首选方法:

df$new <- dplyr::case_when(df$Originating_body %in% GrandChamber ~ "Grand Chamber",
                         df$Originating_body %in% Chamber ~ "Chamber",
                         df$Originating_body %in% Committee ~ "Committee"
                 )


使用命名向量和子集(感谢@Onyambu):

lookups <- c("Court (Grand Chamber)" = 'Grand Chamber', 
             "Court (Plenary)" = 'Grand Chamber',
             "Court (Chamber)" = 'Chamber',
             "Court (Fifth Section)"= 'Chamber', 
             "Court (First Section)"= 'Chamber', 
             "Court (Fourth Section)"= 'Chamber', 
             "Court (Second Section)"= 'Chamber', 
             "Court (Third Section)"= 'Chamber',
             "Court (Fifth Section Committee)" = 'Committee',
             "Court (First Section Committee)" = 'Committee',
             "Court (Fourth Section Committee)" = 'Committee',
             "Court (Second Section Committee)" = 'Committee',
             "Court (Third Section Committee)" = 'Committee')

df$new <- lookups[df$Originating_body]


每种方法都给出

Originating_body origin.simple           new
1           Court (Second Section)       Chamber       Chamber
2           Court (Second Section)       Chamber       Chamber
3           Court (Second Section)       Chamber       Chamber
4           Court (Fourth Section)       Chamber       Chamber
5            Court (Third Section)       Chamber       Chamber
6  Court (First Section Committee)     Committee     Committee
7  Court (Third Section Committee)     Committee     Committee
8  Court (Fifth Section Committee)     Committee     Committee
9                  Court (Chamber)       Chamber       Chamber
10                 Court (Plenary) Grand Chamber Grand Chamber
11           Court (Grand Chamber) Grand Chamber Grand Chamber
4si2a6ki

4si2a6ki2#

这是相当容易使用的字典,你显然已经做了。

> transform(dat, 
+           ob_new=dict$origin.simple[match(dat$Originating_body, 
+                                           dict$Originating_body)])
                  Originating_body        ob_new
1            Court (First Section)       Chamber
2            Court (First Section)       Chamber
3            Court (Third Section)       Chamber
4            Court (Fifth Section)       Chamber
5           Court (Second Section)       Chamber
6  Court (First Section Committee)     Committee
7            Court (First Section)       Chamber
8            Court (Third Section)       Chamber
9            Court (Third Section)       Chamber
10           Court (First Section)       Chamber
11          Court (Second Section)       Chamber
12           Court (Grand Chamber) Grand Chamber
13           Court (Third Section)       Chamber
14           Court (Third Section)       Chamber
15           Court (Third Section)       Chamber

字符串

  • 数据类型:*
> dput(dat)
structure(list(Originating_body = c("Court (First Section)", 
"Court (First Section)", "Court (Third Section)", "Court (Fifth Section)", 
"Court (Second Section)", "Court (First Section Committee)", 
"Court (First Section)", "Court (Third Section)", "Court (Third Section)", 
"Court (First Section)", "Court (Second Section)", "Court (Grand Chamber)", 
"Court (Third Section)", "Court (Third Section)", "Court (Third Section)"
)), row.names = c(NA, -15L), class = "data.frame")
> dput(dict)
structure(list(Originating_body = c("Court (Second Section)", 
"Court (Second Section)", "Court (Second Section)", "Court (Fourth Section)", 
"Court (Third Section)", "Court (First Section Committee)", "Court (Third Section Committee)", 
"Court (Fifth Section Committee)", "Court (Chamber)", "Court (Plenary)", 
"Court (Grand Chamber)", "Court (Fifth Section)", "Court (First Section)", 
"Court (Fourth Section)", "Court (Second Section)", "Court (Third Section)"
), origin.simple = c("Chamber", "Chamber", "Chamber", "Chamber", 
"Chamber", "Committee", "Committee", "Committee", "Chamber", 
"Grand Chamber", "Grand Chamber", "Chamber", "Chamber", "Chamber", 
"Chamber", "Chamber")), class = "data.frame", row.names = c(NA, 
-16L))

相关问题