Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide each value in a data frame by column total in R?

Tags:

r

I use code

> df <- lapply(dane %>% select(starts_with("P_2_")),function(x) {factor(x,levels = 1:8)})
> data.frame(lapply(df,tabulate))

which get result

  P_2_1 P_2_2

1   520   109

2   113    83

3   249   129

4    58    76

5    79   189

6    30   327

7     6   104

8    48    86

What should I change in the code, to get additional columns with divided each value in a data frame by column total?

##     Odp            P_2_1         P_2_1             P_2_2          P_2_2
##   <dbl>            <int>          <dbl>            <int>          <dbl>
## 1     1              520          47.1               109           9.88
## 2     2              113          10.2                83           7.52
## 3     3              249          22.6               129          11.7 
## 4     4               58           5.26               76           6.89
## 5     5               79           7.16              189          17.1 
## 6     6               30           2.72              327          29.6 
## 7     7                6           0.54              104           9.43
## 8     8               48           4.35               86           7.8
like image 425
Chmielu PL Avatar asked Jan 24 '26 16:01

Chmielu PL


1 Answers

Divide by the colSums and cbind

out <- 100 * df1/colSums(df1)
cbind(Odp = seq_len(nrow(df1)), df1, out)

-output

   Odp P_2_1 P_2_2     P_2_1     P_2_2
1   1   520   109 47.144152  9.882140
2   2   113    83 10.244787  7.524932
3   3   249   129 22.574796 11.695376
4   4    58    76  5.258386  6.890299
5   5    79   189  7.162285 17.135086
6   6    30   327  2.719855 29.646419
7   7     6   104  0.543971  9.428830
8   8    48    86  4.351768  7.796917

NOTE: Make sure to rename the columns as duplicate column names can create issues


Or using dplyr

library(dplyr)
df1 %>%
  mutate(across(everything(), ~ .x/sum(.x), 
 .names = "{.col}_prop")) %>% 
  mutate(Odp = row_number(), .before = 1)
 Odp P_2_1 P_2_2 P_2_1_prop P_2_2_prop
1   1   520   109 0.47144152 0.09882140
2   2   113    83 0.10244787 0.07524932
3   3   249   129 0.22574796 0.11695376
4   4    58    76 0.05258386 0.06890299
5   5    79   189 0.07162285 0.17135086
6   6    30   327 0.02719855 0.29646419
7   7     6   104 0.00543971 0.09428830
8   8    48    86 0.04351768 0.07796917

data

df1 <- structure(list(P_2_1 = c(520L, 113L, 249L, 58L, 79L, 30L, 6L, 
48L), P_2_2 = c(109L, 83L, 129L, 76L, 189L, 327L, 104L, 86L)),
 class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8"))
like image 172
akrun Avatar answered Jan 26 '26 04:01

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!