Ruben
Ruben

Reputation: 493

Standardize a column from a data frame based on a criterion in R

I would like to standardize the values from a column of a dataframe based on a criterion of other column of the same dataframe.

For example: I have a data frame with some measures of abundance ("abundance"), and I want to standardize these measures according to its classes ("a", "b" and "c").

 class <- c("a","a","a","a","a","b","b","b","b","c","c","c","c")
 abundance <- c(5,54,6,7,876,0,43,54,1,1,34,54,90)

 stand <- new column with the standardized values 

Do you know if there is some function that allow me to do it?

Thanks for your time

Upvotes: 0

Views: 93

Answers (2)

rnso
rnso

Reputation: 24613

try:

ddt = data.table(class, abundance)
ddt[,stdz:=(abundance-mean(abundance))/sd(abundance),by=class]
ddt
    class abundance       stdz
 1:     a         5 -0.4803884
 2:     a        54 -0.3528747
 3:     a         6 -0.4777860
 4:     a         7 -0.4751837
 5:     a       876  1.7862328
 6:     b         0 -0.8725918
 7:     b        43  0.6588959
 8:     b        54  1.0506718
 9:     b         1 -0.8369758
10:     c         1 -1.1744878
11:     c        34 -0.2885884
12:     c        54  0.2483203
13:     c        90  1.2147560

Can also use scale function as used by @akrun & @BondedDust:

ddt[,stdz:=scale(abundance),by=class]

Upvotes: 1

akrun
akrun

Reputation: 887691

May be you can use stdz from weights where you can also specify the weight

library(weights)
with(dat, ave(abundance, class, FUN=stdz))
#[1] -0.4803884 -0.3528747 -0.4777860 -0.4751837  1.7862328 -0.8725918
#[7]  0.6588959  1.0506718 -0.8369758 -1.1744878 -0.2885884  0.2483203
#[13]  1.2147560

Or use scale from base R

with(dat, ave(abundance, class, FUN=scale))
#[1] -0.4803884 -0.3528747 -0.4777860 -0.4751837  1.7862328 -0.8725918
#[7]  0.6588959  1.0506718 -0.8369758 -1.1744878 -0.2885884  0.2483203
#[13]  1.2147560

Upvotes: 0

Related Questions