我正在处理一个可能很大的数据集,并且正在努力对其应用函数。
第 1 步:让我们创建一些数据。
n<-3 #this number gets larger in application.
precision<-0.3 #This number gets smaller in application.
support<-matrix(seq(0,1,by=precision), ncol=1)
support_n<-as.matrix(combn(support, n))
foo<-function(x,y){
part_1<-mean(x) # note x is a vector
out<-part_1+y #note y is not a vector
out
}
第 2 步:我打算制作这个多核,因为随着 n 变大和精度变高,地狱就会松动。因此- if-then 循环除了少量之外是不可用的,大部分工作必须通过 apply 来完成,所以我可以使用 multicore 包中的“mcapply”。我想对对象 support_n 中的列元素的每个组合使用 foo 函数
第 3 步:正确的输出 - (我不能用 apply 来做到这一点)
out_final<-support_n #this will just be a placeholder for the right answers.
system.time(
for(j in 1:dim(support_n)[2]){ #
for(i in 1:n){
out_final[i,j]<-foo(support_n[-i,j],support_n[i,j])
}
}
)
我的问题:如何使用 apply 创建 out_final 以便能够与 mcapply 并行分发工作?