Coolsun
Coolsun

Reputation: 189

How to find the normalized values within each level of a variable in R

I have a categorical variable B with 3 levels 1,2,3 also I have another variable A with some values.. sample data is as follows

A   B
22  1
23  1
12  1
34  1
43  2
47  2
49  2
65  2
68  3
70  3
75  3
82  3
120 3
.   .   
.   .   
.   .
.   .

All I want is say for every level of B ( say in 1) I need to calculate Val(A)-Min/Max-Min, similarly I need to reproduce the same to other levels (2 & 3)

Upvotes: 1

Views: 196

Answers (2)

Bing
Bing

Reputation: 1103

Solution using dplyr:

set.seed(1)
df=data.frame(A=round(rnorm(21,50,10)),B=rep(1:3,each=7))
library(dplyr)
df %>% group_by(B) %>% mutate(C= (A-min(A))/(max(A)-min(A)))

The output is like

# A tibble: 21 x 3
# Groups:   B [3]
       A     B      C
   <dbl> <int>  <dbl>
 1    44     1 0.0833
 2    52     1 0.417 
 3    42     1 0     
 4    66     1 1     
 5    53     1 0.458 
 6    42     1 0     
 7    55     1 0.542 
 8    57     2 0.784 
 9    56     2 0.757 
10    47     2 0.514 
# ... with 11 more rows

Upvotes: 2

mickey
mickey

Reputation: 2188

You could use the tapply function:

x = read.table(text="A   B
22  1
23  1
12  1
34  1
43  2
47  2
49  2
65  2
68  3
70  3
75  3
82  3
120 3", header = TRUE)

y = tapply(x$A, x$B, function(z) (z - min(z)) / (max(z) - min(z)))

# Or using the scale() function
#y = tapply(x$A, x$B, function(z) scale(z, min(z), max(z) - min(z)))

cbind(x, unlist(y))

Not exactly sure how you want the output, but this should be a decent starting point.

Upvotes: 1

Related Questions