This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.

# 2-Way Frequency Table
attach(meganslaw)
The following objects are masked from meganslaw (pos = 3):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 4):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 5):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 6):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 7):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 12):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 13):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 14):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 15):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 16):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 17):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 18):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 21):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year
megtable <- table(meganslaw$state,meganslaw$ucr.code) # A will be rows, B will be columns 
megtable # print table 
    
       Other Sex_Offense   Theft Violent
  CO   19007       10097  337233   79761
  IA   23928        8991  379848  119569
  ID   10902        6208  152867   55779
  MA   34014        5619  340288  130038
  MI  134395       38464 1017996  376108
  SC  115282       22319  365916  457504
  TX   32383        8791  269712  105562
  UT   13520        7883  169363   52890
  VA   31446       10859  328526  150450
  VT    1751        1134   36562    7260
margin.table(megtable, 1) # A frequencies (summed over B) 

     CO      IA      ID      MA      MI      SC      TX      UT      VA 
 446098  532336  225756  509959 1566963  961021  416448  243656  521281 
     VT 
  46707 
margin.table(megtable, 2) # B frequencies (summed over A)

      Other Sex_Offense       Theft     Violent 
     416628      120365     3398311     1534921 
prop.table(megtable) # cell percentages
    
            Other  Sex_Offense        Theft      Violent
  CO 0.0034746286 0.0018458107 0.0616488353 0.0145809359
  IA 0.0043742259 0.0016436253 0.0694391913 0.0218581503
  ID 0.0019929710 0.0011348711 0.0279452856 0.0101968383
  MA 0.0062180258 0.0010271972 0.0622073132 0.0237719655
  MI 0.0245684592 0.0070315206 0.1860976468 0.0687554899
  SC 0.0210744531 0.0040800881 0.0668923125 0.0836353166
  TX 0.0059198662 0.0016070637 0.0493054673 0.0192975609
  UT 0.0024715620 0.0014410742 0.0309608837 0.0096687065
  VA 0.0057485752 0.0019851103 0.0600571274 0.0275034391
  VT 0.0003200965 0.0002073041 0.0066838201 0.0013271849
prop.table(megtable, 1) # row percentages 
    
          Other Sex_Offense      Theft    Violent
  CO 0.04260723  0.02263404 0.75596169 0.17879704
  IA 0.04494905  0.01688971 0.71354934 0.22461190
  ID 0.04829108  0.02749872 0.67713372 0.24707649
  MA 0.06669948  0.01101853 0.66728502 0.25499697
  MI 0.08576782  0.02454685 0.64966180 0.24002354
  SC 0.11995784  0.02322426 0.38075755 0.47606036
  TX 0.07776001  0.02110948 0.64764869 0.25348183
  UT 0.05548807  0.03235299 0.69509062 0.21706833
  VA 0.06032447  0.02083138 0.63022823 0.28861593
  VT 0.03748903  0.02427902 0.78279487 0.15543709
prop.table(megtable, 2) # column percentages
    
           Other Sex_Offense       Theft     Violent
  CO 0.045621034 0.083886512 0.099235473 0.051964238
  IA 0.057432530 0.074697794 0.111775526 0.077899123
  ID 0.026167228 0.051576455 0.044983228 0.036339981
  MA 0.081641176 0.046683006 0.100134449 0.084719670
  MI 0.322577935 0.319561334 0.299559399 0.245034109
  SC 0.276702478 0.185427658 0.107675842 0.298063549
  TX 0.077726413 0.073036182 0.079366485 0.068773572
  UT 0.032451011 0.065492460 0.049837405 0.034457799
  VA 0.075477404 0.090217256 0.096673318 0.098018074
  VT 0.004202790 0.009421343 0.010758874 0.004729885
# 2-Way Frequency Table 
attach(meganslaw)
The following objects are masked from meganslaw (pos = 3):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 4):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 5):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 6):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 7):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 8):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 13):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 14):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 15):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 16):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 17):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 18):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 19):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year

The following objects are masked from meganslaw (pos = 22):

    age_of_victim, ages, crime, Date, Fed_Law, incident_date,
    incident_number, is.minor, is.svu, meglaw, month,
    month_number, originating_agency_identifier, race_of_victim,
    remove, sex_of_victim, state, timebin, type_of_victim,
    ucr.code, year
megtable <- table(meganslaw$year,meganslaw$ucr.code) # A will be rows, B will be columns 
megtable # print table 
      
        Other Sex_Offense  Theft Violent
  1992    990         682  15691    5561
  1993   2433        1618  32456   12458
  1994   2939        1783  43068   15140
  1995   5426        2903  78271   24947
  1996  20333        6526 223317   72238
  1997  56119       16527 507289  216442
  1998  77791       23840 654148  300140
  1999  87935       26063 684305  321164
  2000  86578       23965 669405  320221
  2001  57834       12971 374152  188538
  2002  18250        3487 116209   58072
margin.table(megtable, 1) # A frequencies (summed over B) 

   1992    1993    1994    1995    1996    1997    1998    1999    2000 
  22924   48965   62930  111547  322414  796377 1055919 1119467 1100169 
   2001    2002 
 633495  196018 
margin.table(megtable, 2) # B frequencies (summed over A)

      Other Sex_Offense       Theft     Violent 
     416628      120365     3398311     1534921 
prop.table(megtable) # cell percentages
      
              Other  Sex_Offense        Theft      Violent
  1992 0.0001809798 0.0001246749 0.0028684378 0.0010165944
  1993 0.0004447715 0.0002957831 0.0059332112 0.0022774200
  1994 0.0005372722 0.0003259464 0.0078731679 0.0027677107
  1995 0.0009919153 0.0005306912 0.0143085522 0.0045605071
  1996 0.0037170317 0.0011930039 0.0408240977 0.0132056725
  1997 0.0102589930 0.0030212651 0.0927364048 0.0395672938
  1998 0.0142208044 0.0043581388 0.1195833809 0.0548679442
  1999 0.0160752071 0.0047645207 0.1250963169 0.0587112962
  2000 0.0158271369 0.0043809898 0.1223724801 0.0585389084
  2001 0.0105725084 0.0023712005 0.0683979178 0.0344662240
  2002 0.0033362430 0.0006374509 0.0212439159 0.0106160167
prop.table(megtable, 1) # row percentages 
      
            Other Sex_Offense      Theft    Violent
  1992 0.04318618  0.02975048 0.68447915 0.24258419
  1993 0.04968855  0.03304401 0.66284080 0.25442663
  1994 0.04670269  0.02833307 0.68437947 0.24058478
  1995 0.04864317  0.02602490 0.70168628 0.22364564
  1996 0.06306488  0.02024106 0.69264052 0.22405355
  1997 0.07046788  0.02075273 0.63699605 0.27178334
  1998 0.07367137  0.02257749 0.61950585 0.28424529
  1999 0.07855077  0.02328162 0.61127751 0.28689010
  2000 0.07869518  0.02178302 0.60845652 0.29106528
  2001 0.09129354  0.02047530 0.59061555 0.29761561
  2002 0.09310369  0.01778918 0.59284862 0.29625851
prop.table(megtable, 2) # column percentages
      
             Other Sex_Offense       Theft     Violent
  1992 0.002376221 0.005666099 0.004617294 0.003622988
  1993 0.005839742 0.013442446 0.009550627 0.008116379
  1994 0.007054255 0.014813276 0.012673354 0.009863700
  1995 0.013023609 0.024118307 0.023032324 0.016252954
  1996 0.048803729 0.054218419 0.065714115 0.047063008
  1997 0.134698100 0.137307357 0.149276803 0.141011818
  1998 0.186715727 0.198064221 0.192492094 0.195541008
  1999 0.211063587 0.216533045 0.201366208 0.209238130
  2000 0.207806484 0.199102729 0.196981677 0.208623766
  2001 0.138814482 0.107763885 0.110099399 0.122832380
  2002 0.043804065 0.028970216 0.034196105 0.037833869
# 3-Way Frequency Table 
lawtable <- table(meganslaw$state,meganslaw$ucr.code, meganslaw$year) 
ftable(lawtable)
                  1992   1993   1994   1995   1996   1997   1998   1999   2000   2001   2002
                                                                                            
CO Other             0      0      0      0      0   3836   4242   3871   4479   2579      0
   Sex_Offense       0      0      0      0      0   2268   2470   2187   2144   1028      0
   Theft             0      0      0      0      0  79430  82590  68530  71917  34766      0
   Violent           0      0      0      0      0  17193  19687  17015  17468   8398      0
IA Other             0      0      0      0   2382   4758   4524   4695   4838   2731      0
   Sex_Offense       0      0      0      0    776   1605   1780   2027   1855    948      0
   Theft             0      0      0      0  39637  74991  76487  76961  76574  35198      0
   Violent           0      0      0      0  11165  23058  23421  24684  24942  12299      0
ID Other           990   2433   2809   3157   1513      0      0      0      0      0      0
   Sex_Offense     682   1618   1620   1507    781      0      0      0      0      0      0
   Theft         15691  32456  39614  44820  20286      0      0      0      0      0      0
   Violent        5561  12458  14494  15918   7348      0      0      0      0      0      0
MA Other             0      0      0      0      0   1405   4596   5377   6501   9296   6839
   Sex_Offense       0      0      0      0      0    212    798    945   1147   1442   1075
   Theft             0      0      0      0      0  18994  53166  60816  67298  80442  59572
   Violent           0      0      0      0      0   7114  20478  22900  26143  31950  21453
MI Other             0      0      0      0  12657  24422  27360  28440  32539   8977      0
   Sex_Offense       0      0      0      0   2979   5460   8387   8742   9991   2905      0
   Theft             0      0      0      0 112290 192668 215922 203841 241837  51438      0
   Violent           0      0      0      0  38202  65936  77514  77191  95012  22253      0
SC Other             0      0      0      0      0  13948  22389  24286  22772  23929   7958
   Sex_Offense       0      0      0      0      0   3391   4605   4691   3912   4120   1600
   Theft             0      0      0      0      0  43953  70315  66830  64487  90221  30110
   Violent           0      0      0      0      0  67643  97432  94481  89933  81939  26076
TX Other             0      0      0      0      0   1224   4980   6218   6186  10322   3453
   Sex_Offense       0      0      0      0      0    431   1562   1605   1853   2528    812
   Theft             0      0      0      0      0  11606  42175  52998  54319  82087  26527
   Violent           0      0      0      0      0   3985  16494  19994  22847  31699  10543
UT Other             0      0      0   1467   1791   2716   3254   3580    712      0      0
   Sex_Offense       0      0      0    958   1273   1494   1775   2002    381      0      0
   Theft             0      0      0  22239  27255  35336  37069  39883   7581      0      0
   Violent           0      0      0   5987   7690  10856  12405  13229   2723      0      0
VA Other             0      0      0    490   1817   3611   5823  11154   8551      0      0
   Sex_Offense       0      0      0    190    564   1544   2184   3695   2682      0      0
   Theft             0      0      0   5152  19314  45750  64902 108016  85392      0      0
   Violent           0      0      0   1658   6875  19803  30615  50346  41153      0      0
VT Other             0      0    130    312    173    199    623    314      0      0      0
   Sex_Offense       0      0    163    248    153    122    279    169      0      0      0
   Theft             0      0   3454   6060   4535   4561  11522   6430      0      0      0
   Violent           0      0    646   1384    958    854   2094   1324      0      0      0
lawtable <- table(meganslaw$is.minor,meganslaw$ucr.code, meganslaw$year) 
ftable(lawtable)
                 1992   1993   1994   1995   1996   1997   1998   1999   2000   2001   2002
                                                                                           
0 Other           867   2130   2590   4904  18444  51326  71109  80050  79069  52643  16598
  Sex_Offense     149    269    281    598   1631   4731   6353   6930   6720   3552   1017
  Theft         14365  29772  39691  71997 207677 476801 617603 646839 634170 356861 111128
  Violent        4169   9104  11076  18814  55486 171691 239871 256954 256733 152332  46609
1 Other           123    303    349    522   1889   4793   6682   7885   7509   5191   1652
  Sex_Offense     533   1349   1502   2305   4895  11796  17487  19133  17245   9419   2470
  Theft          1326   2684   3377   6274  15640  30488  36545  37466  35235  17291   5081
  Violent        1392   3354   4064   6133  16752  44751  60269  64210  63488  36206  11463
lawtable <- table(meganslaw$is.minor,meganslaw$ucr.code, meganslaw$meglaw) 
ftable(lawtable) 
                     0       1
                              
0 Other         121964  257766
  Sex_Offense    10472   21759
  Theft        1033527 2173377
  Violent       408324  814515
1 Other          11670   25228
  Sex_Offense    26930   61204
  Theft          64771  126636
  Violent       104778  207304
#Table ignores missing values. To include NA as a category in counts, include the table option exclude=NULL if the variable is a vector. If the variable is a factor you have to create a new factor using newfactor <- factor(oldfactor, exclude=NULL).
xtabs
function (formula = ~., data = parent.frame(), subset, sparse = FALSE, 
    na.action, exclude = c(NA, NaN), drop.unused.levels = FALSE) 
{
    if (missing(formula) && missing(data)) 
        stop("must supply either 'formula' or 'data'")
    if (!missing(formula)) {
        formula <- as.formula(formula)
        if (!inherits(formula, "formula")) 
            stop("'formula' missing or incorrect")
    }
    if (any(attr(terms(formula, data = data), "order") > 1)) 
        stop("interactions are not allowed")
    m <- match.call(expand.dots = FALSE)
    if (is.matrix(eval(m$data, parent.frame()))) 
        m$data <- as.data.frame(data)
    m$... <- m$exclude <- m$drop.unused.levels <- m$sparse <- NULL
    m[[1L]] <- quote(stats::model.frame)
    mf <- eval(m, parent.frame())
    if (length(formula) == 2L) {
        by <- mf
        y <- NULL
    }
    else {
        i <- attr(attr(mf, "terms"), "response")
        by <- mf[-i]
        y <- mf[[i]]
    }
    has.exclude <- !missing(exclude)
    by <- lapply(by, function(u) {
        if (!is.factor(u)) 
            u <- factor(u, exclude = exclude)
        else if (has.exclude) 
            u <- factor(as.character(u), levels = setdiff(levels(u), 
                exclude), exclude = NULL)
        u[, drop = drop.unused.levels]
    })
    if (!sparse) {
        x <- if (is.null(y)) 
            do.call("table", by)
        else if (NCOL(y) == 1L) 
            tapply(y, by, sum)
        else {
            z <- lapply(as.data.frame(y), tapply, by, sum)
            array(unlist(z), dim = c(dim(z[[1L]]), length(z)), 
                dimnames = c(dimnames(z[[1L]]), list(names(z))))
        }
        x[is.na(x)] <- 0L
        class(x) <- c("xtabs", "table")
        attr(x, "call") <- match.call()
        x
    }
    else {
        if (length(by) != 2L) 
            stop(gettextf("%s applies only to two-way tables", 
                "xtabs(*, sparse=TRUE)"), domain = NA)
        if (is.null(tryCatch(loadNamespace("Matrix"), error = function(e) NULL))) 
            stop(gettextf("%s needs package 'Matrix' correctly installed", 
                "xtabs(*, sparse=TRUE)"), domain = NA)
        if (length(i.ex <- unique(unlist(lapply(by, function(f) which(is.na(f))))))) 
            by <- lapply(by, `[`, -i.ex)
        rows <- by[[1L]]
        cols <- by[[2L]]
        rl <- levels(rows)
        cl <- levels(cols)
        if (is.null(y)) 
            y <- rep.int(1, length(rows))
        as(new("dgTMatrix", i = as.integer(rows) - 1L, j = as.integer(cols) - 
            1L, x = as.double(y), Dim = c(length(rl), length(cl)), 
            Dimnames = list(rl, cl)), "CsparseMatrix")
    }
}
<bytecode: 0x1a9d1eb20>
<environment: namespace:stats>
#The xtabs( ) function allows you to create crosstabulations using formula style input.
# 3-Way Frequency Table
#mytable <- xtabs(~A+B+c, data=mydata)
#ftable(mytable) # print table 
#summary(mytable) # chi-square test of indepedence
install.packages("gmodels")
Error in install.packages : Updating loaded packages
library(gmodels)
CrossTable(meganslaw$meglaw, meganslaw$ucr.code)

 
   Cell Contents
|-------------------------|
|                       N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|

 
Total Observations in Table:  5470225 

 
                 | meganslaw$ucr.code 
meganslaw$meglaw |       Other | Sex_Offense |       Theft |     Violent |   Row Total | 
-----------------|-------------|-------------|-------------|-------------|-------------|
               0 |      133634 |       37402 |     1098298 |      513102 |     1782436 | 
                 |      33.151 |      84.283 |      73.455 |     335.740 |             | 
                 |       0.075 |       0.021 |       0.616 |       0.288 |       0.326 | 
                 |       0.321 |       0.311 |       0.323 |       0.334 |             | 
                 |       0.024 |       0.007 |       0.201 |       0.094 |             | 
-----------------|-------------|-------------|-------------|-------------|-------------|
               1 |      282994 |       82963 |     2300013 |     1021819 |     3687789 | 
                 |      16.023 |      40.737 |      35.503 |     162.275 |             | 
                 |       0.077 |       0.022 |       0.624 |       0.277 |       0.674 | 
                 |       0.679 |       0.689 |       0.677 |       0.666 |             | 
                 |       0.052 |       0.015 |       0.420 |       0.187 |             | 
-----------------|-------------|-------------|-------------|-------------|-------------|
    Column Total |      416628 |      120365 |     3398311 |     1534921 |     5470225 | 
                 |       0.076 |       0.022 |       0.621 |       0.281 |             | 
-----------------|-------------|-------------|-------------|-------------|-------------|

 
install.packages("gmodels")
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 71885  100 71885    0     0  64311      0  0:00:01  0:00:01 --:--:-- 64355

The downloaded binary packages are in
    /var/folders/qx/twjwptxx76d7vl1m4l50_1z00000gn/T//RtmpJEwnbr/downloaded_packages

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).

LS0tCnRpdGxlOiAiRnJlcXVlbmN5IFRhYmxlcyIKb3V0cHV0OgogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQKICBodG1sX2RvY3VtZW50OiBkZWZhdWx0CiAgd29yZF9kb2N1bWVudDogZGVmYXVsdAotLS0KClRoaXMgaXMgYW4gW1IgTWFya2Rvd25dKGh0dHA6Ly9ybWFya2Rvd24ucnN0dWR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