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?
Try this:
Output:
Or keeping the same variables:
Output:
With the new data added you will get this:
Output: