Assignment On Mtcars: A. Crosstable (Mtcars$Cyl, Mtcars$Gear)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Assignment on

Mtcars
mtcars

nrow(mtcars)

table(mtcars$cyl)

str(mtcars)

help("mtcars")

table(mtcars$cyl)

library(dplyr)

x=mtcars%>%count(cyl)

names(x)=c("cyl","Freq")

x$RF=(x$Freq)/32

x$PerRF=((x$Freq)/32)*100

x$CumF=cumsum(x$Freq)

y=mtcars%>%count(cyl,vs)

#cross tabulation

library(gmodels)

CrossTable(mtcars$cyl,mtcars$vs)

Crosstable function using different variables

a. CrossTable(mtcars$cyl,mtcars$gear)

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

Total Observations in Table: 32

| mtcars$gear
mtcars$cyl | 3 | 4 | 5 | Row Total |
-------------|-----------|-----------|-----------|-----------|
4 | 1 | 8 | 2 | 11 |
| 3.350 | 3.640 | 0.046 | |
| 0.091 | 0.727 | 0.182 | 0.344 |
| 0.067 | 0.667 | 0.400 | |
| 0.031 | 0.250 | 0.062 | |
-------------|-----------|-----------|-----------|-----------|
6 | 2 | 4 | 1 | 7 |
| 0.500 | 0.720 | 0.008 | |
| 0.286 | 0.571 | 0.143 | 0.219 |
| 0.133 | 0.333 | 0.200 | |
| 0.062 | 0.125 | 0.031 | |
-------------|-----------|-----------|-----------|-----------|
8 | 12 | 0 | 2 | 14 |
| 4.505 | 5.250 | 0.016 | |
| 0.857 | 0.000 | 0.143 | 0.438 |
| 0.800 | 0.000 | 0.400 | |
| 0.375 | 0.000 | 0.062 | |
-------------|-----------|-----------|-----------|-----------|
Column Total | 15 | 12 | 5 | 32 |
| 0.469 | 0.375 | 0.156 | |
-------------|-----------|-----------|-----------|-----------|

b. CrossTable(mtcars$cyl,mtcars$vs)

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

Total Observations in Table: 32

| mtcars$vs
mtcars$cyl | 0 | 1 | Row Total |
-------------|-----------|-----------|-----------|
4 | 1 | 10 | 11 |
| 4.349 | 5.592 | |
| 0.091 | 0.909 | 0.344 |
| 0.056 | 0.714 | |
| 0.031 | 0.312 | |
-------------|-----------|-----------|-----------|
6 | 3 | 4 | 7 |
| 0.223 | 0.287 | |
| 0.429 | 0.571 | 0.219 |
| 0.167 | 0.286 | |
| 0.094 | 0.125 | |
-------------|-----------|-----------|-----------|
8 | 14 | 0 | 14 |
| 4.764 | 6.125 | |
| 1.000 | 0.000 | 0.438 |
| 0.778 | 0.000 | |

| 0.438 | 0.000 | |
-------------|-----------|-----------|-----------|
Column Total | 18 | 14 | 32 |
| 0.562 | 0.438 | |
-------------|-----------|-----------|-----------|

c. CrossTable(mtcars$cyl,mtcars$am)

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

Total Observations in Table: 32

| mtcars$am
mtcars$cyl | 0 | 1 | Row Total |
-------------|-----------|-----------|-----------|
4 | 3 | 8 | 11 |
| 1.909 | 2.790 | |
| 0.273 | 0.727 | 0.344 |
| 0.158 | 0.615 | |
| 0.094 | 0.250 | |
-------------|-----------|-----------|-----------|
6 | 4 | 3 | 7 |
| 0.006 | 0.009 | |
| 0.571 | 0.429 | 0.219 |
| 0.211 | 0.231 | |
| 0.125 | 0.094 | |
-------------|-----------|-----------|-----------|
8 | 12 | 2 | 14 |
| 1.636 | 2.391 | |
| 0.857 | 0.143 | 0.438 |
| 0.632 | 0.154 | |
| 0.375 | 0.062 | |
-------------|-----------|-----------|-----------|
Column Total | 19 | 13 | 32 |
| 0.594 | 0.406 | |
-------------|-----------|-----------|-----------|

d. CrossTable(mtcars$cyl,mtcars$carb)

Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 32

a. nrow(mtcars)
[1] 32

b. table(mtcars$cyl)

4 6 8
11 7 14

c. str(mtcars)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...

d. table(mtcars$cyl)

4 6 8
11 7 14

You might also like