我有一個包含 6 行和 1 列的資料集。該列包含數字 2 或 3。我想做的是計算 2 出現的次數和 3 出現的次數,然后除以行數。
例如,列中有兩個 2,因此 2/6 = 33%。
我曾經table(df['over1.5'])計算出現次數,但不確定如何將計數除以總行數。
uj5u.com熱心網友回復:
我們可以proportions在table輸出上使用
round(100 *proportions(table(df$over1.5)))
2 3
33 67
或count除以sum頻率
library(dplyr)
df %>%
count(over1.5) %>%
mutate(percentile = round(100*n/sum(n)))
over1.5 n percentile
1 2 2 33
2 3 4 67
資料
df <- data.frame(over1.5 = c(2, 2, 3, 3, 3, 3))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/530789.html
標籤:r数数
