[问题] parSapply 无法运算

楼主: ruokcnn (Dean)   2017-08-02 14:47:52
[问题类型]:
程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
效能咨询(我想让R 跑更快)
[软件熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
小弟在用parSapply整理资料的时候写了一个function
myfunc <-function(x){
n <- as.data.frame(subset(Forthewin,客户帐号==unlist(Forthewin[x,4])&股票==unlist(Forthewin[x,8])&委托日期==unlist(Forthewin[x,20])))
z <- unique(n[,23])
if(length(grep("^1$",z))+length(grep("^-1$",z))==2 & if(length(n[which(n[,23]>0),21])>0) sum(n[,21]*n[,23])/sum(n[which(n[,23]>0),21])<0.2 ) 1 else if(length(grep("^-2$",z))+length(grep("^2$",z))==2) 2 else 0
}
结果回传error
parSapply(cl,index(Forthewin)[1:9],FUN=myfunc)
Error in checkForRemoteErrors(val) :
4 nodes produced errors; first error: 二元运算子中有非数值引数
发现如果里面只要包了dataframe特定row的运算(我不会称呼)
像这种:sum(n[,21]*n[,23])/sum(n[which(n[,23]>0),21])
就不行用parSapply的function运算(但普通sapply还是可以)
myfunc <- function(x){o <- sum(n[,21]*n[,23])/sum(n[which(n[,23]>0),21])}
parSapply(cl,index(Forthewin)[1:9],FUN=myfunc)
#Error in checkForRemoteErrors(val) :
# 4 nodes produced errors; first error: 二元运算子中有非数值引数
如果remove那行就可以正常运行
myfunc <-function(x){
n <- as.data.frame(subset(Forthewin,客户帐号==unlist(Forthewin[x,4])&股票==u
z <- unique(n[,23])
if(length(grep("^1$",z))+length(grep("^-1$",z))==2) 1 else if(length(grep("^
}
parSapply(cl,index(Forthewin)[1:9],FUN=myfunc)
#[1] 0 0 0 0 0 0 0 0 0
可以问为什么会这样或是可以怎么解决吗?
非本科系也没上过计概,对于程式运算逻辑有很多不懂的地方
感谢各位指点
[程式范例]:
[环境叙述]:
R 3.4.1
[关键字]:
Parallel
parSapply
snow
更新:
在万念俱灰之下我把cluster关掉再重开就可以了
stopCluster(cl)
cl <- makeCluster(4,type = "SOCK")
myfunc <-function(x){
n <- as.data.frame(subset(Forthewin,客户帐号==unlist(Forthewin[x,4])&股票==u
z <- unique(n[,23])
if(length(grep("^1$",z))+length(grep("^-1$",z))==2 & if(length(n[which(n[,23
}
parSapply(cl,index(Forthewin)[1:9],FUN=myfunc)
[1] 0 0 0 0 0 0 0 0 0
WHY??????
作者: f496328mm (为什么会流泪)   2017-08-02 15:49:00
推荐用 parallel 方便很多 不过要在linux环境下我以前也是都用snow 改用parallel之后觉得超方便

Links booklink

Contact Us: admin [ a t ] ucptt.com