作者chrisli7 (Vic)
看板R_Language
标题[问题](已更新)将多个Function输出的内容整理成矩阵
时间Tue Oct 4 01:27:18 2016
[问题类型]:
程式谘询(我想用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://webptt.com/cn.aspx?n=bbs/R_Language/M.1475515640.A.DA6.html
1F:→ celestialgod: 给一个简单能够执行的例子来说明会比较容易看懂你的 10/04 03:08
2F:→ celestialgod: 问题 10/04 03:08
3F:→ andrew43: 欢迎。先提供一个极简单的例子来说明问题即可。 10/04 08:23
※ 编辑: chrisli7 (58.114.223.14), 10/04/2016 22:24:11
4F:→ chrisli7: 前辈好,我已经修改过了,烦请前辈帮忙看看问题出在哪呢 10/04 22:25
※ 编辑: chrisli7 (58.114.223.14), 10/04/2016 22:26:28
※ 编辑: chrisli7 (58.114.223.14), 10/04/2016 22:27:05
5F:推 cywhale: rbind(occupation(bike), occupation(bus),...)应该可以 10/04 22:30
6F:→ cywhale: rbind不要写在function里面, function内回传t(df3[,..]) 10/04 22:32
7F:→ chrisli7: 可以耶!谢谢前辈!可是因为我整个Source类似的function 10/04 22:39
8F:→ chrisli7: 有超多个几十个到百个,请问有把他们全部放到rbind里面 10/04 22:40
9F:→ chrisli7: 的方法吗? 10/04 22:40
10F:→ chrisli7: 前辈逻辑真好一下就解出,我怎麽没想到,原来转个念而已 10/04 22:56
11F:推 cywhale: 不是前辈不敢当 你装purrr, data.table这两个pkg, 可用 10/04 23:11
12F:→ cywhale: dl<-lapply(name.lst,get) #name.lst=c("bus","bike"..) 10/04 23:13
13F:→ cywhale: rbindlist(map(dl, occupation)) 应该可以一次做完.. 10/04 23:14
14F:→ chrisli7: 谢谢cywhale大!真心感谢!祝福您有好报~ 10/10 01:02