Search code examples
rdataframefrequency-analysis

Adding a column with relative frequencys to a r-dataframe


I have a dataframe in R and want to add a column with relative freuqencys to each variable. Here is an example dataframe I am working with:

df <- data.frame(
  Ausprägung = c("Obs1", "Obs2", "Obs3", "Obs4", "Total"),
  Anzahl = c(100, 100, 100, 100, 400)
)

I want to add a column with the relative frequencys and have an output dataframe like the one shown down below

Ausprägung Anzahl rel. frequency
Obs1 100 0.25
Obs2 100 0.25
Obs3 100 0.25
Obs4 100 0.25
Total 400 -

I tried working with the prop.table() function but got stuck because I want to work with a dataframe and the prop.table() functions only supports table-objects it seems. I found a few other questions about the calculation of relative frequencys but non of them seemed to fit my case.


Solution

  • This should work:

    df <- data.frame(
      Ausprägung = c("Obs1", "Obs2", "Obs3", "Obs4", "Total"),
      Anzahl = c(100, 100, 100, 100, 400)
    )
    
    transform(df, rel.frequency = ifelse(Ausprägung == "Total", NA, Anzahl / sum(Anzahl[Ausprägung != "Total"])))
    #>   Ausprägung Anzahl rel.frequency
    #> 1       Obs1    100          0.25
    #> 2       Obs2    100          0.25
    #> 3       Obs3    100          0.25
    #> 4       Obs4    100          0.25
    #> 5      Total    400            NA
    

    Created on 2023-11-09 with reprex v2.0.2