Re: [問題] 多個變數的迴圈如何處理?

看板R_Language作者 (casperxdd)時間7年前 (2016/12/24 05:28), 7年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/6 (看更多)
※ 引述《f496328mm (為什麼會流淚)》之銘言: : max.id=function(x2,name){ : value = name[ max(x2) == x2 ] : return(value) : } : x=matrix(rnorm(20),ncol=4) : name=c("A","B","C","D","E") : rownames(x)=name : x : tem = apply(x,2,function(x) max.id(x,name)) : tem : 應該還有更好的方法 : 我先提供一個 : 主要就是利用apply進行col運算 我來試一個 dplyr + reshape2 的解法 test.frame <- data.frame( name = c("A","B","C","D","E"), var1 = c(12,9,10,-5,2), var2 = c(9,29,30,29,1) ) library(reshape2) library(dplyr) test.frame %>% melt %>% group_by(variable) %>% filter(value == max(value)) 得到的output如下 name variable value <fctr> <fctr> <dbl> 1 A var1 12 2 C var2 30 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 68.52.13.65 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1482528500.A.138.html ※ 編輯: abc2090614 (68.52.13.65), 12/24/2016 05:30:34
文章代碼(AID): #1ONPRq4u (R_Language)
討論串 (同標題文章)
文章代碼(AID): #1ONPRq4u (R_Language)