使用apply系列为函数设置多个变量参数

时间:2014-10-10 14:42:41

标签: r

我有以下矢量和变量

x<-c(1,2,3,4,5,7,6,7,8,9,8,7,6,4,5,3,2,2,3,4,5,6,67,7,7)

wavelet<-c("d2","s2")
n.level<-c(1,2)
schirnkfun <- c("soft", "hard", "mid")    
threshfun<-c("universal", "adaptive", "minimax")
threshscale<-c(0.25,1.25)

#I select one variable from above and use lapply which yields results per variable.

library(wmtsa)
ws <- lapply(threshfun, function(k,x)
wavShrink(x,wavelet= "s2",n.level=2,shrink.fun="hard", thresh.fun= k,threshold=NULL,thresh.scale=1, xform="modwt", noise.variance=-1, reflect=TRUE),x=x)

我的问题是,是否可以使用apply系列将所有可变的变量组合和x向量传递给最后一个函数?例如

a)为不同变量的所有可能组合创建一个数据框,并将其传递给函数,以便函数以类似的方式对数据帧的行进行操作。如何将apply应用于原始数据的子集下面的简单表达式确保函数也必须考虑向量x。

A<-expand.grid(c(1,2), c(3,6), c(4,2), c(2,5,3), c(0.25,1.25))
apply(A[,c('Var1' ,  'Var5' , 'Var2' , 'Var3' , 'Var4')], 1, function (x) sum(x))

b)在向量上使用mapply(不确定输出是否是每个可变变量组合的结果,并且所有组合都将传递给函数)

# so here is my attempt to use apply to the combination of variables. First I create a data frame of variables.
A <- expand.grid(c("d2","s2"), c(1,2),c("soft", "hard"),c("universal", "adaptive", "minimax"),c(0.25,1.25))

# and write the function 
function(k,l,m,n,e,x) (wavShrink(x,wavelet= k,n.level= l,shrink.fun= m, thresh.fun= n,threshold=NULL,thresh.scale= e, xform="modwt", noise.variance=-1, reflect=TRUE)

#Attempt to pass the data frame for lapply without success possibly because lapply is for vectors or lists

A<-expand.grid(c("d2","s2"), c(1,2),c("soft", "hard"),c("universal", "adaptive", "minimax"),c(0.25,1.25))

ws<-lapply(A[,c('Var1' ,  'Var2' , 'Var3' , 'Var4' , 'Var5')],1, function(k,l,m,n,e,x)
wavShrink(x,wavelet= k,n.level= l,shrink.fun= m, thresh.fun= n, threshold=NULL,thresh.scale= e, xform="modwt", noise.variance=-1, reflect=TRUE),x=x)
#Error en match.fun(FUN) : '1' is not a function, character or symbol

#and for mapply which take multiple lists or vectors I pass the variables as vectors, however it fails as it seems I can not pass the x vector to the function

FUN<-function(x,k,l,m,n,e) (wavShrink(x,wavelet= k,n.level= l,shrink.fun= m, thresh.fun= n, threshold=NULL,thresh.scale= e, xform="modwt", noise.variance=-1, reflect=TRUE))
mapply(FUN, c("d2","s2"), c(1,2),c("soft", "hard"),c("universal", "adaptive", "minimax"),c(0.25,1.25),x=x)
#Error en wavShrink(x, wavelet = k, n.level = l, shrink.fun = m, thresh.fun = n,  : 
#Time series must contain more than one point

我是R的新手,并且对于apply函数来说是全新的,在尝试使用apply或函数时我可能犯了一些基本错误,但是在尝试了两天之后我决定写一个可能是一个简单的问题和任何帮助都将受到欢迎。

1 个答案:

答案 0 :(得分:0)

我在处理mapply方面取得了一些进展,所以我相信我可以回答我自己的问题

wavelet<-c("d2","s2")
n.level<-c(1,2)
schirnkfun <- c("soft", "hard", "mid")    
threshfun<-c("universal", "adaptive", "minimax")
threshscale<-c(0.25,1.25)
x<-c(1,2,3,4,5,7,6,7,8,9,8,7,6,4,5,3,2,2,3,4,5,6,6,7,7,7,5,6,7,7,8,8,9,0,9,0,8,7,5,4,3,3,4,4,4,4,3,2,2,1,2,3,4,5,6,5,7,8,8,9,9,0,2,3,4,2,3,5,5,2,4,6,7)

w1<- expand.grid(wavelet=wavelet,n.level=n.level,schirnkfun= schirnkfun,threshfun= threshfun,threshscale= threshscale, stringsAsFactors=FALSE)

result<-mapply(function(m,k,p,u,l,x)  (wavShrink(x, wavelet= m,n.level=k,shrink.fun= p,thresh.fun=u, threshold=NULL,thresh.scale= l, xform="modwt", noise.variance=-1, reflect=TRUE)), w1$wavelet,  w1$n.level , w1$schirnkfun,   w1$ threshfun, w1$threshscale ,MoreArgs=list(x=x))

colnames(result)=c(rownames(w1))

colums的数量等于w1的行数,每行来自expand,grid由函数计算。