Reputation: 4243
I have a dataframe as follows:
A B C
ab gb 0.03
fn mn 0.12
po er 0.43
oo et 0.22
iu ew 0.77
ii mn 0.14
dd wr 0.99
qw ty 0.45
How do I normalize column C to be from the range of 0-5. I know you can do it like this with dplyr but it doesn't convert it to 0-5.
normalize <- function(x){
return((x-min(x)) / (max(x)-min(x)))
}
scaled_data <-
df %>%
group_by(A, B) %>%
mutate(NORMALIZED = normalize(C))
Upvotes: 2
Views: 3943
Reputation: 16277
Here's how to do it with dplyr
using scales::rescale
. I'll leave it to you to add group_by
.
df <-read.table(text=" A B C
ab gb 0.03
fn mn 0.12
po er 0.43
oo et 0.22
iu ew 0.77
ii mn 0.14
dd wr 0.99
qw ty 0.45",header=TRUE,stringsAsFactors=FALSE)
df%>%
mutate(C=scales::rescale(C,to=c(0, 5)))
A B C
1 ab gb 0.000000
2 fn mn 0.468750
3 po er 2.083333
4 oo et 0.989583
5 iu ew 3.854167
6 ii mn 0.572917
7 dd wr 5.000000
8 qw ty 2.187500
Upvotes: 4