Can someone please help me correct my code?
I try to calculate the averaged sum of my columns after weighting them.
While the lowest category “Very unhappy” should get the lowest weight (‘1’), the highest category “Very happy” should get the highest weight (‘5’).
My dataframe looks like the following:
<code>structure(list(drivers = c("satisfaction_accessibility", "satisfaction_design",
"satisfaction_innovation", "satisfaction_quality", "satisfaction_quantity",
"satisfaction_support", "satisfaction_tech_stability"), `Very unhappy` = c(0L,
7L, 3L, 15L, 2L, 5L, 0L), Unhappy = c(0L, 3L, 4L, 5L, 5L, 9L,
13L), Neutral = c(4L, 3L, 10L, 1L, 2L, 7L, 2L), Happy = c(18L,
8L, 9L, 3L, 10L, 9L, 8L), `Very happy` = c(8L, 9L, 4L, 6L, 11L,
0L, 7L)), row.names = c(NA, -7L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x00000203f40a42c0>)
</code>
<code>structure(list(drivers = c("satisfaction_accessibility", "satisfaction_design",
"satisfaction_innovation", "satisfaction_quality", "satisfaction_quantity",
"satisfaction_support", "satisfaction_tech_stability"), `Very unhappy` = c(0L,
7L, 3L, 15L, 2L, 5L, 0L), Unhappy = c(0L, 3L, 4L, 5L, 5L, 9L,
13L), Neutral = c(4L, 3L, 10L, 1L, 2L, 7L, 2L), Happy = c(18L,
8L, 9L, 3L, 10L, 9L, 8L), `Very happy` = c(8L, 9L, 4L, 6L, 11L,
0L, 7L)), row.names = c(NA, -7L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x00000203f40a42c0>)
</code>
structure(list(drivers = c("satisfaction_accessibility", "satisfaction_design",
"satisfaction_innovation", "satisfaction_quality", "satisfaction_quantity",
"satisfaction_support", "satisfaction_tech_stability"), `Very unhappy` = c(0L,
7L, 3L, 15L, 2L, 5L, 0L), Unhappy = c(0L, 3L, 4L, 5L, 5L, 9L,
13L), Neutral = c(4L, 3L, 10L, 1L, 2L, 7L, 2L), Happy = c(18L,
8L, 9L, 3L, 10L, 9L, 8L), `Very happy` = c(8L, 9L, 4L, 6L, 11L,
0L, 7L)), row.names = c(NA, -7L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x00000203f40a42c0>)
The code I’ve written comes here:
<code>library(dplyr)
df_driver <- df_driver %>%
rowwise() %>%
mutate(weighted.scores = sum(df_driver$`Very unhappy`*1, df_driver$Unhappy*2, df_driver$Neutral*3, df_driver$Happy*4, df_driver$`Very happy`*5)/sum(df_driver$`Very unhappy`, df_driver$Unhappy, df_driver$Neutral, df_driver$Happy, df_driver$`Very happy`))
</code>
<code>library(dplyr)
df_driver <- df_driver %>%
rowwise() %>%
mutate(weighted.scores = sum(df_driver$`Very unhappy`*1, df_driver$Unhappy*2, df_driver$Neutral*3, df_driver$Happy*4, df_driver$`Very happy`*5)/sum(df_driver$`Very unhappy`, df_driver$Unhappy, df_driver$Neutral, df_driver$Happy, df_driver$`Very happy`))
</code>
library(dplyr)
df_driver <- df_driver %>%
rowwise() %>%
mutate(weighted.scores = sum(df_driver$`Very unhappy`*1, df_driver$Unhappy*2, df_driver$Neutral*3, df_driver$Happy*4, df_driver$`Very happy`*5)/sum(df_driver$`Very unhappy`, df_driver$Unhappy, df_driver$Neutral, df_driver$Happy, df_driver$`Very happy`))
Can someone please correct this code so that it doesnt bring the same value in the new column “weighted.scores” for each row? Any help appreciated.