Slice a string at consecutive indices with R / Rcpp?

后端 未结 2 1226
鱼传尺愫
鱼传尺愫 2021-01-05 18:55

I want to write a function that slices a \'string\' into a vector, sequentially, at a given index. I have a fairly adequate R solution for it; however, I figure that writing

2条回答
  •  旧巷少年郎
    2021-01-05 19:38

    This one-liner using strapplyc from the gsubfn package is fast enough that rcpp may not be needed. Here we apply it to the entire text of James Joyce's Ulysses which only takes a few seconds:

    library(gsubfn)
    joyce <- readLines("http://www.gutenberg.org/files/4300/4300-8.txt") 
    joycec <- paste(joyce, collapse = " ") # all in one string 
    n <- 2
    system.time(s <- strapplyc(joycec, paste(rep(".", n), collapse = ""))[[1]])
    

提交回复
热议问题