Hope you are doing well.

Thanks for raising a request, we are glad assisting you.

I hope the below scenario will help you in understanding


The tapply function:

Applies a function to each cell of a ragged array.

tapply(array, indicies, function, ..., simplify)

The function is applied to each of the cells which are defined by the categorical variables listed in argument indicies. If the results of applying function to each cell is a single number then the results are returned in a multi-way array which has as many dimensions as there are components in the argument indicies.

For example, if the argument indicies = c(gender, employed) then the result will be a 2 by 2 matrix with rows defined by male, female and columns defined by employed, unemployed. If the results are not a single value then the results are in a list with an dim attribute which means that it prints like a list but the user access the components by using subscripts like in an array.

===============================================================================
#creating the data set with two categorical variables
x1 <- runif(16)
x1
 [1] 0.83189832 0.93558412 0.59623797 0.71544196 0.79925238 0.44859140
 [7] 0.03347409 0.62955913 0.97507841 0.71243195 0.58639700 0.43562781
[13] 0.23623549 0.97273216 0.72284040 0.25412129
==============================================================================
cat1 <- rep(1:4, 4)
cat1
 [1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

cat2 <- c(rep(1, 8), rep(2, 8))
cat2
 [1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2
===============================================================================
mat2.df <- data.frame(x1)
names(mat2.df) <- c("x1")
mat2.df$cat1 <- cat1
mat2.df$cat2 <- cat2
mat2.df
          x1 cat1 cat2
 1 0.9574315    1    1
 2 0.1163076    2    1
 3 0.6661923    3    1
 4 0.8265729    4    1
 5 0.6701039    1    1
 6 0.1478860    2    1
 7 0.8537499    3    1
 8 0.9993158    4    1
 9 0.4189768    1    2
10 0.8830733    2    2
11 0.6114867    3    2
12 0.3111015    4    2
13 0.8834808    1    2
14 0.3606836    2    2
15 0.7056246    3    2
16 0.8052925    4    2
===============================================================================
tapply(mat2.df$x1, mat2.df$cat1, mean)
         1         2         3         4
 0.7324982 0.3769876 0.7092634 0.7355707

tapply(mat2.df$x1, list(mat2.df$cat1, mat2.df$cat2), mean)
          1         2
1 0.8137677 0.6512288
2 0.1320968 0.6218785
3 0.7599711 0.6585556


Let us know if you have any further concern over this.

We will be really glad assisting you.