[R] basic table statistics
Dimitri Liakhovitski
ld7631 at gmail.com
Fri Apr 23 23:18:35 CEST 2010
There might be some package.
But you can also do something like:
results<-tapply(test$val,test$ty,function(x){
out1<-as.data.frame(length(x))
out2<-as.data.frame(mean(x))
out3<-as.data.frame(median(x))
out4<-as.data.frame(sd(x))
out<-cbind(out1,out2,out3,out4)
return(out)
})
for(i in 1:length(results)){
results[[i]]["Group.Name"]<-names(results)[i]
}
results<-do.call(rbind,results)
results
Dimitri
On Fri, Apr 23, 2010 at 3:48 PM, Maxim <deeepersound at googlemail.com> wrote:
> Hi,
>
>
> I have a very simple question, but I'm obviously not able to solve the
> problem on my own.
>
>
> I have a data.frame like
>
>
> sample(c("A","B","C"),size=20,replace = T)->type
>
> rnorm(20)->value
>
> data.frame(ty=type,val=value)->test
>
>
> There must be some built in functions, that will do some descriptive
> statistics with tabular output, in the end I like to have something like
>
>
> number of samples mean sd .............
>
> A 5
>
> B 9
>
> C 6
>
>
>
> So I need a function that counts the number of occurrences of factors in
> type and then does something like the *summary* function, but factor
> specific.
>
>
> I tried:
>
>
> vector()->Median
>
> vector()->SD
>
> vector()->Mean
>
>
> as.data.frame(table(type))->int
>
>
> for (count in c(1:(nrow(int))))
>
> {
>
> subset(test, ty==as.character(int$type[count])) -> subtest
>
> median(subtest$val)->Median[count]
>
> sd(subtest$val)->SD[count]
>
> mean(subtest$val)->Mean[count]
>
> }
>
>
> cbind(int,Median,SD,Mean)
>
>
> This works, but: isn't this much too complicated, I bet there is such
> functionality embedded in the base packages, but I cannot find it.
>
>
> Maxim
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Dimitri Liakhovitski
Ninah.com
Dimitri.Liakhovitski at ninah.com
More information about the R-help
mailing list