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,200
  • 2
  • 14
  • 31
giegie
  • 463
  • 4
  • 11
  • 1
    Is `df$AMOUNT_NORM <- df$AMOUNT/sum(df$AMOUNT)` all you need? – Dason Feb 14 '19 at 18:39
  • that is right, but for the list of dataframes – giegie Feb 14 '19 at 18:51
  • Then you just want to apply that to each element of the list with `lapply` – divibisan Feb 14 '19 at 22:17
  • Thanks but still not very helpful. The answer below gives the correct output but it would be great to have the answer for your type of solution. I have tried this but it doesn't work: lapply (df, function (x) { x["AMOUNT_NORM"] <- NA x$AMOUNT_NORM <- x$AMOUNT/sum(x$AMOUNT) }) – giegie Feb 15 '19 at 10:38

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