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

How to add a percentage column to each dataset of a list?

I have this dataset (let’s imagine it with 900 variables ) and the list l2 as follows :

df = data.frame(x = c(1,0,0,0,1,1,1), y = c(2,2,2,2,3,3,2) )

l1 = lapply(df,table)

l2 = lapply(l1,as.data.frame)

I wish to add a percentage column to each of these dataframes based on the Freq column of each dataframe. Appreciate the help.

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 :

You can use proportions:

lapply(df, \(x) transform(as.data.frame(table(x)), prop = proportions(Freq)))
$x
  x Freq      prop
1 0    3 0.4285714
2 1    4 0.5714286

$y
  x Freq      prop
1 2    5 0.7142857
2 3    2 0.2857143
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