0

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).

s1624210
  • 627
  • 4
  • 11

1 Answers1

0

If your output for x is

> x
[1]  1  4  7 10 13 16 19 22 25 28 31

you could just use:

x <- seq(0,10) * 3 + 1
ReelSaemon
  • 33
  • 6