[問題] 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