Concatenate/paste 不同长度的字符串根据索引

Concatenate/paste strings of varying length according to index

我想知道如何连接字符串以形成不同和不同长度以及根据一种条件改变内容的序列。

这里有一个dataframe的例子(我的DF其实是60000行左右)。

  1. column index: 只是一个索引
  2. to_concat: 我要连接的字符串项
  3. max_seq:连接条件的一个示例(to_concat 仅当它是同一序列的一部分时才应连接 - 我现在已经指出了字符串在序列中的位置.
  4. concat_result: 我想要的结果

    index to_concat  max_seq    concat_result
    1        Abc!      1         <abc!+def+_>
    2        def       2         <abc!+def+_>
    3        _         3         <abc!+def+_>
    4        x93       1         <x93+afza+5609+5609+Abc!+def>
    5        afza      2         <x93+afza+5609+5609+Abc!+def>
    6        5609      3         <x93+afza+5609+5609+Abc!+def>
    7        5609      4         <x93+afza+5609+5609+Abc!+def>
    8        Abc!      5         <x93+afza+5609+5609+Abc!+def>
    9        def       6         <x93+afza+5609+5609+Abc!+def>
    10        _         1         <_+x93+afza>
    11        x93       2         <_+x93+afza>
    12        afza      3         <_+x93+afza>
    

我知道 pasteaggregatelength,可能有用。但是不知道按什么顺序做,尤其是如何配制糊状物。

我想我还应该包括一个 "second" 更好地为 max_seq 做的索引(例如:所有要连接在同一序列中的字符串都有相同的数字,所以这里我们有一个 3序列“1 1 1 2 2 2 2 2 2 3 3 3”。

但我不知道这是否是quickest/easiest解决方案,而且我也不知道如何粘贴不同长度...

请问有博士同学帮忙吗?非常感谢。


可重现的例子: dput(dat)

> dput(dat)
structure(list(V1 = c("index", "1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10", "11", "12"), V2 = c("to_concat", "Abc!", 
"def", "_", "x93", "afza", "5609", "5609", "Abc!", "def", "_", 
"x93", "afza"), V3 = c("max_seq", "1", "2", "3", "1", "2", "3", 
"4", "5", "6", "1", "2", "3"), V4 = c("concat_result", "<abc!+def+_>", 
"<abc!+def+_>", "<abc!+def+_>", "<x93+afza+5609+5609+Abc!+def>", 
"<x93+afza+5609+5609+Abc!+def>", "<x93+afza+5609+5609+Abc!+def>", 
"<x93+afza+5609+5609+Abc!+def>", "<x93+afza+5609+5609+Abc!+def>", 
"<x93+afza+5609+5609+Abc!+def>", "<_+x93+afza>", "<_+x93+afza>", 
"<_+x93+afza>")), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c(NA,
-13L))

获得所需结果的几个选项:

1) 使用基数 R:

mydf$grp <- cumsum(mydf$max_seq < c(1,head(mydf$max_seq, -1))) + 1
mydf$concat_result <- ave(mydf$to_concat, mydf$grp, 
                          FUN = function(x) paste0('<',paste(x,collapse='+'),'>'))

给出:

> mydf
   index to_concat max_seq grp                 concat_result
1      1      Abc!       1   1                  <Abc!+def+_>
2      2       def       2   1                  <Abc!+def+_>
3      3         _       3   1                  <Abc!+def+_>
4      4       x93       1   2 <x93+afza+5609+5609+Abc!+def>
5      5      afza       2   2 <x93+afza+5609+5609+Abc!+def>
6      6      5609       3   2 <x93+afza+5609+5609+Abc!+def>
7      7      5609       4   2 <x93+afza+5609+5609+Abc!+def>
8      8      Abc!       5   2 <x93+afza+5609+5609+Abc!+def>
9      9       def       6   2 <x93+afza+5609+5609+Abc!+def>
10    10         _       1   3                  <_+x93+afza>
11    11       x93       2   3                  <_+x93+afza>
12    12      afza       3   3                  <_+x93+afza>

2) 或者使用 data.table 包:

library(data.table)
setDT(mydf)[, grp := cumsum(max_seq < shift(max_seq, fill = 0))+1
            ][, concat_result := paste0('<',paste(to_concat,collapse='+'),'>'), grp][]

3) 或者使用 dplyr 包:

library(dplyr)
mydf %>%
  mutate(grp = cumsum(max_seq < lag(max_seq, n=1, default=0))+1) %>%
  group_by(grp) %>%
  mutate(concat_result = paste0('<',paste(to_concat,collapse='+'),'>'))

已用数据:

mydf <- structure(list(index = 1:12, 
                       to_concat = c("Abc!", "def", "_", "x93", "afza", "5609", "5609", "Abc!", "def", "_", "x93", "afza"), 
                       max_seq = c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L)), 
                  .Names = c("index", "to_concat", "max_seq"), class = "data.frame", row.names = c(NA, -12L))