2017-09-13 79 views
1

如果我有多個字符串,如:如何將字符串分解成重疊的集合的3

skhdsfiiuwkncyeuhrsl 
sdskkjheocbsill 
sldkjflsdkjb 

我怎麼能輸出程序是重疊的三胞胎,例如,我希望它輸出:

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

你嘗試過這麼遠嗎?像'sapply(1:(nchar(x)-2),函數(i)substring(x,i,i + 2))'''也許? – Aramis7d

回答

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" 
+1

@Frank謝謝,複製了錯誤的一行 - 我糾正了這些錯誤:) – cmaher

+0

這怎麼可以用來製作四連音或5? –

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
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) 
相關問題