我在转换一个变量时遇到了麻烦。
我有一个名为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(),但没有任何成功。非常感谢帮助,提前感谢。
2条答案
按热度按时间ecbunoof1#
将您的数据框复制到对象
df
:字符串
准备分类,请注意,这里的错别字可能是问题的主要来源:
型
嵌套
ifelse()
版本:型
我使用
dplyr
和case_when()
的首选方法:型
使用命名向量和子集(感谢@Onyambu):
型
每种方法都给出
4si2a6ki2#
这是相当容易使用的字典,你显然已经做了。
字符串
型