根据几种模式重新排序 R 向量

re-order R vector based on several patterns

vec <- c("a: 25", "g: 32", "h: 44", "i: 88", "",
         "a: 22", "i: 53a", "h: 4.2", "g: rt", "",
         "a: zzz", "h: 53", "g: 634", "i: 4", "qq\t5.6", "",
         "a: p", "h: r", "i: 6", "g: 6", "")
beg_pattern <- "a: "; end_element <- ""
grep_pattern1 <- "g: "; grep_pattern2 <- "h: "
desired_reordered_vec <- c("a: 25", "g: 32", "h: 44", "i: 88", "",
                           "a: 22", "i: 53a", "g: rt", "h: 4.2", "",
                           "a: zzz", "g: 634", "h: 53", "i: 4", "qq\t5.6", "",
                           "a: p", "g: 6", "i: 6", "h: r", "")

基本上,我想重新排序我的矢量,以便我的“g”​​元素出现在每个元素块中的“h”元素之前,这些元素以 beg_pattern 开头并以 [=18= 结尾].本例中有不止一个有效解向量。在我的实际数据中,模式是不同的,除了结束元素,每个块有 20-200 个元素。我的问题有这三个问题的要素:, Split a vector into chunks, Split a vector into three vectors of unequal length in R。但我还没有能够拼凑出一个解决方案。我可以通过这种方式获取索引:

a <- grep("a: ", vec); z <- which("" == vec)
u <- grep("g: ", vec); v <- grep("h: ", vec)

但我不确定其中哪些(如果有的话)可以帮助我执行所需的重新排序。

您可以在 Base R 中编写一个函数来执行此操作:

my_sort <- function(x){
  y <- grep('^(g|h):', x)
  x[y] <- x[y][match(sub(":.*", "", x[y]), c("g", "h"))]
  x
}


f <- head(cumsum(vec == ''), -1)
ave(vec, c(f[1], f), FUN = my_sort)
 [1] "a: 25"  "g: 32"  "h: 44"  "i: 88"  ""       "a: 22"  "i: 53a" "g: rt"  "h: 4.2" ""       "a: zzz"
[12] "g: 634" "h: 53"  "i: 4"   "qq\t5.6" ""       "a: p"   "g: 6"   "i: 6"   "h: r"   ""