[R language Statistics] frequency tables

Source: Internet
Author: User

The frequency tables are a very basic and important concept in statistics, and we will explain its basic usage here.

First we need to load the data and see the basic information of the data

Install.packages (' vcd ') #安装vcd包, where available data arthritislibrary (VCD) are loaded into the required thread pack:grid> Head (arthritis) ################     ################################ #ID Treatment Sex age Improved1 treated Male Some2 29 treated Male  None3 treated Male-None4-treated Male-Marked5-treated Male, Marked6 treated Male Marked> Class (arthritis) ################################################[1] "Data.frame" > Summary (    Arthritis) ############################################# #ID treatment Sex age improved Min.   : 1.00 placebo:43 female:59 Min.                            : 23.00 none:42 1st qu.:21.75 treated:41 male:25 1st qu.:46.00 some:14 median:42.50                            median:57.00 marked:28 mean:42.50 mean:53.36 3rd qu.:63.25   3rd qu.:63.00 Max.   : 84.00 Max. : 74.00

As seen from the results, arthritis is the data of a data.frame structure. Where ID and age are data of type numeric, the other three are data of type factor.

Create a one-dimensional column table

> a<-table (arthritis$improved) #创建一维列联表 > class (a)                    #查看变量a的类型 [1] "table" > Anone   Some Marked     14     

As can be seen from the results, it is the following table

None Some Marked
42 14 28

Create a two-dimensional column table

> b<-table (arthritis$sex,arthritis$improved) > class (b) [1] "table" > B                 None Some Marked  Female  Male    2      6

The result is the following table

None Some Marked
Female 25 12 22
Male 17 2 6

We can also convert a one-dimensional list of table A and two-dimensional column table B into a percentage form

> prop.table (a)     none      Some    Marked 0.5000000 0.1666667 0.3333333 > prop.table (b)                       none       Some     Marked  Female 0.29761905 0.14285714 0.26190476  Male   0.20238095 0.02380952 0.07142857

add a marginal to table and

> Addmargins (a) ####################################  None   Some Marked    Sum     28     > Addmargins (b) ####################################                 None Some Marked sum  Female   -  Male    2      6  sum  84> addmargins (Prop.table (a)) #########################     None      Some    Marked       Sum 0.5000000 0.1666667 0.3333333 1.0000000 > Addmargins (prop.table (b)) #########################                       None       Some     Marked        Sum  Female 0.29761905 0.14285714 0.26190476 0.70238095  Male   0.20238095 0.02380952 0.07142857 0.29761905  Sum    0.50000000 0.16666667 0.33333333 1.00000000

add only a portion of the marginal and

> Addmargins (prop.table (b), 1)                       None       Some     Marked  Female 0.29761905 0.14285714 0.26190476  Male   0.20238095 0.02380952 0.07142857  Sum    0.50000000 0.16666667 0.33333333> addmargins (prop.table (b), 2)                       None       Some     Marked        Sum  Female 0.29761905 0.14285714 0.26190476 0.70238095  Male   0.20238095 0.02380952 0.07142857 0.29761905


Reference: R language Combat

Reprint Please specify source: http://blog.csdn.net/zhyoulun/article/details/46433163

[R language Statistics] frequency tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.