简体   繁体   English

"将常量参数传递给 R 中的 mapply() 函数时出错"

[英]Error in passing a constant argument to mapply() function in R

I've created a function that run FastICA on a dataset with different number of components and it returns ICA signals (S matrix) but in a long format.我创建了一个在具有不同数量组件的数据集上运行 FastICA 的函数,它返回 ICA 信号(S 矩阵),但格式很长。

compute_ICA<- function(r)
  
{
   res_ICA <- fastICA(df, r, alg.typ = "parallel", fun = "logcosh", alpha = 1,
             method = "R", row.norm = FALSE, maxit = 200,
             tol = 0.0000001, verbose = FALSE)
   df_long<-reshape2::melt(as.data.frame(res_ICA$S))
   return(df_long)
}

Now I want to repeat this function 100 times for one specific r (number of components) and keep track of the iteration number.现在我想为一个特定的 r(组件数)重复此函数 100 次,并跟踪迭代次数。 So I tried this :所以我尝试了这个:

iterate_ICA<-function(r,t)
{for (i in r)
{
  res<-mapply(compute_ICA, t,SIMPLIFY = FALSE)
  res_unzip<-do.call(rbind,res)
  res_unzip$iteration<-i
  return(res_unzip)
}
  }

But when I try applying the iterate_ICA() function like this I get this error:但是当我尝试像这样应用 iterate_ICA() 函数时,我得到了这个错误:

res_zip<-mapply(iterate_ICA, 1:100,3,SIMPLIFY = FALSE)

Error in dots[[1L]][[1L]] : object of type 'closure' is not subsettable点 [[1L]][[1L]] 中的错误:“闭包”类型的对象不是子集

Does anyone know what's wrong with this function?有谁知道这个功能有什么问题? Thanks in advance!提前致谢!

I cannot reproduce the error, however, I think you misunderstood what mapply<\/code> does.我无法重现该错误,但是,我认为您误解了mapply<\/code>的作用。

When you apply mapply<\/code> to a function FUN<\/code> , with arguments that are lists or vectors (consider that R basic types like numbers or characters are always vectors), the function FUN<\/code> is called iteratively on the first element of all the arguments, then on the second, etc. Arguments are recycled if necessary.当您将mapply<\/code>应用于函数FUN<\/code>时,参数是列表或向量(考虑 R 基本类型,如数字或字符始终是向量),函数FUN<\/code>在所有参数的第一个元素上迭代调用,然后在第二个元素上迭代调用等。如有必要,参数将被回收。

For instance:例如:

fn <- function(num, let) {
  data.frame(num = num, let = let)
}

mapply(FUN = fn, 1:5, letters[1:5], SIMPLIFY = FALSE)

##> [[1]]
##>   num let
##> 1   1   a
##> 
##> [[2]]
##>   num let
##> 1   2   b
##> 
##> [[3]]
##>   num let
##> 1   3   c
##> 
##> [[4]]
##>   num let
##> 1   4   d
##> 
##> [[5]]
##>   num let
##> 1   5   e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM