Home > OS >  Stantardized the values in R
Stantardized the values in R

Time:03-10

I would like to standardize the data below as follows:

M1 to M4 use: (x-min)/(max-min)

M5 use: (max-x)/(max-min)

result<-structure(list(
M1 = c(0.5265, 0.4256, 0.4256, 0.6600, 0.7382, 0.2761, 0.2767, 0.4578, 0.6068, 
0.3610, 0.6467, 0.2696, 0.4036, 0.6379, 0.6425, 0.5968, 0.6339), 
M2 = c(5.5843, 4.7899, 4.7899, 6.7878, 8.8461, 4.1691,4.1735, 5.4732, 6.4750, 4.4232,7.3049,4.1239,4.9035, 7.2553, 7.3411, 6.5079, 7.2392), 
M3 = c(0.6113, 0.6278, 0.6040, 0.5868, 0.0000, 0.9857, 0.8651, 0.7994, 0.5549, 0.8458, 0.5285, 1.0000, 0.8549, 0.5586, 0.5771, 0.5851, 0.5851),
M4 = c(0.75, -1.875, -2.625, 0.875, 8.625, -6.125, -4.375, -0.75, 2.875, -5.25, 4, -7.25, -4, 4.125, 5, 2.875, 3.125),
M5 = c(11.1942, -27.5446, -27.1662, 35.9843, 101.1193, -98.4829, -77.8855, -27.2394, 60.1665, -77.642, 75.6119,-121.073, -58.1242, 66.5695, 63.8372, 43.5262, 57.1483)), 
class = "data.frame", row.names = c(NA,-17L))

CodePudding user response:

Can be fairly quickly done with mutate in tidyverse

library(tidyverse)

result<-structure(list(
M1 = c(0.5265, 0.4256, 0.4256, 0.6600, 0.7382, 0.2761, 0.2767, 0.4578, 0.6068, 
0.3610, 0.6467, 0.2696, 0.4036, 0.6379, 0.6425, 0.5968, 0.6339), 
M2 = c(5.5843, 4.7899, 4.7899, 6.7878, 8.8461, 4.1691,4.1735, 5.4732, 6.4750, 4.4232,7.3049,4.1239,4.9035, 7.2553, 7.3411, 6.5079, 7.2392), 
M3 = c(0.6113, 0.6278, 0.6040, 0.5868, 0.0000, 0.9857, 0.8651, 0.7994, 0.5549, 0.8458, 0.5285, 1.0000, 0.8549, 0.5586, 0.5771, 0.5851, 0.5851),
M4 = c(0.75, -1.875, -2.625, 0.875, 8.625, -6.125, -4.375, -0.75, 2.875, -5.25, 4, -7.25, -4, 4.125, 5, 2.875, 3.125),
M5 = c(11.1942, -27.5446, -27.1662, 35.9843, 101.1193, -98.4829, -77.8855, -27.2394, 60.1665, -77.642, 75.6119,-121.073, -58.1242, 66.5695, 63.8372, 43.5262, 57.1483)), 
class = "data.frame", row.names = c(NA,-17L))

result <- result %>% 
  mutate(M1=(M1-min(M1))/(max(M1)-min(M1)),
         M2=(M2-min(M2))/(max(M2)-min(M2)),
         M3=(M3-min(M3))/(max(M3)-min(M3)),
         M4=(M4-min(M4))/(max(M4)-min(M4)),
         M5=(max(M5)-M5)/(max(M5)-min(M5)))

#output
structure(list(M1 = c(0.548228766538626, 0.332906530089629, 0.332906530089629, 
0.833119931711481, 1, 0.0138711054204012, 0.0151515151515151, 
0.401621852326078, 0.719590268886044, 0.195049082373026, 0.804737516005122, 
0, 0.285958173282117, 0.785958173282117, 0.795774647887324, 0.698250106700811, 
0.777422108408024), M2 = c(0.30926263182415, 0.141035957816272, 
0.141035957816272, 0.564122654694846, 1, 0.00957180974969301, 
0.010503578840371, 0.285735462284529, 0.497882342975732, 0.0633814747363516, 
0.673626699419762, 0, 0.165092541611961, 0.663123120579391, 0.681292617847613, 
0.504849434585575, 0.659713692770319), M3 = c(0.6113, 0.6278, 
0.604, 0.5868, 0, 0.9857, 0.8651, 0.7994, 0.5549, 0.8458, 0.5285, 
1, 0.8549, 0.5586, 0.5771, 0.5851, 0.5851), M4 = c(0.503937007874016, 
0.338582677165354, 0.291338582677165, 0.511811023622047, 1, 0.0708661417322835, 
0.181102362204724, 0.409448818897638, 0.637795275590551, 0.125984251968504, 
0.708661417322835, 0, 0.204724409448819, 0.716535433070866, 0.771653543307087, 
0.637795275590551, 0.653543307086614), M5 = c(0.404717445203997, 
0.579065521172426, 0.577362491859529, 0.293146972239812, 0, 0.898330860250333, 
0.805630078090015, 0.577691936219212, 0.184312417667039, 0.804534180527408, 
0.114798757652718, 1, 0.716692252611814, 0.155495037406787, 0.167792043198617, 
0.259203851798645, 0.197896146716155)), class = "data.frame", row.names = c(NA, 
-17L))

  •  Tags:  
  • r
  • Related