Home > Software design >  Create a new column with some inherent conditions
Create a new column with some inherent conditions

Time:07-03

I would like to create a column called Weight, which would be as follows:

Weight = Mode (constant * mean).

Mode is column of my database.

Constant is alternative/total alternatives, in this case will be 0,1, because 1/10.

Mean is the average of the values ​​corresponding to a specific alternative. For example, the mean for alternative 10 is (3 2 2 3)/4 = 2,5.

Making an example for alternative 10 then. The weight would be 2 (0,1.2,5) = 2,25.

database<-structure(list(Alternatives = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 
12), Method1 = c(1L, 10L, 7L, 8L, 9L, 6L, 5L, 3L, 4L, 2L), Method2 = c(1L, 
8L, 6L, 7L, 10L, 9L, 4L, 2L, 3L, 5L), Method3 = c(1L, 10L, 7L, 
8L, 9L, 6L, 4L, 2L, 3L, 5L), Method4 = c(1L, 9L, 6L, 7L, 10L, 
8L, 5L, 3L, 4L, 2L), Mode = c(1, 10, 6, 7, 9, 6, 4, 2, 3, 2)), class = "data.frame", row.names = c(NA,-10L))

   Alternatives Method1 Method2 Method3 Method4 Mode
1             3       1       1       1       1    1
2             4      10       8      10       9   10
3             5       7       6       7       6    6
4             6       8       7       8       7    7
5             7       9      10       9      10    9
6             8       6       9       6       8    6
7             9       5       4       4       5    4
8            10       3       2       2       3    2
9            11       4       3       3       4    3
10           12       2       5       5       2    2

CodePudding user response:

If your constant equals 0.1 , then try

database |> mutate(Weight = Mode  
.1 * ((Method1   Method2   Method3   Method4) / 4))

  • Output
   Alternatives Method1 Method2 Method3 Method4 Mode Weight
1             3       1       1       1       1    1  1.100
2             4      10       8      10       9   10 10.925
3             5       7       6       7       6    6  6.650
4             6       8       7       8       7    7  7.750
5             7       9      10       9      10    9  9.950
6             8       6       9       6       8    6  6.725
7             9       5       4       4       5    4  4.450
8            10       3       2       2       3    2  2.250
9            11       4       3       3       4    3  3.350
10           12       2       5       5       2    2  2.350

if it is the probability then change it to

database |> mutate(Weight = Mode  
1/ nrow(database) * ((Method1   Method2   Method3   Method4) / 4)) |>
 mutate(rank = rank(Weight))

CodePudding user response:

This can also be accomplished using the base package.

attach(database)
database$Weight = .1 * (Method1   Method2   Method3   Method4)/4   Mode
database
#   Alternatives Method1 Method2 Method3 Method4 Mode Weight
#1             3       1       1       1       1    1  1.100
#2             4      10       8      10       9   10 10.925
#3             5       7       6       7       6    6  6.650
#4             6       8       7       8       7    7  7.750
#5             7       9      10       9      10    9  9.950
#6             8       6       9       6       8    6  6.725
#7             9       5       4       4       5    4  4.450
#8            10       3       2       2       3    2  2.250
#9            11       4       3       3       4    3  3.350
#10           12       2       5       5       2    2  2.350

One can also use the rank function on the resulting "weight" column.

database$weight = rank(database$weight)
  •  Tags:  
  • r
  • Related