xtabs()
is the numerical version of barchartGC()
. You use it when you want to study
The function xtabs()
comes with the stats
package, which is always loaded in R. However, some of the data and other functions that we will use come from the tigerstats
package, so make sure that it is loaded:
To see a table of the tallies for the factor variable seat (from the mat111survey
data frame in the tigerstats
package):
xtabs(~seat,data=m111survey)
## seat
## 1_front 2_middle 3_back
## 27 32 12
In order to get the actual distribution of seat, you want percents rather than counts, so apply the function rowPerc()
from tigerstats
:
rowPerc(xtabs(~seat,data=m111survey))
##
## seat 1_front 2_middle 3_back Total
## 38.03 45.07 16.9 100
If you have a table of the counts for a variable, then you can enter it directly. For example, suppose you have already made:
Seat <- xtabs(~seat,data=m111survey)
Seat
## seat
## 1_front 2_middle 3_back
## 27 32 12
Then you can just enter the table into rowPerc()
:
rowPerc(Seat)
##
## seat 1_front 2_middle 3_back Total
## 38.03 45.07 16.9 100
You can also use xtabs()
to study the relationship between two factor variables. For example, if you want to see whether males and females differ in their seating preferences, then you might try formula-data input as follows:
xtabs(~sex+seat,data=m111survey)
## seat
## sex 1_front 2_middle 3_back
## female 19 16 5
## male 8 16 7
Of course, row percents are the way to actually study the relationship:
rowPerc(xtabs(~sex+seat,data=m111survey))
## seat
## sex 1_front 2_middle 3_back Total
## female 47.50 40.00 12.50 100.00
## male 25.81 51.61 22.58 100.00
Note the type of formula used to study the relationship between two factor variables:
\[\sim ExplanatoryFactor + ResponseFactor\]
Note also that people usually want to do several things with their two-way table, so often they will make it and store it in a well-named object, and then print out the object:
SexSeat <- xtabs(~sex+seat,data=m111survey)
SexSeat
## seat
## sex 1_front 2_middle 3_back
## female 19 16 5
## male 8 16 7
Then to get row percents they just put in the named object:
rowPerc(SexSeat)
## seat
## sex 1_front 2_middle 3_back Total
## female 47.50 40.00 12.50 100.00
## male 25.81 51.61 22.58 100.00
We don’t use column percents very often, but you can get them:
colPerc(SexSeat)
## seat
## sex 1_front 2_middle 3_back
## female 70.37 50 41.67
## male 29.63 50 58.33
## Total 100.00 100 100.00