5

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?

starball
  • 20,030
  • 7
  • 43
  • 238
user42485
  • 751
  • 2
  • 9
  • 19
  • I want 3 new columns fc to be added to the data frame with the default naming for each day (dayx_fc I think) or replacing the original columns. Either way works for me. – user42485 Oct 09 '20 at 15:36

1 Answers1

4

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
Duck
  • 39,058
  • 13
  • 42
  • 84
  • 1
    This works perfectly for my reprex. Now I realize my next issue is that I have some missing values in my 'day' columns, so am getting errors. Would you be able to help edit the ./baseline function to give NAs if the day value is missing? I will update the question. – user42485 Oct 09 '20 at 15:43
  • @user42485 Even with `NA` it works as you will get `NA` in the ratio. I have added the output with the new data you shared! – Duck Oct 09 '20 at 15:47
  • 1
    Got it! I think my values are actually missing, rather, not NA. But I will replace them to NA and then should work. Thanks! – user42485 Oct 09 '20 at 15:48