-1

I have a problem, I want to normalize a column in a dataframe in order to have this column with a standard deviation equal to 1. The sd() function return only one value of standard deviation but I want to have a normalize vector in return.

Example of my df :

df <- data.frame(name = c("1", "2", "3"), date_article = c("feb. 2021", "jan.2021", "dec. 2020"), occurences = c(3, 0, 1))

Someone can help me ? Thanks you in advance !

Alan CUZON
  • 43
  • 8

1 Answers1

0

Do you mean scale for normalization?

df$occurences <- scale(df$occurences)

such that

> df
  name date_article occurences
1    1    feb. 2021  1.0910895
2    2     jan.2021 -0.8728716
3    3    dec. 2020 -0.2182179

and

> mean(df$occurences)
[1] 1.84992e-17

> sd(df$occurences)
[1] 1
`` 
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Thanks a lot that's perfect. I did not know the scale function! – Alan CUZON Feb 09 '21 at 16:19
  • Last thing I don't see how to make, I need to standardize my vector in order to have a mean equal to 100. I don't see any parameter in scale function in order to make that. Someone know how to specify the mean of an existing vector ? Thanks in advance ! – Alan CUZON Feb 09 '21 at 16:47
  • @AlanCUZON what about `df$occurences <- scale(df$occurences) + 100`? – ThomasIsCoding Feb 09 '21 at 22:07