mardi 11 mai 2021

How do I using conditionals and stop function inside a function with vectors

I have a function that shifts the letters in the word based on random integer x. I need to set a conditional in the beginning that if x>list, the function will stop. I tried using the stop function but it produced an error.

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

library(purrr)

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)

Aucun commentaire:

Enregistrer un commentaire