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

作者: Edster (Edster)   2014-10-25 14:19:50
## 可參考我寫的這支, 功能類似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)

Links booklink

Contact Us: admin [ a t ] ucptt.com