我有一个数据框,其中一列中的元素是
"1.cn3.ap.1"
"7.fr9.ap.3"
"4.dl.ap.2"
"5.d2.cr.1"
"4.dl.u.1"
"4.dl.ap.1"
df<- df[order(df$A),]
#this gave the following result :
"1.cn3.ap.1"
"4.dl.ap.1"
"4.dl.ap.2"
"4.dl.u.1"
"5.d2.cr.1"
"7.fr9.ap.3"
但我需要这样的数据:
"1.cn3.ap.1"
"4.dl.u.1"
"4.dl.ap.1"
"4.dl.ap.2"
"5.d2.cr.1"
"7.fr9.ap.3"
答案 0 :(得分:1)
您可以通过按周期拆分数据,然后对各列进行排序,然后在排序后再将列重新组合在一起,从而获得所需内容。类似的东西可能吗?
library(dplyr)
library(tidyr)
df <- df %>%
separate(A, into = c("part1","part2","part3","part4"), sep = "\\.") %>%
arrange(part1, part2, desc(part3), part4) %>%
unite(A, part1:part4, sep = ".")