是否有SAS PROC FREQ的R等效项?


18

有人知道R等于SAS PROC FREQ吗?

我试图一次为多个变量生成摘要描述性统计信息。


2
为什么这个问题结束了?它涉及数据可视化,并生成了一些有价值的响应。
z0lo

Answers:



9

汇总基数R中的数据只是一件令人头疼的事。这是SAS运作良好的领域之一。对于R,我推荐plyr包装。

在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;

plyr

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

8

我不使用SAS。因此,我无法评论以下是否重复SAS PROC FREQ,但这是在我经常使用的data.frame中描述变量的两种快速策略:

  • describeHmisc提供了有用的变量摘要,包括数字和非数字数据
  • describepsych提供数字数据的描述性统计信息

R示例

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

以下是输出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]

然后,下面是psych describe数字变量的输出:

> 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

3

我使用的是{EPICALC}中代码簿功能,该功能可提供数字变量的摘要统计信息以及带有级别标签和因子代码的频率表。http://cran.r-project.org/doc/contrib/Epicalc_Book.pdf(请参阅第50页)此外,这非常有用,因为它为定量变量提供了sd。

请享用 !

样本输出


1
+1(来自之前)。我真的很喜欢这种方式codebook()。1个问题是nas被删除,您可能希望将其包含在输出中。解决此问题(至少包括因数)的一种方法是使用?recode.is.na 1st(例如,“丢失”);对于数字变量,您可以立即在带有逻辑值的列左侧创建一个新变量is.na(),然后运行codebook()。不过,这有点混乱。
gung-恢复莫妮卡

3

您可以检出我的summarytools程序包(CRAN链接),该程序包包括类似Codebook的功能,以及markdown和html格式设置选项。

install.packages("summarytools")
library(summarytools)
dfSummary(CO2, style = "grid", plain.ascii = TRUE)

数据框摘要

二氧化碳

+------------+---------------+-------------------------------------+--------------------+-----------+
| Variable   | Properties    | Stats / Values                      | Freqs, % Valid     | N Valid   |
+============+===============+=====================================+====================+===========+
| Plant      | type:integer  | 1. Qn1                              | 1: 7 (8.3%)        | 84/84     |
|            | class:ordered | 2. Qn2                              | 2: 7 (8.3%)        | (100.0%)  |
|            | + factor      | 3. Qn3                              | 3: 7 (8.3%)        |           |
|            |               | 4. Qc1                              | 4: 7 (8.3%)        |           |
|            |               | 5. Qc3                              | 5: 7 (8.3%)        |           |
|            |               | 6. Qc2                              | 6: 7 (8.3%)        |           |
|            |               | 7. Mn3                              | 7: 7 (8.3%)        |           |
|            |               | 8. Mn2                              | 8: 7 (8.3%)        |           |
|            |               | 9. Mn1                              | 9: 7 (8.3%)        |           |
|            |               | 10. Mc2                             | 10: 7 (8.3%)       |           |
|            |               | ... 2 other levels                  | others: 14 (16.7%) |           |
+------------+---------------+-------------------------------------+--------------------+-----------+
| Type       | type:integer  | 1. Quebec                           | 1: 42 (50%)        | 84/84     |
|            | class:factor  | 2. Mississippi                      | 2: 42 (50%)        | (100.0%)  |
+------------+---------------+-------------------------------------+--------------------+-----------+
| Treatment  | type:integer  | 1. nonchilled                       | 1: 42 (50%)        | 84/84     |
|            | class:factor  | 2. chilled                          | 2: 42 (50%)        | (100.0%)  |
+------------+---------------+-------------------------------------+--------------------+-----------+
| conc       | type:double   | mean (sd) = 435 (295.92)            | 95: 12 (14.3%)     | 84/84     |
|            | class:numeric | min < med < max = 95 < 350 < 1000   | 175: 12 (14.3%)    | (100.0%)  |
|            |               | IQR (CV) = 500 (0.68)               | 250: 12 (14.3%)    |           |
|            |               |                                     | 350: 12 (14.3%)    |           |
|            |               |                                     | 500: 12 (14.3%)    |           |
|            |               |                                     | 675: 12 (14.3%)    |           |
|            |               |                                     | 1000: 12 (14.3%)   |           |
+------------+---------------+-------------------------------------+--------------------+-----------+
| uptake     | type:double   | mean (sd) = 27.21 (10.81)           | 76 distinct values | 84/84     |
|            | class:numeric | min < med < max = 7.7 < 28.3 < 45.5 |                    | (100.0%)  |
|            |               | IQR (CV) = 19.23 (0.4)              |                    |           |
+------------+---------------+-------------------------------------+--------------------+-----------+

编辑

在更新版本的summarytools中,该freq()函数(生成简单的频率表,就原始问题而言更关键)接受数据帧以及单个变量。有关交叉表(proc freq也可以),请参见该ctable()函数。

freq(CO2)

频率

CO2 $植物

类型:有序因子

          Freq   % Valid    % Valid Cum   % Total    % Total Cum
    Qn1      7      8.33           8.33      8.33           8.33
    Qn2      7      8.33          16.67      8.33          16.67
    Qn3      7      8.33          25.00      8.33          25.00
    Qc1      7      8.33          33.33      8.33          33.33
    Qc3      7      8.33          41.67      8.33          41.67
    Qc2      7      8.33          50.00      8.33          50.00
    Mn3      7      8.33          58.33      8.33          58.33
    Mn2      7      8.33          66.67      8.33          66.67
    Mn1      7      8.33          75.00      8.33          75.00
    Mc2      7      8.33          83.33      8.33          83.33
    Mc3      7      8.33          91.67      8.33          91.67
    Mc1      7      8.33         100.00      8.33         100.00
   <NA>      0                               0.00         100.00
  Total     84    100.00         100.00    100.00         100.00
CO2 $类型

类型:因子

                Freq   % Valid    % Valid Cum   % Total    % Total Cum
       Quebec     42     50.00          50.00     50.00          50.00
  Mississippi     42     50.00         100.00     50.00         100.00
         <NA>      0                               0.00         100.00
        Total     84    100.00         100.00    100.00         100.00
二氧化碳处理

类型:因子

               Freq   % Valid    % Valid Cum   % Total    % Total Cum
  nonchilled     42     50.00          50.00     50.00          50.00
     chilled     42     50.00         100.00     50.00         100.00
        <NA>      0                               0.00         100.00
       Total     84    100.00         100.00    100.00         100.00

2

谢谢大家的所有建议。我最终使用表或Rcmdr的numSummary函数加上apply:

apply(dataframe[,c('need_rbcs','need_platelets','need_ffp')],2,table) 

这工作得很好,并且不太方便。但是,我一定会尝试其中一些其他解决方案!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.