Context: I realise I am repeating the same thing three times below (to compute a weighted average with 3 different variables) and am wondering if anyone has an idea of how to optimise it? I thought a for loop may be too complicated.
Broader question: Is this bad practice in code? I am looking to share this publicly and am cleaning up my script (beginner here). Any tips/ advice would be much appreciated.
df_word2 <- df_word2 %>% mutate(VA_prod=(repet*VA_mean_sum)) # prep: product of repeats * mean V+A ratings
sumVAprod_word <- aggregate(df_word2[, 8], list(df_word2$year), sum) %>% rename(c("year"="Group.1", "sumVAprod_word"="x")) # sum VA_prod by year
df_word2 <- df_word2 %>% mutate(A_prod=(repet*A_mean_sum)) # prep: product of repeats * mean A ratings
sumAprod_word <- aggregate(df_word2[, 9], list(df_word2$year), sum) %>% rename(c("year"="Group.1", "sumAprod_word"="x")) # sum A_prod by year
df_word2 <- df_word2 %>% mutate(V_prod=(repet*V_mean_sum_r)) # prep: product of repeats * mean V ratings
sumVprod_word <- aggregate(df_word2[, 10], list(df_word2$year), sum) %>% rename(c("year"="Group.1", "sumVprod_word"="x"))# sum V_prod by year
These 3 lines may be the easiest to optimise I think.
# compute standardisation
word_year <- word_year %>% mutate(sev_word=(sumVAprod_word/sum_repet_word)) # V+A
word_year <- word_year %>% mutate(aro_word=(sumAprod_word/sum_repet_word)) # A
word_year <- word_year %>% mutate(val_word=(sumVprod_word/sum_repet_word)) # V