Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用不同的列索引对数据帧列表进行lappy_R_Lapply_R Caret - Fatal编程技术网

R 使用不同的列索引对数据帧列表进行lappy

R 使用不同的列索引对数据帧列表进行lappy,r,lapply,r-caret,R,Lapply,R Caret,我尝试使用插入符号包在数据帧列表上进行功能选择。我有不同的数据帧,但最后6列是相同的。当我尝试在单个df上应用该模型时,效果很好 # For a single dataframe mx.chem # the name of my single dataframe #define the control mx.control <- rfeControl(functions=rfFuncs, method = "cv", number = 10) # run the rfe

我尝试使用插入符号包在数据帧列表上进行功能选择。我有不同的数据帧,但最后6列是相同的。当我尝试在单个df上应用该模型时,效果很好

# For a single dataframe
mx.chem # the name of my single dataframe
#define the control   
mx.control <- rfeControl(functions=rfFuncs, method = "cv", number = 10) 
# run the rfe     
mx.results <- rfe(mx.chem[,1:22], mx.chem[,23], sizes = c(1:22), rfeControl = mx.control)
print(mex.results)
但我的问题是,当我试图在df列表上使用lappy时。到目前为止,我掌握的代码是

 require(mlbench)
 require(caret)
 mylist # is a df list containing 3 df 
  for (i in 1:3) {
  my.control <- rfeControl(functions=rfFuncs, method = "cv", number = 10)  # define the 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))
  }

我不确定是否正确使用了列索引。有人知道如何修复我的代码吗。谢谢

您的代码没有按您的想法执行。lengthi将始终为1,因为i是循环索引,取值为1到3。你的意思是:

length(mylist[[i]])
请注意双括号。这就是从列表中选择元素的方式,在本例中为数据框。如果使用单括号,则返回一个包含所需元素的列表

但这仍然不是你的目标。如果要在代码中更改该行,则有2个循环:

一个外部循环,每次基于单个数据帧创建longdata和idxindustry。 在所有三个数据帧上使用longdata和idxindustry的值的内部lappy循环。 请记住,lappy接受列表中的每个元素,并将其作为第一个参数传递给指定的函数。因此,您可以在单圈中执行此操作,如下所示:

my.control <- rfeControl(functions=rfFuncs, method = "cv", number = 10)  

my.results <- lapply(mylist, function(x){
# x becomes one of the data frames in the list mylist here, so you can
# treat it like a data frame in the code below
  longdata <- length(x) - 6
  idxindustry <- longdata +1
  rfe( x[,1:longdata], x[,idxindustry], data = x, 
      sizes =c(1:longdata), rfeControl = my.control)
})

然后,根据手头的数据帧,使用longdata和IdxinIndustry运行rfe。注意,为了提高性能,我将对rfeControl的调用放在Lappy循环之外。

这里有两种可能的方法:

#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)
}

似乎没有..的数据参数。它当前正在传递给模型配件。这是故意的吗?我刚刚编辑了我的问题。我的问题是当我想使用lapply时。我不知道如何在listlongdata中包含的数据帧上指定列请解释通过1 2 3循环的for循环使用for循环或lappy,而不是两者都使用