user2506086
user2506086

Reputation: 513

Normalize a variable data.table

I'm looking to normalize a variable in a data.table by subtracting the mean within each group. I have done it in the following manner:

dx <- data.table(x=c(1,3,5,1,8,11),group=factor(c(1,1,1,2,2,2)))
dy <- dx[,.(xmean=mean(x)),by=.(group)]
setkey(dx,group)
setkey(dy,group)
dx[dy,x_norm:=x-xmean]

I'm wondering if there is a more concise way of doing this?

Upvotes: 2

Views: 2108

Answers (1)

Chris
Chris

Reputation: 6372

You can use the scale function to do this:

dx[, x_norm := scale(x, center = TRUE, scale = FALSE), by = group]

This is equivalent to @Hadd E. Nuff's way of:

dx[, x_norm := x - mean(x), by = group]

Upvotes: 9

Related Questions