0

I have a LIST of dataframes. Each dataframe has the same numer of rows and columns.

Here is a sample dataframe:

df
TIME  AMOUNT
20     456
30     345
15     122
12     267

Here is the expected RESULT: I would like to count the AMOUNT_NORM column where

each value in the AMOUNT column was divided by the sum of all values in the AMOUNT column.

df
TIME  AMOUNT AMOUNT_NORM
20     456   0.38
30     345   0.29
15     122   0.1
12     267   0.22
Tamara Koliada
  • 1,194
  • 2
  • 12
  • 31
giegie
  • 391
  • 3
  • 10

1 Answers1

2

The following should do what you want

library(tidyverse)
df %>% mutate(AMOUNT_NORM = AMOUNT/SUM(AMOUNT))

EDIT: didn't read the list of dataframes bit. in this case you just do:

lapply(your_df_list, function(x) {
   x %>% mutate(AMOUNT_NORM = AMOUNT/SUM(AMOUNT))
})
jludewig
  • 428
  • 2
  • 8