[問題] parSapply 無法運算

看板R_Language作者 (Dean)時間8年前 (2017/08/02 14:47), 8年前編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
[問題類型]: 程式諮詢(我想用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?????? -- 作者 seacore07 (海角7號) 看板 Gossiping 標題 [問卦] 異性戀的男生跟女生 真的有純友誼嗎? 時間 Thu Dec 8 10:55:50 2016

有阿 我跟我老婆
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.146.38 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1501656479.A.A1E.html ※ 編輯: ruokcnn (223.136.146.38), 08/02/2017 14:50:42 ※ 編輯: ruokcnn (223.136.146.38), 08/02/2017 15:28:43

08/02 15:49, , 1F
推薦用 parallel 方便很多 不過要在linux環境下
08/02 15:49, 1F

08/02 15:49, , 2F
我以前也是都用snow 改用parallel之後覺得超方便
08/02 15:49, 2F
文章代碼(AID): #1PWNMVeU (R_Language)
文章代碼(AID): #1PWNMVeU (R_Language)