我在下面的数据集中设置了DimentionID和Itemid列:
dput(dim_grouped)
structure(list(DimentionID = c("1_18 to less than 25 years old",
"1_25 to less than 35 years old", "1_35 to less than 45 years old",
"1_45 to less than 55 years old", "1_More than 55 years old",
"10_18 to less than 25 years old", "10_25 to less than 35 years old",
"10_35 to less than 45 years old", "10_45 to less than 55 years old",
"10_More than 55 years old", "2_18 to less than 25 years old",
"2_25 to less than 35 years old", "2_35 to less than 45 years old",
"2_45 to less than 55 years old", "2_More than 55 years old",
"3_18 to less than 25 years old", "3_25 to less than 35 years old",
"3_35 to less than 45 years old", "3_45 to less than 55 years old",
"3_More than 55 years old", "4_18 to less than 25 years old",
"4_25 to less than 35 years old", "4_35 to less than 45 years old",
"4_45 to less than 55 years old", "4_More than 55 years old",
"5_18 to less than 25 years old", "5_25 to less than 35 years old",
"5_35 to less than 45 years old", "5_45 to less than 55 years old",
"5_More than 55 years old", "6_18 to less than 25 years old",
"6_25 to less than 35 years old", "6_35 to less than 45 years old",
"6_45 to less than 55 years old", "6_More than 55 years old",
"7_18 to less than 25 years old", "7_25 to less than 35 years old",
"7_35 to less than 45 years old", "7_45 to less than 55 years old",
"7_More than 55 years old", "8_18 to less than 25 years old",
"8_25 to less than 35 years old", "8_35 to less than 45 years old",
"8_45 to less than 55 years old", "8_More than 55 years old",
"9_18 to less than 25 years old", "9_25 to less than 35 years old",
"9_35 to less than 45 years old", "9_45 to less than 55 years old",
"9_More than 55 years old"), Itemid = list(c("QID_1_18 to less than 25 years old",
"QID_2_18 to less than 25 years old", "QID_3_18 to less than 25 years old",
"QID_4_18 to less than 25 years old", "QID_5_18 to less than 25 years old",
"QID_6_18 to less than 25 years old"), c("QID_1_25 to less than 35 years old",
"QID_2_25 to less than 35 years old", "QID_3_25 to less than 35 years old",
"QID_4_25 to less than 35 years old", "QID_5_25 to less than 35 years old",
"QID_6_25 to less than 35 years old"), c("QID_1_35 to less than 45 years old",
"QID_2_35 to less than 45 years old", "QID_3_35 to less than 45 years old",
"QID_4_35 to less than 45 years old", "QID_5_35 to less than 45 years old",
"QID_6_35 to less than 45 years old"), c("QID_1_45 to less than 55 years old",
"QID_2_45 to less than 55 years old", "QID_3_45 to less than 55 years old",
"QID_4_45 to less than 55 years old", "QID_5_45 to less than 55 years old",
"QID_6_45 to less than 55 years old"), c("QID_1_More than 55 years old",
"QID_2_More than 55 years old", "QID_3_More than 55 years old",
"QID_4_More than 55 years old", "QID_5_More than 55 years old",
"QID_6_More than 55 years old"), c("QID_53_18 to less than 25 years old",
"QID_54_18 to less than 25 years old", "QID_55_18 to less than 25 years old"
), c("QID_53_25 to less than 35 years old", "QID_54_25 to less than 35 years old",
"QID_55_25 to less than 35 years old"), c("QID_53_35 to less than 45 years old",
"QID_54_35 to less than 45 years old", "QID_55_35 to less than 45 years old"
), c("QID_53_45 to less than 55 years old", "QID_54_45 to less than 55 years old",
"QID_55_45 to less than 55 years old"), c("QID_53_More than 55 years old",
"QID_54_More than 55 years old", "QID_55_More than 55 years old"
), c("QID_7_18 to less than 25 years old", "QID_20_18 to less than 25 years old",
"QID_21_18 to less than 25 years old", "QID_36_18 to less than 25 years old",
"QID_37_18 to less than 25 years old", "QID_46_18 to less than 25 years old"
), c("QID_7_25 to less than 35 years old", "QID_20_25 to less than 35 years old",
"QID_21_25 to less than 35 years old", "QID_36_25 to less than 35 years old",
"QID_37_25 to less than 35 years old", "QID_46_25 to less than 35 years old"
), c("QID_7_35 to less than 45 years old", "QID_20_35 to less than 45 years old",
"QID_21_35 to less than 45 years old", "QID_36_35 to less than 45 years old",
"QID_37_35 to less than 45 years old", "QID_46_35 to less than 45 years old"
), c("QID_7_45 to less than 55 years old", "QID_20_45 to less than 55 years old",
"QID_21_45 to less than 55 years old", "QID_36_45 to less than 55 years old",
"QID_37_45 to less than 55 years old", "QID_46_45 to less than 55 years old"
), c("QID_7_More than 55 years old", "QID_20_More than 55 years old",
"QID_21_More than 55 years old", "QID_36_More than 55 years old",
"QID_37_More than 55 years old", "QID_46_More than 55 years old"
), c("QID_8_18 to less than 25 years old", "QID_10_18 to less than 25 years old",
"QID_11_18 to less than 25 years old", "QID_34_18 to less than 25 years old",
"QID_52_18 to less than 25 years old"), c("QID_8_25 to less than 35 years old",
"QID_10_25 to less than 35 years old", "QID_11_25 to less than 35 years old",
"QID_34_25 to less than 35 years old", "QID_52_25 to less than 35 years old"
), c("QID_8_35 to less than 45 years old", "QID_10_35 to less than 45 years old",
"QID_11_35 to less than 45 years old", "QID_34_35 to less than 45 years old",
"QID_52_35 to less than 45 years old"), c("QID_8_45 to less than 55 years old",
"QID_10_45 to less than 55 years old", "QID_11_45 to less than 55 years old",
"QID_34_45 to less than 55 years old", "QID_52_45 to less than 55 years old"
), c("QID_8_More than 55 years old", "QID_10_More than 55 years old",
"QID_11_More than 55 years old", "QID_34_More than 55 years old",
"QID_52_More than 55 years old"), c("QID_9_18 to less than 25 years old",
"QID_12_18 to less than 25 years old", "QID_24_18 to less than 25 years old",
"QID_25_18 to less than 25 years old", "QID_38_18 to less than 25 years old",
"QID_43_18 to less than 25 years old"), c("QID_9_25 to less than 35 years old",
"QID_12_25 to less than 35 years old", "QID_24_25 to less than 35 years old",
"QID_25_25 to less than 35 years old", "QID_38_25 to less than 35 years old",
"QID_43_25 to less than 35 years old"), c("QID_9_35 to less than 45 years old",
"QID_12_35 to less than 45 years old", "QID_24_35 to less than 45 years old",
"QID_25_35 to less than 45 years old", "QID_38_35 to less than 45 years old",
"QID_43_35 to less than 45 years old"), c("QID_9_45 to less than 55 years old",
"QID_12_45 to less than 55 years old", "QID_24_45 to less than 55 years old",
"QID_25_45 to less than 55 years old", "QID_38_45 to less than 55 years old",
"QID_43_45 to less than 55 years old"), c("QID_9_More than 55 years old",
"QID_12_More than 55 years old", "QID_24_More than 55 years old",
"QID_25_More than 55 years old", "QID_38_More than 55 years old",
"QID_43_More than 55 years old"), c("QID_13_18 to less than 25 years old",
"QID_23_18 to less than 25 years old", "QID_26_18 to less than 25 years old",
"QID_33_18 to less than 25 years old", "QID_42_18 to less than 25 years old",
"QID_45_18 to less than 25 years old"), c("QID_13_25 to less than 35 years old",
"QID_23_25 to less than 35 years old", "QID_26_25 to less than 35 years old",
"QID_33_25 to less than 35 years old", "QID_42_25 to less than 35 years old",
"QID_45_25 to less than 35 years old"), c("QID_13_35 to less than 45 years old",
"QID_23_35 to less than 45 years old", "QID_26_35 to less than 45 years old",
"QID_33_35 to less than 45 years old", "QID_42_35 to less than 45 years old",
"QID_45_35 to less than 45 years old"), c("QID_13_45 to less than 55 years old",
"QID_23_45 to less than 55 years old", "QID_26_45 to less than 55 years old",
"QID_33_45 to less than 55 years old", "QID_42_45 to less than 55 years old",
"QID_45_45 to less than 55 years old"), c("QID_13_More than 55 years old",
"QID_23_More than 55 years old", "QID_26_More than 55 years old",
"QID_33_More than 55 years old", "QID_42_More than 55 years old",
"QID_45_More than 55 years old"), c("QID_14_18 to less than 25 years old",
"QID_15_18 to less than 25 years old", "QID_16_18 to less than 25 years old",
"QID_17_18 to less than 25 years old", "QID_18_18 to less than 25 years old",
"QID_19_18 to less than 25 years old"), c("QID_14_25 to less than 35 years old",
"QID_15_25 to less than 35 years old", "QID_16_25 to less than 35 years old",
"QID_17_25 to less than 35 years old", "QID_18_25 to less than 35 years old",
"QID_19_25 to less than 35 years old"), c("QID_14_35 to less than 45 years old",
"QID_15_35 to less than 45 years old", "QID_16_35 to less than 45 years old",
"QID_17_35 to less than 45 years old", "QID_18_35 to less than 45 years old",
"QID_19_35 to less than 45 years old"), c("QID_14_45 to less than 55 years old",
"QID_15_45 to less than 55 years old", "QID_16_45 to less than 55 years old",
"QID_17_45 to less than 55 years old", "QID_18_45 to less than 55 years old",
"QID_19_45 to less than 55 years old"), c("QID_14_More than 55 years old",
"QID_15_More than 55 years old", "QID_16_More than 55 years old",
"QID_17_More than 55 years old", "QID_18_More than 55 years old",
"QID_19_More than 55 years old"), c("QID_22_18 to less than 25 years old",
"QID_35_18 to less than 25 years old", "QID_39_18 to less than 25 years old",
"QID_40_18 to less than 25 years old", "QID_41_18 to less than 25 years old",
"QID_44_18 to less than 25 years old"), c("QID_22_25 to less than 35 years old",
"QID_35_25 to less than 35 years old", "QID_39_25 to less than 35 years old",
"QID_40_25 to less than 35 years old", "QID_41_25 to less than 35 years old",
"QID_44_25 to less than 35 years old"), c("QID_22_35 to less than 45 years old",
"QID_35_35 to less than 45 years old", "QID_39_35 to less than 45 years old",
"QID_40_35 to less than 45 years old", "QID_41_35 to less than 45 years old",
"QID_44_35 to less than 45 years old"), c("QID_22_45 to less than 55 years old",
"QID_35_45 to less than 55 years old", "QID_39_45 to less than 55 years old",
"QID_40_45 to less than 55 years old", "QID_41_45 to less than 55 years old",
"QID_44_45 to less than 55 years old"), c("QID_22_More than 55 years old",
"QID_35_More than 55 years old", "QID_39_More than 55 years old",
"QID_40_More than 55 years old", "QID_41_More than 55 years old",
"QID_44_More than 55 years old"), c("QID_27_18 to less than 25 years old",
"QID_28_18 to less than 25 years old", "QID_29_18 to less than 25 years old",
"QID_30_18 to less than 25 years old", "QID_31_18 to less than 25 years old",
"QID_32_18 to less than 25 years old"), c("QID_27_25 to less than 35 years old",
"QID_28_25 to less than 35 years old", "QID_29_25 to less than 35 years old",
"QID_30_25 to less than 35 years old", "QID_31_25 to less than 35 years old",
"QID_32_25 to less than 35 years old"), c("QID_27_35 to less than 45 years old",
"QID_28_35 to less than 45 years old", "QID_29_35 to less than 45 years old",
"QID_30_35 to less than 45 years old", "QID_31_35 to less than 45 years old",
"QID_32_35 to less than 45 years old"), c("QID_27_45 to less than 55 years old",
"QID_28_45 to less than 55 years old", "QID_29_45 to less than 55 years old",
"QID_30_45 to less than 55 years old", "QID_31_45 to less than 55 years old",
"QID_32_45 to less than 55 years old"), c("QID_27_More than 55 years old",
"QID_28_More than 55 years old", "QID_29_More than 55 years old",
"QID_30_More than 55 years old", "QID_31_More than 55 years old",
"QID_32_More than 55 years old"), c("QID_47_18 to less than 25 years old",
"QID_48_18 to less than 25 years old", "QID_49_18 to less than 25 years old",
"QID_50_18 to less than 25 years old", "QID_51_18 to less than 25 years old"
), c("QID_47_25 to less than 35 years old", "QID_48_25 to less than 35 years old",
"QID_49_25 to less than 35 years old", "QID_50_25 to less than 35 years old",
"QID_51_25 to less than 35 years old"), c("QID_47_35 to less than 45 years old",
"QID_48_35 to less than 45 years old", "QID_49_35 to less than 45 years old",
"QID_50_35 to less than 45 years old", "QID_51_35 to less than 45 years old"
), c("QID_47_45 to less than 55 years old", "QID_48_45 to less than 55 years old",
"QID_49_45 to less than 55 years old", "QID_50_45 to less than 55 years old",
"QID_51_45 to less than 55 years old"), c("QID_47_More than 55 years old",
"QID_48_More than 55 years old", "QID_49_More than 55 years old",
"QID_50_More than 55 years old", "QID_51_More than 55 years old"
))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-50L))
我需要根据DimentionID对此数据集的行进行排序,此列中的所有值最后都具有年龄组类别,例如1_8到25岁以下,1_25到35岁以下,等等。我要订购这些年龄类别的行。 例如:最后所有具有18至小于25岁的值,然后所有具有25至小于35岁的值,等等。
答案 0 :(得分:0)
您可以使用正则表达式:
使用a
作为您的data.frame
:
matches <- regexpr('[[:alpha:]]{4} than [[:digit:]]{2}',a$DimentionID)
content <- substr(a$DimentionID, matches, matches + attr(matches, 'match.length'))
content
向量现在应包含与排序相关的部分字符串。
剩下的就是用它来创建索引向量并用它对data.frame
进行排序:
a[order(content),]
答案 1 :(得分:0)
这是一种方法:
library(dplyr)
dim_grouped %>%
mutate(age_g = gsub('[0-9]+_', '', DimentionID)) %>%
arrange(age_g)