Basically, using R I would like to take a data set that looks like this:
And transform it to look like this:
Notice that the final table groups points by team and then sorts the summary data by the number of points scored. Learning how to do this in R would be a huge time saver.
The two functions you will need … once you have an example … are xtabs
and ftable
. First we convert an existing table to a dataframe and then note that the counts are now in a variable named “Freq”. Then we re-tabulate to a smaller number of dimensions:
> Tdf <- as.data.frame(Titanic, stringsAsFactors=FALSE)
> str(Tdf)
'data.frame': 32 obs. of 6 variables:
$ Class : chr "1st" "2nd" "3rd" "Crew" ...
$ Sex : chr "Male" "Male" "Male" "Male" ...
$ Age : chr "Child" "Child" "Child" "Child" ...
$ Survived: chr "No" "No" "No" "No" ...
$ Freq : num 0 0 35 0 0 0 17 0 118 154 ...
> ftable( xtabs(Freq ~ Class+Sex, Tdf) , row.vars=c("Class", "Sex") )
Class Sex
1st Female 145
Male 180
2nd Female 106
Male 179
3rd Female 196
Male 510
Crew Female 23
Male 862
I haven’t gotten very many points for it but I did at one time demonstrate on SO how to do sub-totals and grand totals:
R: calculating column sums & row sums as an aggregation from a dataframe