r-如何在带向量的函数中使用条件词和停止函数



我有一个函数,可以根据随机整数x来移动单词中的字母。我需要在开头设置一个条件,如果x>列表中,该功能将停止。我试着使用停止功能,但它产生了一个错误。

Error in .f(.x[[i]], ...) : argument "x" is missing, with no default

wordlist <- c("Is", "Book", "Appreciate", "Cherry", "Computer", "Of")
shift <- function(list, x){
x <- sample(1:4,6, replace==TRUE)
if (x>list) stop("length of x is greater than the letters in the word!")
paste0(substr(list,nchar(list)-x+1,nchar(list)), substr(list,1,nchar(list)-x))
}
map(wordlist, shift)

例如,对于前两个单词";的";以及";Book";,比如x=3表示";的";x=2表示";Book";,它将产生以下输出:

[[1]]
[[1]]$result
NULL
[[1]]$error
<simpleError in .f(...): length of x is greater than the letters in the word!>
[[2]]
[[2]]$result
[1] "okbo"
[[2]]$error
NULL

似乎有点令人困惑的是x在哪里进入图片。它是一组长度等于单词表的随机数吗?它是单词列表中所有成员共享的一个随机数吗?

如果单词列表中的每个成员的x都不同,为什么不简单地在函数中生成它呢?

例如:

wordShift <- function(x) {
# Set maximum value for shift location for each word
nC <- nchar(x)
# Now pick a random number between 1 and the value of nC for each element of
# nC. Use vapply to do this relatively quickly and type-safely
shiftLoc <- vapply(nC, sample, integer(1L), size = 1L)
# Pre-allocate your results if you can. Return a vector like the input
ret <- character(length(x))

# Now loop through the list and rotate words
for (i in seq_along(ret)) {
ret[i] <- paste0(substr(x[i], shiftLoc[i], nC[i]),
substr(x[i], 1L, shiftLoc[i] - 1L))
}
ret
}

以上不应生成非法的移位位置,并在不需要其他包的情况下使用*apply或来自base的自然矢量化函数来提高速度和可靠性。以下是一些示例输出:

wordList <- c("Is", "Book", "Appreciate", "Cherry", "Computer", "Of")
set.seed(19L)
shifted <- t(replicate(25L, wordShift(wordList))) # transposing b/c replicated pastes columnwise
> shifted
[,1] [,2]   [,3]         [,4]     [,5]       [,6]
[1,] "Is" "ookB" "ciateAppre" "erryCh" "uterComp" "fO"
[2,] "Is" "okBo" "Appreciate" "ryCher" "terCompu" "fO"
[3,] "Is" "kBoo" "reciateApp" "ryCher" "puterCom" "Of"
[4,] "Is" "okBo" "teApprecia" "erryCh" "rCompute" "fO"
[5,] "Is" "Book" "reciateApp" "rryChe" "erComput" "Of"
[6,] "sI" "ookB" "ciateAppre" "rryChe" "rCompute" "fO"
[7,] "Is" "okBo" "ciateAppre" "erryCh" "uterComp" "Of"
[8,] "Is" "Book" "teApprecia" "rryChe" "Computer" "fO"
[9,] "Is" "Book" "eAppreciat" "herryC" "uterComp" "Of"
[10,] "sI" "kBoo" "preciateAp" "rryChe" "puterCom" "fO"
[11,] "sI" "kBoo" "eAppreciat" "erryCh" "rCompute" "fO"
[12,] "Is" "okBo" "reciateApp" "yCherr" "Computer" "fO"
[13,] "Is" "okBo" "eciateAppr" "Cherry" "puterCom" "fO"
[14,] "sI" "ookB" "teApprecia" "ryCher" "Computer" "Of"
[15,] "Is" "ookB" "eciateAppr" "Cherry" "erComput" "Of"
[16,] "sI" "kBoo" "ciateAppre" "yCherr" "mputerCo" "fO"
[17,] "Is" "Book" "ciateAppre" "rryChe" "terCompu" "Of"
[18,] "sI" "okBo" "ateAppreci" "rryChe" "puterCom" "fO"
[19,] "sI" "ookB" "teApprecia" "ryCher" "Computer" "Of"
[20,] "Is" "Book" "eciateAppr" "Cherry" "terCompu" "fO"
[21,] "Is" "okBo" "preciateAp" "erryCh" "mputerCo" "Of"
[22,] "sI" "kBoo" "eAppreciat" "rryChe" "rCompute" "fO"
[23,] "sI" "okBo" "ciateAppre" "Cherry" "erComput" "Of"
[24,] "sI" "okBo" "reciateApp" "Cherry" "omputerC" "fO"
[25,] "sI" "kBoo" "ciateAppre" "herryC" "omputerC" "Of"

最新更新