Search code examples
rdplyracross

How to refer to other column names within dplyr mutate across?


I want to use dplyr mutate across, and refer to another static column to be used for all mutate functions.

df <- data.frame(baseline = c(1,2,3), day1 = c(NA,2,2), day2 = c(2,3,4), day3= c(5,4,6))

I want to make a new column 'fc' for the change from each day over the baseline. I think I might need a combination of 'sym' and !! around baseline to make it work but haven't figured it out.

df %>% mutate(fc = mutate(across(starts_with('day')), ./baseline))

gives the error

Warning message: In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : corrupt data frame: columns will be truncated or padded with NAs

I have some missing values in each day column so have edited the code above. How can I incorporate giving NAs in the output when there is an NA in the input, instead of failing?


Solution

  • Try this:

    library(dplyr)
    #Code
    df2 <- df %>% mutate(across(day1:day3,.fns = list(fc = ~ ./baseline)))
    

    Output:

      baseline day1 day2 day3   day1_fc  day2_fc day3_fc
    1        1    2    2    5 2.0000000 2.000000       5
    2        2    2    3    4 1.0000000 1.500000       2
    3        3    2    4    6 0.6666667 1.333333       2
    

    Or keeping the same variables:

    #Code 2
    df <- df %>% mutate(across(day1:day3,~ ./baseline))
    

    Output:

      baseline      day1     day2 day3
    1        1 2.0000000 2.000000    5
    2        2 1.0000000 1.500000    2
    3        3 0.6666667 1.333333    2
    

    With the new data added you will get this:

    #Code 3
    df2 <- df %>% mutate(across(day1:day3,.fns = list(fc = ~ ./baseline)))
    

    Output:

      baseline day1 day2 day3   day1_fc  day2_fc day3_fc
    1        1   NA    2    5        NA 2.000000       5
    2        2    2    3    4 1.0000000 1.500000       2
    3        3    2    4    6 0.6666667 1.333333       2