Search code examples
rlistdataframefrequencypercentage

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.


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