![](/img/trans.png)
[英]Calculate proportions for multiple variables according to factor level across multiple columns
[英]Proportions for factor columns in matrix in R
我想計算 R 中矩陣中因子水平的比例。
樣本數據:
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"
如果我將mtx
轉換為數據幀,我可以使用sapply
來獲取比例:
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
但是有沒有辦法通過數據幀轉換來獲得比例而不走彎路?
您可以使用apply
對列使用MARGIN = 2
矩陣效果更好。
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
如果我們在數據集的col
上做table
,我們可以以矢量化的方式做到這一點
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.