r - lapply on dataframe list using different column index -
i'm try feature selection on dataframe list using caret package. have different dataframes last 6 columns same. when trying apply model on single df works fine
# single dataframe mx.chem # name of single dataframe #define control mx.control <- rfecontrol(functions=rffuncs, method = "cv", number = 10) # run rfe mx.results <- rfe(mx.chem[,1:22], mx.chem[,23], sizes = c(1:22), rfecontrol = mx.control) print(mex.results)
but problem when try use lapply on list of df. code have until is
require(mlbench) require(caret) mylist # df list containing 3 df (i in 1:3) { my.control <- rfecontrol(functions=rffuncs, method = "cv", number = 10) # define control longdata <- length(i)-6 idxindustry <- longdata +1 my.results <- lapply(mylist, function(x) rfe ( x[,1:longdata], x[,idxindustry], data = x, sizes =c(1:longdata), rfecontrol = my.control)) }
i'm not sure if i'm using column index properly. have idea how fix make code work.
here 2 possible ways:
#using lapply mx.control <- rfecontrol(functions=rffuncs, method = "cv", number = 10) rfe.lst <- lapply(mylist, function(x) { longdata <- ncol(x)-6 rfe ( x[,1:longdata], x[,longdata + 1], sizes =c(1:longdata), rfecontrol = mx.control) }) #for loop mx.control <- rfecontrol(functions=rffuncs, method = "cv", number = 10) rfe.lst <- vector("list", 3) for(i in 1:3) { longdata <- ncol(mylist[[i]])-6 rfe.lst[[i]] <- rfe(mylist[[i]][,1:longdata], x[,longdata + 1], sizes=c(1:longdata), rfecontrol=mx.control) }
Comments
Post a Comment