如何将字符串分成重叠的3组

时间:2017-09-13 17:02:38

标签: r

如果我有多个字符串,如:

skhdsfiiuwkncyeuhrsl
sdskkjheocbsill
sldkjflsdkjb

如何将输出编程为重叠的三元组,例如,我希望它输出:

skh, khd, hds, ..., rsl
sds, dsk, skk, ..., ill
sld, ldk, dkj, ..., kjb

3 个答案:

答案 0 :(得分:4)

substring有效:

x = c("skhdsfiiuwkncyeuhrsl", "sdskkjheocbsill", "sldkjflsdkjb", "ab")
n = 3
lapply(x, function(z) 
  if ((nc <- nchar(z)) >= n) 
    substring(z, seq(1, nc - n + 1), seq(n, nc)) 
  else 
    character(0)
)

给出了

[[1]]
 [1] "skh" "khd" "hds" "dsf" "sfi" "fii" "iiu" "iuw" "uwk" "wkn" "knc" "ncy"
[13] "cye" "yeu" "euh" "uhr" "hrs" "rsl"

[[2]]
 [1] "sds" "dsk" "skk" "kkj" "kjh" "jhe" "heo" "eoc" "ocb" "cbs" "bsi" "sil"
[13] "ill"

[[3]]
 [1] "sld" "ldk" "dkj" "kjf" "jfl" "fls" "lsd" "sdk" "dkj" "kjb"

[[4]]
character(0)

答案 1 :(得分:2)

this answer获取灵感,这是一个单行:

strings <- c("skhdsfiiuwkncyeuhrsl",
"sdskkjheocbsill",
"sldkjflsdkjb")

sapply(strings, function(x) substring(x, seq(1,nchar(x)-2,1), seq(3,nchar(x),1)))

# $skhdsfiiuwkncyeuhrsl
#  [1] "skh" "khd" "hds" "dsf" "sfi" "fii" "iiu" "iuw" "uwk" "wkn" "knc" "ncy" "cye" "yeu" "euh"
# [16] "uhr" "hrs" "rsl"

# $sdskkjheocbsill
#  [1] "sds" "dsk" "skk" "kkj" "kjh" "jhe" "heo" "eoc" "ocb" "cbs" "bsi" "sil" "ill"

# $sldkjflsdkjb
#  [1] "sld" "ldk" "dkj" "kjf" "jfl" "fls" "lsd" "sdk" "dkj" "kjb"

答案 2 :(得分:1)

a <- "skhdsfiiuwkncyeuhrsl"
b <- "sdskkjheocbsill"
c <- "sldkjflsdkjb"

make_triplets <- 
  function(X){
    nTriplets <- length(2:(nchar(X)-1))
    triplets <- character(nTriplets)
    for(i in 2:(nchar(X)-1)){
      triplets[i-1] <- substr(X, i - 1, i + 1)
    }
    return(triplets)
  }
make_triplets(a)

make_triplets(b)

make_triplets(c)