How to use lapply or a family of the apply function for calling a function within a function?
I have a parent function (i.e., hrat
) that calls a sister function (i.e., drat
) within it. I would like to apply this function over certain vector. I am providing a code to demonstrate my logic. I get following error message.
Code:
drat <- function(y){
x <- y * 5
return(x)
}
hrat <- function(z, j, drat){
y <- z +1
w <- drat(y) + j
return(w)
}
z <- c(1:5)
j <- 4
result <- lapply(z,j, function(x) hrat(x, drat(x)))
ERROR MESSAGE:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'j' of mode 'function' was not found
Any help will be appreciated. Thank you
To avoid confusion, it is better to have anonymous function call
lapply(z, function(x) hrat(x, drat))