Re: [問題] for loop 迴圈命名變數

看板R_Language作者 (天)時間9年前 (2016/10/21 20:23), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《pinlateng (中肯不嘴砲)》之銘言: : [問題類型]: : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : [軟體熟悉度]: : 新手(沒寫過程式,R 是我的第一次) : [問題敘述]: : 我現在將一份問卷匯入到 R 之後,希望依照某行的數值加以分類,再將分類結果 : 用迴圈命名以便後續處理 : [程式範例]: : 有個 data frame:data : 裏頭有一行 column_a,值是 1~5 : 現在想以 column_a 的數值為條件將特定列抓出來指定成一個變數 : 也就是把 : column_a_1 <- data[data$column_a == 1,] : column_a_2 <- data[data$column_a == 2,] : . : . : . : 濃縮成類似這樣的感覺: : for (x in c(1:5)){ : column_a[x] <- data[data$column_a == x,] : } : [環境敘述]: : : R version 3.3.1 (2016-06-21) : Platform: x86_64-w64-mingw32/x64 (64-bit) : Running under: Windows >= 8 x64 (build 9200) : [關鍵字]: : 迴圈 變數 命名 不建議這麼做,我建議下面的做法 dataList <- split(data, data$column_a) for (i in 1:length(dataList)){ # do something with dataList[[i]] } 至於你要的做法,自行?assign或是eval + parse -- R資料整理套件系列文: magrittr #1LhSWhpH (R_Language) https://goo.gl/OBto1x data.table #1LhW7Tvj (R_Language) https://goo.gl/QFtp17 dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/GcfNoP tidyr #1Liqls1R (R_Language) https://goo.gl/pcq5nq pipeR #1NXESRm5 (R_Language) https://goo.gl/cDIzTh -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.235.91.146 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1477052610.A.1AD.html

10/21 23:51, , 1F
好的,我來試試看。謝謝!
10/21 23:51, 1F
文章代碼(AID): #1O2WZ26j (R_Language)
討論串 (同標題文章)
文章代碼(AID): #1O2WZ26j (R_Language)