我想按字母顺序对矢量的元素(字符串)进行排序,但只考虑第n个字符后的字符。字符串可以包含数字和字符,例如:
> v <- c("ENCSR529JNJ_HNR35NPK_21_K562", "ENCSR529MBZ_AP22IG_11_K562", "ENCSR529MBZ_AP22IG_21_K562", "ENCSR530BOP_DUPT6H_11_K562", "ENCSR530BOP_DUPT6H_21_K562")
并在第11个字符后排序后,v将变为:
"ENCSR529MBZ_AP22IG_11_K562", "ENCSR529MBZ_AP22IG_21_K562", "ENCSR530BOP_DUPT6H_11_K562", "ENCSR530BOP_DUPT6H_21_K562", "ENCSR529JNJ_HNR35NPK_21_K562"
任何帮助将不胜感激!感谢
答案 0 :(得分:1)
v[order(substr(v, start = 12, stop = max(nchar(v))))]
# [1] "ENCSR529MBZ_AP22IG_11_K562" "ENCSR529MBZ_AP22IG_21_K562" "ENCSR530BOP_DUPT6H_11_K562" "ENCSR530BOP_DUPT6H_21_K562"
# [5] "ENCSR529JNJ_HNR35NPK_21_K562"
substr(v, start = 12, stop = max(nchar(v)))
给出了省略前11个字符的子字符串。所以我们按此顺序。