I have the following vector:
set.seed(1); v1 = rnorm(100, 40, 10)
fun1 <- function(x){
x = x - 1
return(x)
}
fun2 <- function(x){
x = x * 10
return(x)
}
fun3 <- function(x){
x = x / 5
return(x)
}
I would like to set up a loop for the length of the vector but apply different functions for the elements [i] :
fun1 for v1[1:20]&v1[41:60]
fun2 for v1[21:40]
fun3 for v1[61:100]
And then return a vector.
Turns out I have no idea really how to do it elegantly.
result[1:20]<-fun1(v1[1:20])etc?fun1 <- function(x) x - 1. Assignment and return are not necessary, R functions return the last line be default.