Clean table of descriptive statistics in R

jesse

New Member
Hello,

I am migrating to R from Stata. However, I am having much trouble producing a simple, clean table of descriptive statistics. In Stata, I can type:

summarize v1 v2 v3

and get a table with variable names in the first column followed by the number of observations, means, sds, and min/max statistics for each variable. I have tried many different things, but I have so far failed to produce something of similar simplicity in R.

Is there a way print simple descriptive statistics like this in R?

I know that this is a very basic question, and I appreciate any help I receive.

jesse

New Member
Thank you for your answer. Actually, I was aware of both summary() and describe(). However, it is obtaining the output in a simplified format that I am concerned with, with variables displayed in first column and the various statistics following:

Lazar

Phineas Packard
you could write your own function to produce something similar. I will whip something up or someone else will give me a few secs

Lazar

Phineas Packard
It is a rush job and not great but something like:
Code:
> data(iris)
> cleanSummary <- function(data) {
+   out <- apply(data,2,function(x) c(length(x), mean(x, na.rm=TRUE),sd(x, na.rm=TRUE),
+                                         min(x, na.rm=TRUE), max(x, na.rm=TRUE) ))
+   out<-data.frame(Variables=names(data), t(out))
+   names(out)<- c("Variables", "Obs", "Mean", "Std. Dev.", "Min", "Max")
+   row.names(out)<-NULL
+   out
+ }
>
> cleanSummary(iris[,1:4])
Variables Obs     Mean Std. Dev. Min Max
1 Sepal.Length 150 5.843333 0.8280661 4.3 7.9
2  Sepal.Width 150 3.057333 0.4358663 2.0 4.4
3 Petal.Length 150 3.758000 1.7652982 1.0 6.9
4  Petal.Width 150 1.199333 0.7622377 0.1 2.5

jesse

New Member
Thank you so much. This works perfectly and is precisely what I was after. Also, seeing this code may help me to solve similar problems like this in the future by myself, or at least get started. Cheers.