using tidyverse for paired calculation from multiple data frames


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):

if(treatment == "A"){
df_1$mean_value = control_A/reading_A)(v/t)
else {(treatment == "B"){
df_1$mean_value = control_B/reading_B)

df_1 <- df %>%
group_by(p_control) %>%
mutate(control_mean = mean(reading, na.rm = T)) %>%
select(treatment, control_mean) %>%
distinct() %>%

t <- 7 #### length of trial
v <-10 #### volume, L

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
! %>% # 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() %>% # 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:


summary_df <- df %>%
group_by(plate_control) %>%
mutate(control_mean = mean(use_fluor_control, na.rm = T)) %>%
select(plate_control, control_mean) %>%
distinct() %>%

Sorry, I can't get the datapasta to work...

Well, if for some strange reason, datapasta is not working, you can still use dput(), the thing is we need sample data to reproduce your issue.

Try this guide

followed by this one

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