Description Usage Arguments Details Value Author(s) See Also Examples
Crosstabulates variables with small numbers of unique values.
1 2 |
formula |
a formula containing the variables to be crosstabulated |
data |
the data frame from which to select the variables |
varnames |
optional labels for the variables (defaults to names(data)) |
or |
whether to calculate the odds ratio (only for 2x2 tables). |
chisq |
logical - whether to display chi squared test(s) of the table(s) |
phi |
whether to calculate and display the phi coefficient of association - only for 2x2 tables |
html |
whether to format the resulting table with HTML tags. |
bgcol |
background color for the table if html=TRUE. |
lastone |
A flag that controls the names of the returned list. |
xtab will accept a formula referring to columns in a data frame or two explicit variable names. It calls calculate.xtab for the calculations and displays one or more tables of results by calling print.xtab. If html is TRUE, the resulting table will be formatted with HTML tags. The default value of lastone should not be changed.
The result of calculate.xtab if there is only one table to display, otherwise a nested list of tables.
Jim Lemon
table, calculate.xtab, print.xtab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | test.df<-data.frame(sex=sample(c("MALE","FEMALE"),1000,TRUE),
suburb=sample(1:4,1000,TRUE),social.type=sample(LETTERS[1:4],1000,TRUE))
xtab(sex~suburb+social.type,test.df,chisq=TRUE)
# now add some value labels
attr(test.df$suburb,"value.labels")<-1:4
names(attr(test.df$suburb,"value.labels"))<-
c("Upper","Middle","Working","Slum")
attr(test.df$social.type,"value.labels")<-LETTERS[1:4]
names(attr(test.df$social.type,"value.labels"))<-
c("Gregarious","Mixer","Aloof","Hermit")
xtab(sex~suburb+social.type,test.df)
# now have some fun with ridiculously long factor labels
testxtab<-data.frame(firstbit=sample(c("Ecomaniacs","Redneck rogues"),50,TRUE),
secondbit=sample(c("Macho bungy jumpers","Wimpy quiche munchers"),50,TRUE))
# and format the table in HTML and add some tests
xtab(secondbit~firstbit,testxtab,html=TRUE,chisq=TRUE,phi=TRUE)
|
Count for social.type = A is 257 ( 25.7 %)
Crosstabulation of sex by suburb
suburb
sex 1 2 3 4
FEMALE 31 34 28 39 132
23.48 25.76 21.21 29.55 -
56.36 59.65 44.44 47.56 51.36
MALE 24 23 35 43 125
19.2 18.4 28 34.4 -
43.64 40.35 55.56 52.44 48.64
55 57 63 82 257
21.40 22.18 24.51 31.91 100.00
X2[3]=3.799, p=0.2840289
Count for social.type = B is 221 ( 22.1 %)
Crosstabulation of sex by suburb
suburb
sex 1 2 3 4
FEMALE 37 28 31 22 118
31.36 23.73 26.27 18.64 -
66.07 50.91 54.39 41.51 53.39
MALE 19 27 26 31 103
18.45 26.21 25.24 30.1 -
33.93 49.09 45.61 58.49 46.61
56 55 57 53 221
25.34 24.89 25.79 23.98 100.00
X2[3]=6.784, p=0.07911241
Count for social.type = C is 268 ( 26.8 %)
Crosstabulation of sex by suburb
suburb
sex 1 2 3 4
FEMALE 31 27 29 31 118
26.27 22.88 24.58 26.27 -
44.29 39.13 40.85 53.45 44.03
MALE 39 42 42 27 150
26 28 28 18 -
55.71 60.87 59.15 46.55 55.97
70 69 71 58 268
26.12 25.75 26.49 21.64 100.00
X2[3]=3.054, p=0.3833826
Count for social.type = D is 254 ( 25.4 %)
Crosstabulation of sex by suburb
suburb
sex 1 2 3 4
FEMALE 33 39 29 31 132
25 29.55 21.97 23.48 -
55.93 49.37 54.72 49.21 51.97
MALE 26 40 24 32 122
21.31 32.79 19.67 26.23 -
44.07 50.63 45.28 50.79 48.03
59 79 53 63 254
23.23 31.10 20.87 24.80 100.00
X2[3]=0.938, p=0.8161303
Count for social.type = Gregarious is 257 ( 25.7 %)
Crosstabulation of sex by suburb
suburb
sex Upper MiddleWorking Slum
FEMALE 31 34 28 39 132
23.48 25.76 21.21 29.55 -
56.36 59.65 44.44 47.56 51.36
MALE 24 23 35 43 125
19.2 18.4 28 34.4 -
43.64 40.35 55.56 52.44 48.64
55 57 63 82 257
21.40 22.18 24.51 31.91 100.00
Count for social.type = Mixer is 221 ( 22.1 %)
Crosstabulation of sex by suburb
suburb
sex Upper MiddleWorking Slum
FEMALE 37 28 31 22 118
31.36 23.73 26.27 18.64 -
66.07 50.91 54.39 41.51 53.39
MALE 19 27 26 31 103
18.45 26.21 25.24 30.1 -
33.93 49.09 45.61 58.49 46.61
56 55 57 53 221
25.34 24.89 25.79 23.98 100.00
Count for social.type = Aloof is 268 ( 26.8 %)
Crosstabulation of sex by suburb
suburb
sex Upper MiddleWorking Slum
FEMALE 31 27 29 31 118
26.27 22.88 24.58 26.27 -
44.29 39.13 40.85 53.45 44.03
MALE 39 42 42 27 150
26 28 28 18 -
55.71 60.87 59.15 46.55 55.97
70 69 71 58 268
26.12 25.75 26.49 21.64 100.00
Count for social.type = Hermit is 254 ( 25.4 %)
Crosstabulation of sex by suburb
suburb
sex Upper MiddleWorking Slum
FEMALE 33 39 29 31 132
25 29.55 21.97 23.48 -
55.93 49.37 54.72 49.21 51.97
MALE 26 40 24 32 122
21.31 32.79 19.67 26.23 -
44.07 50.63 45.28 50.79 48.03
59 79 53 63 254
23.23 31.10 20.87 24.80 100.00
<table border=0 relief="flat" style="background-color:lightgray">
<tr>
<td colspan=4>
Crosstabulation of secondbit by firstbit
<tr><td><td colspan=2 align=center>firstbit<tr><td>secondbit<td>Ecomaniacs<td>Redneck rogues
<tr><td>Macho bungy jumpers <td>12<br>52.17<br>52.17<td>11<br>47.83<br>40.74<td>23<br>-<br>46
<tr><td>Wimpy quiche munchers<td>11<br>40.74<br>47.83<td>16<br>59.26<br>59.26<td>27<br>-<br>54
<tr><td>
<td>23<br>46<td>27<br>54<td> 50 <br>100
<tr><td colspan=4>X2[1]=0.274, p=0.6004296
<tr><td colspan=4>odds ratio = 1.59
<tr><td colspan=4>phi = 0.114
</table>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.