R_Language 板


LINE

※ 引述《YangPeiHung (楊培宏)》之銘言: : [問題類型]: : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : [軟體熟悉度]: : 入門(寫過其他程式,只是對語法不熟悉) : [問題敘述]: : 目前有4個學生與不同科目的試題共10份,由電腦隨機控制他們可以作答的時間間隔, : 想要觀察的是他們在同時作答的時候的考試表現,資料格式如下 : Examtable : StudentID examID start(sec) end(sec) average(score/sec) : 001 1 A D 0.05 : 001 1 G K 0.63 : ...以此類推 : 因為要轉換成一個自創的標籤為:(examID)-(start)-(end) : 要觀察他們的同時作答秒數區間,就要把每個人在同一份試卷的作答秒數區間取交集 : 例如:紅色為有作答的秒數 : start|ABCD|EF|GHIJK|LMNO|PQRS|TUVW|XYZ12345|end 學生1 : start|ABCDE|FGH|IJKLMN|OPQ|RSTUVWXYZ|12|345|end 學生2 : start|ABCD|EFGH|IJK|LMNOPQ|RS|TUVW|XYZ|12|345|end 取交集 : 新的標籤就是1-A-D 1-I-K 1-R-S 1-X-Z 1-3-5 ,以此類推, : 並且做出一個新的table : rownames就是新標籤,colnames是studentID 中間要填入的就是average(score/sec) : (這裡假設在作答秒數內分數分配為uniform, : 並且每份試卷的最開始與最後結束考試時間等長) : StudentID_1 StudentID_2 ...... : 1-(A)-(D) 0.05 score/sec ...... : 1-(I)-(K) 0.63 score/sec ...... : ....以此類推 : [程式範例]: : 取intersect的程式碼運行上沒有問題 : 但是不知道如何回測並且生成新標籤與填入平均分數 : for (i in 1:10){ : ExamTemp<- Examtable[,c(1:4)] : ExamTemp1<-subset(ExamTemp, ExamTemp$examID =="i")[,-2] : intersect<-function(start, end, id, overlap=length(unique(id))) { : dd<-rbind(data.frame(pos=start, event=1), data.frame(pos=end, event=-1)) : dd<-aggregate(event~pos, dd, sum) : dd<-dd[order(dd$pos),] : dd$open <- cumsum(dd$event) : r<-rle(dd$open>=overlap) : ex<-cumsum(r$lengths-1 + rep(1, length(r$lengths))) : sx<-ex-r$lengths+1 : cbind(dd$pos[sx[r$values]],dd$pos[ex[r$values]+1]) : } : with(ExamTemp1, intersect(Start,End,StudentID,length(unique(StudentID)))) ->df : 如何利用df這個intersect的矩陣回測原本的資料並且填入新標籤與平均 : } : [環境敘述]: : R-3.3.2 這問題,我覺得解起來好難XD 而且我看不懂你的intersect的思維Orz,只好自己幹一個XD 好讀版:https://pastebin.com/8R1iXjcz library(foreach) library(iterators) library(data.table) library(pipeR) # data generation set.seed(10) k <- 1 outList <- foreach(v = iter(matrix(sample(3:29, 6000, TRUE), 1000), by = "row")) %:% when(k <= 4) %do% { if (all(diff(sort(v)) > 2)) { k <- k + 1 return(data.table(studentID = k, matrix(c(1, sort(v), 31), 4, 2, TRUE, list(NULL, c("Start", "End"))))) } else return(NULL) } outDT <- rbindlist(outList) %>>% `[`(j = `:=`(studentID = match(studentID, sort(unique(studentID))), avgScore = abs(rnorm(nrow(.))))) # studentID Start End avgScore # 1: 1 1 3 0.4605151 # 2: 1 6 10 0.2350253 # 3: 1 19 22 0.6432573 # 4: 1 25 31 0.9131981 # 5: 2 1 4 0.9882860 # 6: 2 7 11 0.1127413 # 7: 2 16 20 1.4900499 # 8: 2 26 31 0.4432356 # 9: 3 1 5 1.3623441 # 10: 3 10 14 1.0452357 # 11: 3 21 25 0.2339315 # 12: 3 28 31 2.5524180 # 13: 4 1 4 1.7687187 # 14: 4 7 10 0.6595706 # 15: 4 19 23 0.3707332 # 16: 4 26 31 0.5928033 # find overlap iter <- isplit(outDT, outDT$studentID) resDT <- copy(iter$nextElem()$value) %>>% `[`(j = `:=`(studentID = NULL)) setkey(resDT, Start, End) while (TRUE) { v <- tryCatch(iter$nextElem(), error = function(e) e) if (any(class(v) == "error")) break resDT <- foverlaps(v$value, resDT, type = "any", nomatch = 0) %>>% `[`(j = `:=`(Start = pmax(Start, i.Start), End = pmin(End, i.End))) %>>% `[`(j = .(Start, End)) setkey(resDT, Start, End) } # Start End # 1: 1 3 # 2: 10 10 # 3: 28 31 # 得到最後的答案 finalResDT <- foreach(it = isplit(outDT, outDT$studentID), .final = rbindlist) %do% { foverlaps(it$value, resDT, type = "any", nomatch = 0) %>>% `[`(j = avgScore := (i.End-End+1)/(Start-i.Start+1) * avgScore) %>>% `[`(j = .(Start, End, studentID, avgScore)) } %>>% dcast(Start + End ~ studentID, val.var = "avgScore") %>>% setnames(as.character(1:(ncol(.)-2)), paste0("studentID-", 1:(ncol(.)-2))) # Start End studentID-1 studentID-2 studentID-3 studentID-4 # 1: 1 3 0.46051506 1.97657201 4.087032 3.5374375 # 2: 10 10 0.04700506 0.05637067 5.226179 0.1648927 # 3: 28 31 0.22829953 0.14774520 2.552418 0.1976011 有十個考試就把後面兩段code包成函數,一次丟一個考試的outDT進來計算 最後合併再記得多加一個examID回來就好 -- R資料整理套件系列文: magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9 data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.233.82.44
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1492002777.A.1A7.html
1F:推 YangPeiHung: 後面的回測與填入部分可以運行!!非常感謝你 04/13 10:10
2F:→ YangPeiHung: 但我的交集這邊跟你不一樣的是我沒有同一秒的交集,不 04/13 10:12
3F:→ YangPeiHung: 過沒有大影響,我先看看還有什麼狀況~ 04/13 10:13
4F:推 YangPeiHung: 出現這個問題: Aggregate ffunction missing, defa 04/13 11:34
5F:推 YangPeiHung: default to length 04/13 11:35
這個應該是message
6F:→ YangPeiHung: 傳遞了兩個引數給'length' 但它需要一個 04/13 11:36
7F:推 YangPeiHung: 補充一下 他是Error in .fun (value[0], ...) 04/13 12:16
這個要看你的code以及資料,如果只是單純用我的資料出現問題,請再推文告知
8F:推 YangPeiHung: 我後來改用xtabs 就解決了這個問題,這兩個函數差異 04/13 20:08
9F:→ YangPeiHung: 在哪? 04/13 20:08
不懂你改在哪... ※ 編輯: celestialgod (111.246.26.70), 04/13/2017 20:46:00
10F:推 YangPeiHung: 已經回文貼出~ 04/13 21:44







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燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP