Re: [問題] 找出重複數據 平均後刪掉其餘重複列

看板R_Language作者 (Edster)時間10年前 (2014/10/25 14:19), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
## 可參考我寫的這支, 功能類似Excel的 AverageIfs,... ## Author: Edster.Shih library(compiler) ## statifs StatIfs <- function(data, concol = grep("SiteID", colnames(data)), Con = NULL, U = NULL, stat=1:9, LL=0, cri = 0, relativeN=TRUE){ data = data.matrix(data) if(length(Con) == 0) Con <- data[,concol] if(length(U) == 0) U <- unique(Con) TT =list(); M = data.frame(matrix(NA, nrow=length(U), ncol=dim(data)[2], dimnames=list(U, colnames(data)))) for(n in 1:9) TT[[n]] <- M; names(TT) = c("N","Min", "Q1", "Median", "Q3", "Max", "Mean", "Sd", "Sum") for(i in 1:length(U)){ for(j in 1:dim(data)[2]){ s = data[Con==U[i],j]; if(relativeN) N=length(s[s>=LL])/length(s) else N=length(s[s>=LL]) if(length(s[!is.na(s)])==0 | N < cri){S = rep(NA, 6)}else{q = quantile(s, c(0,0.25,0.5,0.75,1),na.rm=T) S = c(N, q[1], q[2], q[3], q[4], q[5], mean(s,na.rm=T), sd(s, na.rm=T), sum(s, na.rm=T))} for(n in 1:9) TT[[n]][i,j] = S[n] } } return(TT) } StatIfs <- cmpfun(StatIfs) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.24.127.66 ※ 文章網址: http://www.ptt.cc/bbs/R_Language/M.1414217993.A.BC0.html
文章代碼(AID): #1KIq49l0 (R_Language)
文章代碼(AID): #1KIq49l0 (R_Language)