I am trying to use tidyverse (I hope this is the right term) to run some calculations. I need to first take the means of the control values (from df_1) and then use these means in the calculations from the treatments (df_2). The issue is the controls are paired with specific treatments and I'm not sure how to do this without using very long if-else statements so I thought I'd try this way. Unfortunately, it's not working. Maybe the if-else is the way to go? (but I still need to use two separate data frames!) Many thanks in advance.
The if-else statement would go something like this (for one treatment, there are 10 treatments in total):
df_2 <- df %>%
arrange(treatment_df2, bio_rep, technical_rep) %>% # organize rows in this nested order
group_by(treatment_df2) %>%
mutate(new_stat = (df_1$control_mean from paired treatment/value_treatment_df2) * (v/t)), na.rm = T) %>% # make a new column that calculates the wanted value
group_by(treatment_df2) %>%
mutate(n_fr = n()) %>% # count the value of the wanted value
filter(n_fr > 0, # keep only wanted values with non-zero values
!is.na(fvalue_treatment_df2)) %>% # drop rows with invalid frequencies
mean_value_treatment_df2_per_treatment = mean(new_stat, na.rm = TRUE)%>% # get the mean of the calculated value per treatment
select(treatment_df2, mean_value_treatment_df2_per_treatment) %>% # select certain columns
distinct() %>% as.data.frame() # drop duplicates.
To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one: