R_Language 板


LINE

※ 引述《fifish89 (OMG)》之銘言: : [問題類型]: : 想針對每行判別是否有包含某特定值 : [軟體熟悉度]: : 使用者(已經有用R 做過不少作品) : [問題敘述]: : 資料格式如下: : ID D1 D2 D3 D4....D50 : A 123 23 ......... 55 : B 24 005 : C 504 . . 002 : D 002 . . . : . . . ... : H . . 002 ... : . . . ... : XX 410 . ... : ============================ : CODE1<-有指定某些數字一 : CODE2<-有指定某些數字二 : . : . : . : CODE15<-有指定某些數字十五 : 這是醫院資料, : 每個人若最多有50個診斷欄位(D1-->D50), : 而CODE1...CODE15是每個疾病的指定疾病碼, : 想要判斷每個人是否有罹患這些疾病。 : EX: : 假設CODE1=002(中風(stroke)疾病碼), : 我就會去判斷每個診斷欄位中(D1-->D50)是否有002這個疾病碼, : ID=C,在D4中有抓到中風碼 : ID=D,在D1中有抓到中風碼 : ID=H,在D3中有抓到中風碼 : 希望輸出結果為 : ID stroke .... : A 0 : B 0 : C 1 : D 1 : . . : . . : H 1 : . . : XX . 程式好讀版:http://pastebin.com/MN73bPPH library(data.table) library(tidyr) library(dplyr) library(magrittr) N = 1e5 dat = data.table(ID = paste0("P", 1:N)) for (i in 1:50) eval(parse(text = paste0("dat %<>% mutate(D", i, "=paste0(sample(LETTERS, N, TRUE),", "sample(as.character(1:100), N, TRUE)))"))) # single search code1 = "D5" st = proc.time() a = dat %>% gather(D, code, -ID) %>% group_by(ID) %>% summarise(stroke = any(code == code1)) %>% distinct() proc.time() - st # user system elapsed # 0.42 0.05 0.47 # multiple search codes_search = c("D5", "E55", "Z2", "A96") st = proc.time() a = dat %>% gather(D, code, -ID) %>% group_by(ID) for (i in 1:length(codes_search)) eval(parse(text = paste0("a %<>% mutate(codes_search_", i, "=codes_search[", i, "])"))) b = a %>% group_by(ID) %>% summarise_(.dots = paste0( "any(code == codes_search_", 1:length(codes_search),")")) %>% setnames(c("ID", paste0("stroke", 1:length(codes_search)))) proc.time() - st # user system elapsed # 2.74 0.26 3.01 ## list search diseases = vector('list', 15) for (i in 1:length(diseases)) diseases[[i]] = paste0(sample(LETTERS, i, TRUE), sample(1:100, i, TRUE)) st = proc.time() a = dat %>% gather(D, code, -ID) %>% group_by(ID) out_list = llply(1:length(diseases), function(i){ a %>% setkey(ID) %>% group_by(ID) %>% summarise(stroke = any(code %in% diseases[[i]])) %>% setnames("stroke", paste0("stroke_", i)) }) out = Reduce(function(x, y) merge(x, y), out_list) proc.time() - st # user system elapsed # 8.88 0.36 9.23 ## faster way st = proc.time() a = dat %>% gather(D, code, -ID) %>% group_by(ID) eval(parse(text = paste("a %<>% summarise(", paste0("stroke_", 1:length(diseases) ,"=any(diseases[[", 1:length(diseases), "]] %in% code)", collapse = ","), ")"))) proc.time() - st # user system elapsed # 5.79 0.05 5.85 ## the fastest way st = proc.time() out2 = vector('list', length(diseases)) for (i in 1:length(diseases)) out2[[i]] = rowMeans(sweep(as.matrix(dat[,2:51, with = FALSE]), 2, 1:(ncol(dat)-1), function(x, y){matrix(x %in% diseases[[i]], nrow(dat))})) > 0 out2 = do.call(cbind, out2) %>% data.table %>% cbind(dat$ID, .) %>% setnames(c("ID", paste0("stroke_", 1:length(diseases)))) %>% tbl_dt(FALSE) proc.time() - st # user system elapsed # 3.20 0.52 3.71 all.equal(out2, a) # TRUE## the fastest way 關於multiple search部分,我稍微說明一下 發現summarise_裡面lazyeval部分的environment設定在data內部 可能是避免一些問題才這樣設計 所以沒辦法直接用.dots把global variables塞進去跑 只好先手動mutate進去之後 再一次summarise --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.27.107
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1437373001.A.DE6.html
1F:→ andrew43: C大有空可以為大家介紹這幾套你常用的資料整理套件技術 07/20 22:32
2F:→ andrew43: 這些東西非常好用,但沒接觸的朋友應該都看不懂。 07/20 22:33
3F:→ andrew43: 像我也是邊看邊猜,但目前用不到就沒認真吸收。XD 07/20 22:35
4F:→ celestialgod: 有空來寫dplyr跟tidyr的詳細用法 07/20 23:07
5F:推 fifish89: 感謝樓上各位高手,也努力學習dplyr技術中... 07/21 09:55
※ 編輯: celestialgod (123.205.27.107), 07/22/2015 14:46:55







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP