Search code examples
rprobability

Probability of an event in R


I've the following data frame:

enter image description here

and I need to evaluate the probability that a student is a "M", i. e. P("M") equal to the ratio between the number of the "M" and the number of studenti. I did it in this way:

nStudenti <- length(studenti$sesso)
tabellaSesso <- table(studenti$sesso)
nMaschi <- tabellaSesso[names(tabellaSesso) == "M"]
P = nMaschi / nStudenti

Is this the shortest way or there are commands which simply the things?


Solution

  • You can use table() to get the absolute frequencies and then use prop.table() to get the probabilities. If you are only interested in a specific value like "M", you can just index that value.

    # sample data
    studenti <- data.frame(sesso = sample(c("M", "F", NA), 100, replace = TRUE))
    
    # all probabilties
    prop.table(table(studenti$sesso))
    #> 
    #>        F        M 
    #> 0.530303 0.469697
    
    # specfic probability 
    prop.table(table(studenti$sesso))["M"]
    #>        M 
    #> 0.469697
    

    Created on 2021-10-06 by the reprex package (v2.0.1)