I'm trying to simplify code looking like this
someFunc <- function(x) {
return(x + 3L)
}
x <- 1L
for (i in 1:10) {
x <- c(x, someFunc(x[length(x)]))
}
I was thinking about reduce like this
replicate(10, x <- c(x, someFunc(x[length(x)]))
but this does not work, since it uses a copy of x
. How can I get the logic to work without using a loop. I think there has to be a way in R (someFunc
should be arbitrary - I just posted it as an example).