我正在嘗試從隨機選擇的字母中生成隨機的單詞字串,并將它們與字串的相似性進行比較。
使用 random 包,我可以輕松地生成與我的字串長度相同的隨機字母序列,但是我想超越一個簡單的單詞并與一個句子進行比較。
為此,我的隨機字串生成需要包含空格以創建單詞,但我不確定如何包含空格字符。
library(random)
text = 'THIS IS A DEMO'
textlength <- nchar(text)
string <- randomStrings(
n = 100,
len = textlength,
digits = FALSE,
upperalpha = TRUE,
loweralpha = FALSE,
unique = FALSE,
check = TRUE)
輸出:
[1,] "QSBMNCORYVYPVB"
[2,] "WCGGPWLPYLAIIH"
...
所需的輸出:
[1,] "QSB MNC R YV "
[2,] "W G PWLP IIH"
...
uj5u.com熱心網友回復:
text = 'THIS IS A DEMO'
textlength <- nchar(text)
n <- 10
string <- random::randomStrings(
n = n,
len = textlength,
digits = FALSE,
upperalpha = TRUE,
loweralpha = FALSE,
unique = FALSE,
check = TRUE)
spaces <- replicate(n,
structure(sort(sample(textlength, sample(textlength, 1))),
match.length = 1))
regmatches(string, spaces) <- " "
string
[1] " A Z " "W YL JSJI FYV" "LL D AG " " XH Z FV R "
[5] " B Y F " "AHO QDZOM ADLQ" " REKOEQMNG H J" "O D FWO P "
[9] " B S " " A "
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477713.html
