How can I normalize column values in a data frame for all rows that share the same ID given in another column?

1.4k views Asked by At

I have a dataframe that looks like this

ID  value
1   0.5
1   0.6
1   0.7
2   0.5
2   0.5
2   0.5

and I would like to add a column with normalization for values of the same ID like this: norm = value/max(values with same ID)

ID  value norm
1   0.5   0.5/0.7
1   0.6   0.6/0.7
1   0.7    1
2   0.5    1
2   0.3   0.3/0.5
2   0.5    1

Is there an easy way to do this in R without first sorting and then looping? Cheers

3

There are 3 answers

0
Zheyuan Li On BEST ANSWER

A solution using basic R tools:

data$norm <- with(data, value / ave(value, ID, FUN = max))

Function ave is pretty useful, and you may want to read ?ave.

0
www On
# Create an example data frame
dt <- read.csv(text = "ID, value
1, 0.5
1, 0.6
1, 0.7
2, 0.5
2, 0.5
               2, 0.5")

# Load package
library(tidyverse)

# Create a new data frame with a column showing normalization
dt2 <- dt %>%
  # Group the ID, make sure the following command works only in each group
  group_by(ID) %>%
  # Create the new column norm 
  # norm equals each value divided by the maximum value of each ID group
  mutate(norm = value/max(value))
0
akrun On

We can use data.table

library(data.table)
setDT(dt)[, norm := value/max(value), ID]