How to create a prop.table () table for a table of three dimensions?

Could you tell me how I can get the percentages in the three dimensional table. I know how to create percentages in a two dimensional table by doing the following

 p <-with(mtcars,tapply(carb,list(cyl,vs),length)) prop.table(p,2) # by column 

However, if I can try to add another variable, I do not?

 p <- with(mtcars,tapply(carb,list(cyl,vs,gear),length)) 
+6
source share
2 answers

You can specify several input levels of the prop.table function, where 1 = row, 2 = column, 3 = strata, etc. etc.

A simple example:

 test <- 1:8 dim(test) <- c(2,2,2) test , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 

Then you can do things like:

 # % of all values in each stratum/sub-table prop.table(test,3) # row % within each stratum/sub-table prop.table(test,c(3,1)) # column % within each stratum/sub-table prop.table(test,c(3,2)) 

There may be an easy way to deal with NA s, but the circular version should be to set them to 0 , and then reset as NA :

 # set one of the values to NA as an example test[7] <- NA # do the procedure nas <- is.na(test) test[nas] <- 0 result <- prop.table(test,c(3,2)) result[nas] <- NA result , , 1 [,1] [,2] [1,] 0.3333333 0.4285714 [2,] 0.6666667 0.5714286 , , 2 [,1] [,2] [1,] 0.4545455 NA [2,] 0.5454545 1 
+13
source

Try using reshape2 and acast to create a three-dimensional table. This value will be 0 instead of NA for these values ​​without data.

 library(reshape2) tables <- acast(mtcars, cyl~vs~gear,value.var = 'carb', fun.aggregate = 'length') tables , , 3 0 1 4 0 1 6 0 2 8 12 0 , , 4 0 1 4 0 8 6 2 2 8 0 0 , , 5 0 1 4 1 1 6 1 0 8 2 0 prop.table(tables, 2:3) , , 3 0 1 4 0 0.3333333 6 0 0.6666667 8 1 0.0000000 , , 4 0 1 4 0 0.8 6 1 0.2 8 0 0.0 , , 5 0 1 4 0.25 1 6 0.25 0 8 0.50 0 

You can also use table instead of your tapply call

 with(mtcars,table(cyl, vs ,gear)) , , gear = 3 vs cyl 0 1 4 0 1 6 0 2 8 12 0 , , gear = 4 vs cyl 0 1 4 0 8 6 2 2 8 0 0 , , gear = 5 vs cyl 0 1 4 1 1 6 1 0 8 2 0 

and then use prop.table for the appropriate sizes

+1
source

All Articles