Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading