how to optimise code?

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

You could create a function and use that. You could even combine it with one of the purrr::map() functions.

Otherwise, for the last bit:

word_year <- word_year %>% mutate(sev_word=(sumVAprod_word/sum_repet_word), # V+A
                                  aro_word=(sumAprod_word/sum_repet_word), # A
                                  val_word=(sumVprod_word/sum_repet_word)) # V

Anyway, a reproducible example of df_word2 and word_year would help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.