Suppose you have the following dataframe:
data <- data.frame(a=runif(n=348), b=runif(n=348), c=runif(n=348))
and then suppose that you would like to apply the bld.mbb.bootstrap
function (from forecast
package) for every column.
With one column the code will go like this:
reps <- 500L
sim <- bld.mbb.bootstrap(data$a, reps)
Which returns a list of 500.
Question 1: How do I get to change all the names? To say iteration #i (inside the list).
Question 2: I want to make a for loop that does "sim" for all the columns in the dataframe. So I think it'd go like this:
for (i in names(data)){
sim <- bld.mbb.bootstrap(data$i, reps)
}
How can I make a list that stores (in this case 3) lists?
I want a list that contains 3 lists named like the variable: a, b, and c.
where a, b, c are lists that contain the iterations.
Thanks in advance!
CodePudding user response:
A base R approach using lapply
lapply(data, function(x) bld.mbb.bootstrap(x, reps))
or
setNames(lapply(colnames(data), function(x)
bld.mbb.bootstrap(data[,x], reps)), colnames(data))