在R中使用外部的矩阵元素计算

时间:2014-03-31 06:21:29

标签: r apply dplyr

我试图通过用户定义的函数计算矩阵的每个元素。我在for之后使用了双sapply循环,嵌套applyexpand.grid

# create dataset
ll = lapply(1:20, FUN = function(x) floor(runif(n=floor(runif(1,10,30)),1,50)))

# sapply line
doSapply = function(ll){
  m = sapply(1:length(ll),FUN = function(x){sapply(1:length(ll),FUN = function(y){if(length(intersect(ll[[x]],ll[[y]]))>1){return(1)}else{return(0)}})})

}


doFor = function(ll){
  m = matrix(rep(0,length(ll)*length(ll)),length(ll))
  # double for loop
  for(x in 1:length(ll)){
    for(y in 1:length(ll)){
      if(length(intersect(ll[[x]],ll[[y]]))>1){
        m[x,y] = 1
      } else{
        m[x,y] = 0
      } 
    }
  }
}

doApply = function(ll){
  x = expand.grid(1:length(ll),1:length(ll))
  vals = apply(x,1,FUN = function(x){
    if(length(intersect(ll[[x[1]]],ll[[x[2]]]))>1){
      return(1)
    } else{
      return(0)
    }
  } )
  matrix(vals,nrow=length(ll),byrow=FALSE)
#   return(matrix(vals,nrow=length(ll),byrow=FALSE))
}
timeSapply = system.time(doSapply(ll))
timeFor = system.time(doFor(ll))
timeApply = system.time(doApply(ll))

我正在寻找更优雅的解决方案(当然更快)。我正在尝试使用外部,我写道:

doOuter = function(ll){
  outer(1:length(ll),1:length(ll),FUN = function(x,y){if(length(intersect(ll[[x]],ll[[y]]))>1){return(1)}else{return(0)}})
}

我得到了Error in ll[[y]] : recursive indexing failed at level 2

我认为Outer需要将函数化为矢量化,所以我尝试了它:

doMatch = function(x,y,ll){
  if(length(intersect(ll[[x]],ll[[y]]))>1){return(1)}else{return(0)}
}

doMatVec = Vectorize(doMatch)
outer(1:length(ll),1:length(ll),doMatVec,ll )

我得到了Error in ll[[x]] : subscript out of bounds

我想我在Vectorize中可能会出错,关于我正在尝试做的一般想法,但我会在这里感谢一些帮助。 我也在关注plyr / dplyr - 没有太大的成功。

0 个答案:

没有答案