Re: [問題] 找出重複數據 平均後刪掉其餘重複列
## 可參考我寫的這支, 功能類似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
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章