[問題] (已更新)將多個Function輸出的內容整理成矩陣

看板R_Language作者 (Vic)時間7年前 (2016/10/04 01:27), 7年前編輯推噓2(2012)
留言14則, 4人參與, 最新討論串1/1
[問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: 新手(沒寫過程式,R 是我的第一次) [問題敘述]: 各位前輩好,因為自學R,見識可能短淺,實在想不出方法, 希望前輩可以提供想法,謝謝! 我目前遇到的問題是,我目前有寫幾個Function,然後把資料集丟入, 在source區裡面,寫成以下的方式: function1(data1) function1(data2) function2(data3) . . function3(data7) 如果達成if條件,會return出一個矩陣, 不知道是否有辦法自動讓這些判斷出來的矩陣, 能夠用rbind的方式或是其他方式堆疊成一個新的矩陣呢? 我用的方式是,先建立一個空矩陣, 我在function中最後寫這個 newmatrix <- rbind(newmatrix,df3[,criteria]) 就是希望能夠在function執行完後可以重新覆寫我設立的矩陣讓他跑完, 可是實際上第一個function執行完後跑出的newmatrix並不會真的改變。 我在Console中打newmatrix,跑出來仍都是NA。 很抱歉我的觀念也許不足,想請前輩們指點,謝謝!! 因為我的function有點多,PO上來解釋也會搞得很複雜,若有前輩需要我再補上, 謝謝!! -------------------------------------------------------- 謝謝前輩!不好意思剛下班現在才回。 以下是其中一個Function,會這樣寫是因為資料裡面是複選題, 其他若還有需要補充解釋的煩請告訴我,我會盡快回答當時為何這麼寫的。 我是用子集跟原本的資料集做比較,跑出一個criteria去選要return的資料 (上述的if條件我已經修改掉了) occupation <- function(x){ student<-x %>% filter(grepl("學生",occupation)) %>% nrow() salaryman<-x %>% filter(grepl("上班族",occupation)) %>% nrow() housekeeper<-x %>% filter(grepl("家管",occupation)) %>% nrow() turist<-x %>% filter(grepl("觀光客",occupation)) %>% nrow() others<-x %>% filter(grepl("其他",occupation)) %>% nrow() sum <- sum(student,salaryman,housekeeper,turist,others) percentage <- round((c(student,salaryman,housekeeper,turist,others)/sum),digits = 4) df1 <- matrix(c("學生","上班族","家管","觀光客","其他",student,salaryman,housekeeper,turist,others,percentage),nrow = 3,ncol = 5,byrow = T) student <- tasty %>% filter(grepl("學生",occupation)) %>% nrow() salaryman <- tasty %>% filter(grepl("上班族",occupation)) %>% nrow() housekeeper <- tasty %>% filter(grepl("家管",occupation)) %>% nrow() turist <- tasty %>% filter(grepl("觀光客",occupation)) %>% nrow() others <- tasty %>% filter(grepl("其他",occupation)) %>% nrow() sum <- sum(student,salaryman,housekeeper,turist,others) percentage_tasty <- round((c(student,salaryman,housekeeper,turist,others)/sum),digits = 4) difference_of_percentage <- percentage - percentage_tasty index <- percentage*difference_of_percentage df3 <- rbind(df1,difference_of_percentage,index) rownames(df3) <- c("名稱","次數","比例","與母體比例差","指數") criteria <- df3[4,] >= 0.015 new_matrix <- rbind(new_matrix,t(df3[,criteria])) return(new_matrix) } 以下是跑出的結果 > occupation(bike) 名稱 次數 比例 與母體比例差 指數 [1,] NA NA NA NA NA [2,] "學生" "8" "0.32" "0.1472" "0.047104" [3,] "觀光客" "1" "0.04" "0.0189" "0.000756" [4,] "其他" "3" "0.12" "0.0795" "0.00954" 但是這個表格沒有辦法堆疊越來越多,再按別的function又會重跑 > occupation(bus) 名稱 次數 比例 與母體比例差 指數 [1,] NA NA NA NA NA [2,] "學生" "19" "0.2262" "0.0534" "0.01207908" 請問前輩我該怎麼寫呢?感激不盡!! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 58.114.223.14 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1475515640.A.DA6.html

10/04 03:08, , 1F
給一個簡單能夠執行的例子來說明會比較容易看懂你的
10/04 03:08, 1F

10/04 03:08, , 2F
問題
10/04 03:08, 2F

10/04 08:23, , 3F
歡迎。先提供一個極簡單的例子來說明問題即可。
10/04 08:23, 3F
※ 編輯: chrisli7 (58.114.223.14), 10/04/2016 22:24:11

10/04 22:25, , 4F
前輩好,我已經修改過了,煩請前輩幫忙看看問題出在哪呢
10/04 22:25, 4F
※ 編輯: chrisli7 (58.114.223.14), 10/04/2016 22:26:28 ※ 編輯: chrisli7 (58.114.223.14), 10/04/2016 22:27:05

10/04 22:30, , 5F
rbind(occupation(bike), occupation(bus),...)應該可以
10/04 22:30, 5F

10/04 22:32, , 6F
rbind不要寫在function裡面, function內回傳t(df3[,..])
10/04 22:32, 6F

10/04 22:39, , 7F
可以耶!謝謝前輩!可是因為我整個Source類似的function
10/04 22:39, 7F

10/04 22:40, , 8F
有超多個幾十個到百個,請問有把他們全部放到rbind裡面
10/04 22:40, 8F

10/04 22:40, , 9F
的方法嗎?
10/04 22:40, 9F

10/04 22:56, , 10F
前輩邏輯真好一下就解出,我怎麼沒想到,原來轉個念而已
10/04 22:56, 10F

10/04 23:11, , 11F
不是前輩不敢當 你裝purrr, data.table這兩個pkg, 可用
10/04 23:11, 11F

10/04 23:13, , 12F
dl<-lapply(name.lst,get) #name.lst=c("bus","bike"..)
10/04 23:13, 12F

10/04 23:14, , 13F
rbindlist(map(dl, occupation)) 應該可以一次做完..
10/04 23:14, 13F

10/10 01:02, , 14F
謝謝cywhale大!真心感謝!祝福您有好報~
10/10 01:02, 14F
文章代碼(AID): #1NyfJusc (R_Language)