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

机器算法验证 r 描述性统计 sas
2022-01-29 01:44:49

有人知道 R 相当于 SASPROC FREQ吗?

我正在尝试一次为多个变量生成摘要描述性统计数据。

4个回答

我使用tableand prop.table,但CrossTablegmodels包中可能会给你更接近 SAS 的结果。请参阅此链接

此外,要同时生成“多个变量的描述性统计”,您可以使用该summary函数;例如,summary(mydata)

在基础 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))

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

  • describeinHmisc提供了有用的变量摘要,包括数字和非数字数据
  • describeinpsych提供数值数据的描述性统计

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

你可以查看我的summarytools包(CRAN 链接),它包含一个类似代码本的功能,带有 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)

频率

二氧化碳$植物

类型:有序因子

          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
CO2$处理

类型:因子

               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