基于不同组和不同子组的新列

时间:2018-09-27 08:56:05

标签: r dataframe

此问题是以下问题的扩展,因此,请参阅问题以获取数据和其他信息。

New columns on Subgroup and Range of percentage in another column

在上述问题中,相同子组的输出名称相同,即 Group1 10-30%,以及 Group2 10-30%具有相同的名称,即New_Group1

更新的询问

  1. 每个群组的名称可以与我的“群组名称”不同吗?因此,介于10%至30%之间的 Group1 被称为Group1_1,而介于10-30%之间的 Group2 被称为Group2_1
  2. 此外,在上述问题中,两组的削减幅度相同,我是否可以根据自己的需要削减削减幅度?例如:在 Group1 中,我可以将剪切分为“ Highest”,“ 10-30%”和“ less than 10%”,而在 Group2 中, ,我可以将其剪切为“ Highest”,“ 40-10%”,“ 10-5%”和“ less than 5%”。

3。因此,新类别将是Sub_Group_name。因为,我们拥有的削减数量减去最高数量后,新的组将被命名为 Group1_1 Group1_2 等。

更新的示例数据:

df_test<- data.frame("Group.Name"=c("Group1","Group2","Group1","Group2","Group2","Group2","Group1"),
                "Sub_group_name"=c("A","A","B","C","D","E","C"),
                "Total%"=c(35,16,2,29,4,8,13))

更新后的输出数据:

df_Updated_output<- data.frame("Group.Name"=c("Group1","Group2","Group1","Group2","Group2","Group2","Group1"),
                     "Sub_group_name"=c("A","A","B","C","D","E","C"),
                     "Total%"=c(35,16,2,29,4,8,13),
                     "category"=c("A","New_Group2_1","New_Group1_2","C","New_Group2_3","New_Group2_2","New_Group1_1"))

出现新问题的原因是由于更新了要求,解决方案将发生巨大变化。

1 个答案:

答案 0 :(得分:0)

以下提到的ifelse条件将为您提供所需的输出。

df_test$category <- ifelse(df_test$Group.Name == "Group1" & df_test$Total. >=10 & df_test$Total. <= 30,"Group1_1",ifelse(df_test$Group.Name == "Group1" & df_test$Total. < 10,"Group1_2",ifelse(df_test$Group.Name == "Group2" & df_test$Total. >10 & df_test$Total. <= 40,"Group2_1",ifelse(df_test$Group.Name == "Group2" & df_test$Total. >=5 & df_test$Total. <= 10,"Group2_2",ifelse(df_test$Group.Name == "Group2" & df_test$Total. < 5,"Group2_3",df_test$Sub_group_name)))))

我认为您在group2_2(5%到40%)条件下提供了错误的数据,因此请查看我的条件并正确进行更改。