我想把一个单词分成双字组。我正在使用qlcMatrix
包,但它只返回不同的二元组。例如,对于单词"detected"
,它只返回一次"te"
。这是我使用的命令
test_domain <- c("detected")
library("qlcMatrix", lib.loc="~/R/win-library/3.2")
bigram1 <- splitStrings(test_domain, sep = "", bigrams = TRUE, left.boundary = "", right.boundary = "")$bigrams
这是我得到的结果:
bigram1
# [1] "ec" "ed" "de" "te" "ct" "et"
使用R
基础的另一种方法是使用mapply
和substr
:
nc <- nchar("detected")
mapply(function(x, y){substr("detected", x, y)}, x=1:(nc-1), y=2:nc)
# [1] "de" "et" "te" "ec" "ct" "te" "ed"
没有包你可以这样做:
test_domain <- c("detected")
temp <- strsplit(test_domain ,'')[[1]]
sapply(1:(length(temp)-1), function(x){paste(temp[x:(x+1)], collapse='')})
# [1] "de" "et" "te" "ec" "ct" "te" "ed"