I tried to run a couple of for-loops inside a function in order to get a couple of vectors, but it didn't work:
x_vec <- seq(-10, 10, .1)
y_vec <- seq(-10, 10, .1)
first_dim <- NA
second_dim <- NA
fgradient <- function(x, y) {
for (i in seq_along (x)) {
first_dim[i] <- (2 * (i - 2))
}
for (i in seq_along (y)) {
second_dim[i] <- (4 * i)
}
}
Here fgradient (x_vec, y_vec) doesn't obtain the two vectors first_dim and second_dim. However, if I run the two for-loops separately outside the function, they do work and I get the two vectors. What am I missing? Thanks so much!