[英]How to refer to other column names within dplyr mutate across
Hopefully a simple question.希望是一个简单的问题。 I want to use dplyr mutate across, and refer to another static column to be used for all mutate functions.
我想使用 dplyr mutate,并引用另一个用于所有 mutate 函数的静态列。
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.我想为每天超过基线的变化创建一个新列“fc”。 I think I might need a combination of 'sym' and !!
我想我可能需要 'sym' 和 !! 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
警告消息:在 format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : 损坏的数据帧:列将被截断或用 NA 填充
UPDATE: 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?
当输入中有 NA 时,如何在输出中加入给出的 NA,而不是失败?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.