I'm not sure how to best explain what I am after but here goes. I have a data frame with 2 geographical factors. One is the major region the other is the component regions.
I am trying to process all the regions at the same time without using "for". So I need (think, I do) a list of matrices each structured according to the number of subregions within each region.
So is there a way of using lapply with xtabs or is there a better way to achieve my desired output?
Using the Titanic data as an example
t1 <- as.data.frame(Titanic)
t2 <- split(t1,t1$Class)
# I would then drop any unused levels in the factors for the geography creating distinctly different data.frames (see end of message)
xtabs(Freq ~ Age + Sex + Class,t1)
, , Class = 1st
Sex
Age Male Female
Child 5 1
Adult 175 144
, , Class = 2nd
Sex
Age Male Female
Child 11 13
Adult 168 93
, , Class = 3rd
Sex
Age Male Female
Child 48 31
Adult 462 165
, , Class = Crew
Sex
Age Male Female
Child 0 0
Adult 862 23
Can I do something with t2 to produce a list which is in effect an Age by Sex crosstab with one item for each value of Class. I would be wanting to drop.unused.levels, so that the last part of the table is just
Sex
Age Male Female
Adult 862 23
or in my case each item in the list has the same number of rows as there are subregions for that region.
List of 9
$ 1:`data.frame': 4009 obs. of 7 variables:
..$ sex : Factor w/ 2 levels "Females","Males": 2 2 2 2 2 2 2 2 2 2 ...
..$ age : Factor w/ 18 levels "0-4","5-9","10-14",..: 1 1 1 1 1 1 1 1 1 1 ...
..$ lga : Factor w/ 23 levels "Carnamah (S)",..: 1 2 3 4 5 6 7 8 9 10 ... # 23 subregions
..$ psn : num [1:4009] 71 336 26 84 30 133 904 385 99 110 ...
..$ year : num [1:4009] 1991 1991 1991 1991 1991 ...
..$ agecomp : Factor w/ 14 levels "0-4","5-9","10-14",..: 1 1 1 1 1 1 1 1 1 1 ...
..$ RegionNum: num [1:4009] 1 1 1 1 1 1 1 1 1 1 ...
$ 2:`data.frame': 720 obs. of 7 variables:
..$ sex : Factor w/ 2 levels "Females","Males": 2 2 2 2 2 2 2 2 2 2 ...
..$ age : Factor w/ 18 levels "0-4","5-9","10-14",..: 1 1 1 1 2 2 2 2 3 3 ...
..$ lga : Factor w/ 4 levels "Broome (S)","De..",..: 1 2 3 4 1 2 3 4 1 2 ... # 4 subregions etc
..$ psn : num [1:720] 495 445 189 377 415 374 189 330 324 319 ...
..$ year : num [1:720] 1991 1991 1991 1991 1991 ...
..$ agecomp : Factor w/ 14 levels "0-4","5-9","10-14",..: 1 1 1 1 2 2 2 2 3 3 ...
..$ RegionNum: num [1:720] 2 2 2 2 2 2 2 2 2 2 ...
So these two items would produce