Search code examples
rvariablesgrouping

How can I compute a variable in R using a grouping variable?


I organized my dataset so that it would look like like this:

Sample Target Concentration

sample1 mutant 18.36

sample1 wildtype 3563.34

sample2 mutant 19.33

sample2 wildtype 3650.24

sample3 mutant 15.81

sample3 wildtype 3920.16

Sample Mutant/wildtype

sample1 18.36/356334

sample2 19.33/3650.24

sample3 15.81/3920.16

I want to calculate the mutant to wild type ratio by sample but couldn't find a specific argument in the mutate function of r for this seemingly simple task.


Solution

  • One thing you can do is pivot your data wider so all info about each sample is contained in a single row. We will create a new column for "mutant" and for "wildtype" and the values in these columns will be the concentrations.

    First, I created some dummy data to work with.

    data <- data.frame(sample = c(1,1,2,2,3,3), 
                      type = c("m", "w", "m", "w", "m", "w"), 
                      concentration = c(1,2,3,4,5,6))
    

    Dummy data:

      sample type concentration
    1      1    m             1
    2      1    w             2
    3      2    m             3
    4      2    w             4
    5      3    m             5
    6      3    w             6
    

    Here's what you do:

    library(tidyverse)
    
    data %>% 
      pivot_wider(names_from = type, values_from = concentration) %>% 
      mutate(ratio = m/w) -> data
    

    And this is what you get:

    # A tibble: 3 × 4
      sample     m     w ratio
       <dbl> <dbl> <dbl> <dbl>
    1      1     1     2 0.5  
    2      2     3     4 0.75 
    3      3     5     6 0.833