[英]how to use lapply on multiple variable?
我试图通过使用lapply而不是for循环来加速我的代码,以在lapply函数中提供函数变量,而我在lapply函数中提供所有函数变量。 但这给了我这个错误,这是什么问题?
Error in functionE(A[[i]],A,weight) :
argument "weight" is missing, with no default
代码结构:
weight<-weight
A<-functionB() #A is a list
C<-lapply(1:length(A),function(i,A,weight){
if(functionE(A[[i]],A,weight)==TRUE)
#some other functions
return(A)
})
编辑 :实际上, A
是图形列表(为简单起见,我这样布置)
for (i in 1:vcount(A[[1]])
{
#some function and if condition that needs((weight and, A))
#there is a function which return j
V(A[[1]])[i]$attribute2<-V(A[[1]])[j]$attribute1
}
所以我想访问A
中的for。
首先,全局环境中的R变量对于函数是可见的,因此您不必像以前那样传递它们。
我尚不清楚您要做什么,但是apply
函数族mapply
您的输入用作第一个变量,而其他变量则必须是常量(除非您使用mapply
)。 您需要将这些其他变量作为(s/l)apply
变量传递。
lappy(1:length(A), function(i,A,weight){
#whatever your function does
},A,weight)
在这种情况下, i
将从1到length(A)交替出现,而所有其他变量将保持不变。 请记住,您根本不需要添加A
和weight
,因为您可以在函数内使用它们而无需手动传递它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.