3

I have data like this:

Name Data
A      5
A      6
A     -1
A     -3
B      6
B      2
B     -1
B      9

I want to normalize the data so the values are between -1 and 1. I also want to do group it by name.

Here is what I am trying:

library(dplyr)
library(scales)

df2 <- df %>% 
  group_by(name) %>%  
  rescale(Data,to=c(-1,1))

I get this error:

Error in UseMethod("rescale") : no applicable method for 'rescale' applied to an object of class "c('grouped_df', 'tbl_df', 'tbl', 'data.frame')"

akrun
  • 874,273
  • 37
  • 540
  • 662
AJ Jebelli
  • 101
  • 6
  • Does this answer your question? [Standardizing a vector in R so that values shift towards boundaries](https://stackoverflow.com/questions/66754306/standardizing-a-vector-in-r-so-that-values-shift-towards-boundaries) – eduardokapp Aug 05 '21 at 21:16
  • 1
    You misspelled “Name”. – IRTFM Aug 05 '21 at 21:52

2 Answers2

3

It should be within mutate

library(dplyr)
library(scales)
df %>% 
    group_by(Name) %>%
    mutate(Data = rescale(Data, to = c(-1, 1))) %>%
    ungroup

-output

# A tibble: 8 x 2
  Name    Data
  <chr>  <dbl>
1 A      0.778
2 A      1    
3 A     -0.556
4 A     -1    
5 B      0.4  
6 B     -0.4  
7 B     -1    
8 B      1    

data

df <- structure(list(Name = c("A", "A", "A", "A", "B", "B", "B", "B"
), Data = c(5L, 6L, -1L, -3L, 6L, 2L, -1L, 9L)), 
class = "data.frame", row.names = c(NA, 
-8L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you akrun. Could you please describe a scenario where normalizing data is important. Just one practical scenario in your mind. – TarJae Aug 05 '21 at 21:18
  • 1
    @TarJae when there are large difference in quantities in different variables, the one with higher values could tilt the model built – akrun Aug 05 '21 at 21:26
3

A base R option

transform(
  df,
  Data_normalize = ave(Data,
    Name,
    FUN = function(x) (x - min(x)) / diff(range(x)) * 2 - 1
  )
)

gives

  Name Data Data_normalize
1    A    5      0.7777778
2    A    6      1.0000000
3    A   -1     -0.5555556
4    A   -3     -1.0000000
5    B    6      0.4000000
6    B    2     -0.4000000
7    B   -1     -1.0000000
8    B    9      1.0000000
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81