简体   繁体   中英

Proportions for factor columns in matrix in R

I'd like to calculate proportions of factor levels in a matrix in R.

Sample data:

mtx <- matrix(NA, nrow=8, ncol=4)
set.seed(12)
wordclass <- c("Function", "Content", "Insert")
for(i in 1:nrow(mtx)){
  mtx[i,] <- sample(wordclass, 4, replace = T)
}
mtx
     [,1]       [,2]       [,3]       [,4]      
[1,] "Content"  "Content"  "Insert"   "Insert"  
[2,] "Content"  "Function" "Function" "Content" 
[3,] "Insert"   "Content"  "Function" "Content" 
[4,] "Function" "Content"  "Content"  "Content" 
[5,] "Insert"   "Function" "Function" "Insert"  
[6,] "Content"  "Insert"   "Content"  "Function"
[7,] "Insert"   "Content"  "Function" "Function"
[8,] "Function" "Content"  "Insert"   "Content"

If I convert mtx to a dataframe, I could use sapply to get the proportions:

mtx_df <- as.data.frame(mtx)
props <- as.data.frame(sapply(mtx_df, function(x) prop.table(table(x))))
props
            V1    V2   V3   V4
Content  0.375 0.625 0.25 0.50
Function 0.250 0.250 0.50 0.25
Insert   0.375 0.125 0.25 0.25

But is there a way to obtain the proportions without the detour via the dataframe conversion?

You can use apply which works better with matrices using MARGIN = 2 for columns.

apply(mtx, 2, function(x) prop.table(table(factor(x, levels = wordclass))))

#          [,1]  [,2] [,3] [,4]
#Content  0.375 0.625 0.25 0.50
#Function 0.250 0.250 0.50 0.25
#Insert   0.375 0.125 0.25 0.25

We could do this in a vectorized way if we do the table on the col of the dataset

prop.table(table(c(mtx), c(col(mtx))), 2)
#           1     2     3     4
#  Content  0.375 0.625 0.250 0.500
#  Function 0.250 0.250 0.500 0.250
#  Insert   0.375 0.125 0.250 0.250

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM