# Is there an R equivalent of SAS PROC FREQ?

Does anyone know of an R equivalent to SAS PROC FREQ?

I am trying to generate summary descriptive statistics for multiple variables at once.

-
Why was this question closed? It relates to data visualization and generated several worthwhile responses. –  z0lo Feb 28 at 14:26

I use table and prop.table, but CrossTable in the gmodels package might give you results even closer to SAS. See this link.

Also, to generate "descriptive statistics for multiple variables at once," you would use the summary function; e.g., summary(mydata).

-
In addition, I warmly recommend the vcd package, but see the accompanying vignette: Working with categorical data with R and the vcd and vcdExtra packages. –  chl Aug 4 '11 at 19:19

Summarising data in base R is just a headache. This is one of the areas where SAS works quite well. For R, I recommend the plyr package.

In SAS:

/* tabulate by a and b, with summary stats for x and y in each cell */
proc summary data=dat nway;
class a b;
var x y;
output out=smry mean(x)=xmean mean(y)=ymean var(y)=yvar;
run;


with plyr:

smry <- ddply(dat, .(a, b), summarise, xmean=mean(x), ymean=mean(y), yvar=var(y))

-

I don't use SAS; so I can't comment on whether the following replicate SAS PROC FREQ, but these are two quick strategies for describing variables in a data.frame that I often use:

• describe in Hmisc provides a useful summary of variables including numeric and non-numeric data
• describe in psych provides descriptive statistics for numeric data

### R Example

> library(MASS) # provides dataset called "survey"
> library(Hmisc) # Hmisc describe
> library(psych) # psych describe


The following is the output of Hmisc describe:

> Hmisc::describe(survey)
survey

12  Variables      237  Observations
----------------------------------------------------------------------------------------------------------------------
Sex
n missing  unique
236       1       2

Female (118, 50%), Male (118, 50%)
----------------------------------------------------------------------------------------------------------------------
Wr.Hnd
n missing  unique    Mean     .05     .10     .25     .50     .75     .90     .95
236       1      60   18.67   16.00   16.50   17.50   18.50   19.80   21.15   22.05

lowest : 13.0 14.0 15.0 15.4 15.5, highest: 22.5 22.8 23.0 23.1 23.2
----------------------------------------------------------------------------------------------------------------------
NW.Hnd
n missing  unique    Mean     .05     .10     .25     .50     .75     .90     .95
236       1      68   18.58   15.50   16.30   17.50   18.50   19.72   21.00   22.22

lowest : 12.5 13.0 13.3 13.5 15.0, highest: 22.7 23.0 23.2 23.3 23.5
----------------------------------------------------------------------------------------------------------------------
[ABBREVIATED OUTPUT]


Then below is the output of psych describe for the numeric variables:

> psych::describe(survey[,sapply(survey, class) %in% c("numeric", "integer") ])
var   n   mean    sd median trimmed   mad    min   max range  skew kurtosis   se
Wr.Hnd   1 236  18.67  1.88  18.50   18.61  1.48  13.00  23.2 10.20  0.18     0.36 0.12
NW.Hnd   2 236  18.58  1.97  18.50   18.55  1.63  12.50  23.5 11.00  0.02     0.51 0.13
Pulse    3 192  74.15 11.69  72.50   74.02 11.12  35.00 104.0 69.00 -0.02     0.41 0.84
Height   4 209 172.38  9.85 171.00  172.19 10.08 150.00 200.0 50.00  0.22    -0.39 0.68
Age      5 237  20.37  6.47  18.58   18.99  1.61  16.75  73.0 56.25  5.16    34.53 0.42

-

I use the codebook function from {EPICALC} which gives summary statistics for a numeric variable and a frequency table with level labels and codes for factors. http://cran.r-project.org/doc/contrib/Epicalc_Book.pdf (see p.50) Moreover, this is very useful because it provides sd for quantitative variables.

Enjoy !

-
+1 (from earlier). I really like the way codebook() lays this out. 1 issue is that nas are dropped, which you may want included in your output. 1 way to deal w/ this (at least w/ factors) is to use ?recode.is.na 1st (eg, to "missing"); for numeric variables, you can create a new variable immediately to the left of the column w/ a logical value based on is.na(), then run codebook(). It is a bit of a kluge, though. –  gung Dec 4 '12 at 15:35
apply(dataframe[,c('need_rbcs','need_platelets','need_ffp')],2,table)