Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalize by Group

Tags:

r

dplyr

normalize

I'm trying to normalize the StrengthCode by Item

E.g.

ID    Item    StrengthCode
7     A       1
7     A       5
7     A       7
8     B       1
8     B       3
9     A       5
9     A       3

What I need to achieve is something like this:

ID    Item    StrengthCode    Nor
7     A       1    0.14
7     A       5    0.71
7     A       7    1
8     B       1    0.34
8     B       3    1
9     A       5    0.71
9     A       3    0.42

I tried this code but I'm stuck.... if you can help me would be awesome!!!

normalit <- function(m){(m - min(m))/(max(m)-min(m))}

Tbl.Test <- Tbl.3.1 %>%
  group_by(ID, Item) %>%
  mutate(Nor = normalit(StregthCode))

I get this error:

Warning message NAs introduced by coercion

like image 648
Seb Avatar asked Apr 28 '17 12:04

Seb


People also ask

What is Group normalization?

Group Normalization is a normalization layer that divides channels into groups and normalizes the features within each group. GN does not exploit the batch dimension, and its computation is independent of batch sizes. In the case where the group size is 1, it is equivalent to Instance Normalization.

What does it mean to normalize by population?

To normalize, in a statistical sense, is to transform a set of measurements so that they may be compared in a meaningful way. Technically, normalization involves factoring out the size of the domain when you wish to compare counts collected over unequal areas or populations.


1 Answers

Your desired output looks like you are wanting this:

df <- read.table(header=TRUE, text=
'ID    Item    StrengthCode
7     A       1
7     A       5
7     A       7
8     B       1
8     B       3
9     A       5
9     A       3')
df$Nor <- ave(df$StrengthCode, df$Item, FUN=function(x) x/max(x)) 
df
# > df
#   ID Item StrengthCode       Nor
# 1  7    A            1 0.1428571
# 2  7    A            5 0.7142857
# 3  7    A            7 1.0000000
# 4  8    B            1 0.3333333
# 5  8    B            3 1.0000000
# 6  9    A            5 0.7142857
# 7  9    A            3 0.4285714

With dplyr you can do (thx to Sotos for the comment+code):

library("dplyr")
(df %>% group_by(Item) %>% mutate(Nor = StrengthCode/max(StrengthCode))) 
# > (df %>% group_by(Item) %>% mutate(Nor = StrengthCode/max(StrengthCode)))
# Source: local data frame [7 x 4]
# Groups: Item [2]
# 
#      ID   Item StrengthCode       Nor
#   <int> <fctr>        <int>     <dbl>
# 1     7      A            1 0.1428571
# 2     7      A            5 0.7142857
# 3     7      A            7 1.0000000
# 4     8      B            1 0.3333333
# 5     8      B            3 1.0000000
# 6     9      A            5 0.7142857
# 7     9      A            3 0.4285714
like image 181
jogo Avatar answered Sep 27 '22 19:09

jogo