如何根据频率计算概率

时间:2015-07-27 07:31:47

标签: r

我将月份存储为:

count.data <- count.month

x    freq
Jan   7
Feb   23
Mar   86
Apr   281

现在我想找到X的概率。

示例:

Jan属性的概率:7 /(7 + 23 + 86 + 281)#total freq

4 个答案:

答案 0 :(得分:4)

你可以试试这个。假设您的dat.frame名为dat

dat$prob <- dat$freq/sum(dat$freq)
dat
#x freq       prob
#1 Jan    7 0.01763224
#2 Feb   23 0.05793451
#3 Mar   86 0.21662469
#4 Apr  281 0.70780856

答案 1 :(得分:3)

dplyr

 library(dplyr)
 df %>% mutate(prob = freq / sum(freq))
    x freq       prob
1 Jan    7 0.01763224
2 Feb   23 0.05793451
3 Mar   86 0.21662469
4 Apr  281 0.70780856

答案 2 :(得分:2)

df$prob <- prop.table(df$freq)
df

输出:

    x freq       prob
1 Jan    7 0.01763224
2 Feb   23 0.05793451
3 Mar   86 0.21662469
4 Apr  281 0.70780856

如果您想访问特定月份:

 prop.table(df$freq)[df$x == "Feb"] 
[1] 0.05793451

sqldf包裹:

library(sqldf)
sqldf('SELECT x, freq, (freq*1.0)/(SELECT SUM(freq) FROM df) AS prob
      FROM df')

答案 3 :(得分:2)

或使用data.table

library(data.table)
setDT(df)[, prob:= freq/sum(freq)]