I have a vector with the optimal lambda for the box cox transformation. This is my lambda vector:
lambda <- data.frame(lambda=c(0.01,0.01,0.01))
str(lambda)
#'data.frame': 3 obs. of 1 variable:
#$ lambda: num 0.01 0.01 0.01
I also have data:
df:
obs1 obs2 obs3
34.3 232.2 2.0
2.1 56.3 90.0
etc...
Then I have a function that calculates the box-cox transformation for a column:
bc <- function (obs, lambda) {
(obs^lambda-1)/lambda }
I am trying to apply the function to my entire dataset as so:
result <- as.data.frame(lapply(df, function(u) { lapply(lambda, function(v) { bc(u,v) } ) } ))
This doesn't seem to work since I tried to run it separately on one column like this:
d <- bc(df[,1],0.01)
Then I did:
z <- result[,1] == d
table(z)
resulted in this:
FALSE TRUE
3051 2
So basically, the two columns were not equal.
I am not sure why this is happening.
Consider using
mapply
here so you can pass multiple arguments to your function of interest. A simple example - A function that requires two values, and we want to loop over each pair from two different vectors:So for your example,