Home > OS >  Is there an R function that turns a frequency table into a prop table?
Is there an R function that turns a frequency table into a prop table?

Time:07-14

What is the simplest way of turning a frequency data table into a prop table in R?

This is the data:

         Time Total Blog News Social.Network Microblog Other Forums Pictures Video
1  15.KW 2022  1816   23  326             39       678    99     27      523     0
2  16.KW 2022  2535   32  690             42       815   135     26      644     1
3  17.KW 2022  2181   20  362             79       805   110     14      634     1
4  18.KW 2022  2583   19  895             25       692   127      6      658     0
5  19.KW 2022  2337   21  555             22       908   148      8      599     0
6  20.KW 2022  2091   23  392             18       851   119      5      554     0
7  21.KW 2022  1658   17  344             16       650   129      1      417     0
8  22.KW 2022  2476   24  798             24       937   150      7      443     0
9  23.KW 2022  1687   14  341             17       691   102      9      400     0
10 24.KW 2022  2476   21  521             29       984   110     19      509     0
11 25.KW 2022  2412   22  696             31       845   115     29      561     0
12 26.KW 2022  2197   22  715             13       709   128     59      445     0
13 27.KW 2022  2111   20  429             10       937    86     28      474     1
14 28.KW 2022   752    5  121              4       373    42      3      172     0

CodePudding user response:

Your data frame df has a 2nd column called Total. It seems that you want to divide subsequent columns by this one.

df[-1] <- df[-1] / df$Total

After this, the 1st column Time does not change. 2nd column Total becomes 1. Other columns become proportions.

  •  Tags:  
  • r
  • Related