I'm trying to make function to return statistics by group like followings. To make statistics by group, I used subset in this function code. But errors occurred when argument 'y' is applied to 'subset'. How can I solve this problem? I'll wait wisdom of yours. It's sure that tapply can be used, but my intention is to make function. Thank you.
sbyg<-function(dt,grp,y) {
# dt=data.frame, grp=group variable, y=value variable
ng<-length(unique(grp))
x<-as.vector(unique(grp))
statis<-matrix(nrow=ng,ncol=6)
for (i in 1:ng) {
dta<-dt[grp==x[i],]
attach(dta)
statis[i,1]<-nrow(dta) # 건수
statis[i,2]<-colSums(!is.na(dta))[1] # nonmiss건수
statis[i,3]<-mean(dta[,y],na.rm=TRUE) # 평균
statis[i,4]<-median(dta[,y],na.rm=TRUE) # 중위수
statis[i,5]<-min(dta[,y],na.rm=TRUE)
statis[i,6]<-max(dta[,y],na.rm=TRUE)
detach(dta)
}
rownames(statis)<-x
colnames(statis)<-c("count","nonmiss","mean","median","min","max")
statis
}
sbyg(iris,Species,Sepal.Length) # error occurs