基于不同组和不同子组的新列 [英] New column based on different groups and different subgroups
问题描述
此问题是以下问题的扩展,因此请参考该问题以获取数据和其他信息。
This question is extension to the below question so please refer the question for data and other information.
在上述问题中,相同子组的输出名称相同,即 Group1 和 10-30%和<具有 10-30%的strong> Group2 具有相同的名称,即 New_Group1
。
In the above question, the output was same name for the same subgroups i.e. Group1 with 10-30% and Group2 with 10-30% is having same name i.e. New_Group1
.
更新的询问:
- 姓名的组数与我的Group_Name不同。因此,介于10-30%之间的 Group1 被称为
Group1_1
,而介于10-30%之间的 Group2 被称为Group2_1
- 另外,在上述问题中,两组的削减幅度相同,我是否可以根据需要增加削减幅度?例如:在 Group1 中,我可以削减为
最高
,10-30%$ c $ c>和
低于10%
,而在 Group2 中,我可以将切割降低为最高
,40-10%
,10-5%
和少于5%
。
- Can the names of groups be different as per my Group_Name. So,Group1 between 10-30% is called
Group1_1
whereas Group2 between 10-30% is calledGroup2_1
- Also, in the above question the cuts are same across two groups, can I have cuts accroding to my need. For example: In Group1 I can have cuts as "
Highest
", "10-30%
" and "less than 10%
" whereas in Group2, I can have cuts as "Highest
","40-10%
","10-5%
" and "less than 5%
".
3。因此,新类别将成为最高类别将是 Sub_Group_name
。因为,我们拥有的削减数量减去最高数量后,新的组将被命名为 Group1_1 , Group1_2 等。
3.So new categories will be like, for highest it will be the Sub_Group_name
. For, the number of cuts we have, minus the highest, the new groups would be names as Group1_1, Group1_2 etc.
更新的示例数据:
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"))
出现新问题的原因是
推荐答案
下面提到的ifelse条件将为您提供所需的输出。
below mentioned ifelse condition will provide you the required output.
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%)的情况下提供了错误的数据,因此请看看我的状况,并根据需要进行更改。
i think you have provided wrong data in group2_2 (5% to 40%) condition so please have a look at my condition and make the changes accodingly.
这篇关于基于不同组和不同子组的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!