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.
This should work:
Created on 2023-11-09 with reprex v2.0.2