滚动文本与R中的Data.Table连接

滚动文本与R中的Data.Table连接,r,data.table,R,Data.table,我有一个如下所示的数据集: rownum<-c(1,2,3,4,5,6,7,8,9,10) name<-c("jeff","jeff","mary","jeff","jeff","jeff","mary","mary","mary","mary") text<-c("a","b","c","d","e","f","g","h","i","j") a<-data.table(rownum,name,text) rownum您可以使用Reduce和accumulate选项:

我有一个如下所示的数据集:

rownum<-c(1,2,3,4,5,6,7,8,9,10)
name<-c("jeff","jeff","mary","jeff","jeff","jeff","mary","mary","mary","mary")
text<-c("a","b","c","d","e","f","g","h","i","j")
a<-data.table(rownum,name,text)

rownum您可以使用
Reduce
accumulate
选项:

a[, rolltext := Reduce(paste0, text, accumulate = TRUE), by = name]

    rownum name text rolltext
 1:      1 jeff    a        a
 2:      2 jeff    b       ab
 3:      3 mary    c        c
 4:      4 jeff    d      abd
 5:      5 jeff    e     abde
 6:      6 jeff    f    abdef
 7:      7 mary    g       cg
 8:      8 mary    h      cgh
 9:      9 mary    i     cghi
10:     10 mary    j    cghij
或者,正如@DavidArenburg所建议的,使用
sapply
构造每一行:

a[, rolltext := sapply(1:.N, function(x) paste(text[1:x], collapse = '')), by = name]


这是一个连续求和,而滚动求和(在OP的标题中)则有所不同,至少在R术语中是这样。

这里有一个使用
substring()的想法

    rownum name text rolltext
 1:      1 jeff    a        a
 2:      2 jeff    b       ab
 3:      3 mary    c        c
 4:      4 jeff    d      abd
 5:      5 jeff    e     abde
 6:      6 jeff    f    abdef
 7:      7 mary    g       cg
 8:      8 mary    h      cgh
 9:      9 mary    i     cghi
10:     10 mary    j    cghij
我们也许可以通过stringi软件包来加快速度


美好的也可以
a[,rolltext:=sappy(1:N,函数(x)粘贴(text[1:x],collapse=''),by=name]
谢谢,David,我已经在中编辑过了。这样做的缺点可能是它需要更多的粘贴(
sum(seq(.N)-1)
vs
.N-1
with
Reduce
)。无论如何,这是我的直觉/猜测。
a[, rolltext := substring(paste(text, collapse = ""), 1, 1:.N), by = name]
    rownum name text rolltext
 1:      1 jeff    a        a
 2:      2 jeff    b       ab
 3:      3 mary    c        c
 4:      4 jeff    d      abd
 5:      5 jeff    e     abde
 6:      6 jeff    f    abdef
 7:      7 mary    g       cg
 8:      8 mary    h      cgh
 9:      9 mary    i     cghi
10:     10 mary    j    cghij
library(stringi)
a[, rolltext := stri_sub(stri_c(text, collapse = ""), length = 1:.N), by = name]