R 根据条件跨列搜索最小日期
我有以下数据框架,其中有几个日期变量。
x <- structure(list(id = c(1, 2, 3, 4), date = structure(c(18611,
16801, 16801, 17532), class = "Date"), s1 = c(0, 1, 1, NA), date1 = structure(c(17880,
16450, 16416, NA), class = "Date"), s2 = c(0, 0, 1, NA), date2 = structure(c(17880,
NA, 15869, NA), class = "Date"), DN = structure(c(18611, 15869,
15869, NA), class = "Date")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L))
我想将date1
和date2
和生成dn
根据case_when 。我目前正在使用此代码:
x <- mutate(date = as.Date(date),
date1 = as.Date(date1),
date2 = as.Date(date2),
DN = case_when(
s1 == 1 | s2 == 1 ~ min(date1, date2, na.rm = T),
s1 == 0 | s2 == 0 ~ date,
is.na(s1) & is.na(s2) ~ NA_real_
))
但是,我会得到一个奇怪的结果!对于id = 2
dn
的值是从id = 3
中获取的,我无法理解!
有什么想法吗? 感谢向前
I have the following data frame with several date variables.
x <- structure(list(id = c(1, 2, 3, 4), date = structure(c(18611,
16801, 16801, 17532), class = "Date"), s1 = c(0, 1, 1, NA), date1 = structure(c(17880,
16450, 16416, NA), class = "Date"), s2 = c(0, 0, 1, NA), date2 = structure(c(17880,
NA, 15869, NA), class = "Date"), DN = structure(c(18611, 15869,
15869, NA), class = "Date")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L))
I would like to compare date1
and date2
and generate DN
as the minimum of the two dates based on the conditions in case_when
. I am currently using this code:
x <- mutate(date = as.Date(date),
date1 = as.Date(date1),
date2 = as.Date(date2),
DN = case_when(
s1 == 1 | s2 == 1 ~ min(date1, date2, na.rm = T),
s1 == 0 | s2 == 0 ~ date,
is.na(s1) & is.na(s2) ~ NA_real_
))
However, I get a strange result !! For id = 2
the value of DN
is taken from id = 3
, which I can not understand !!
Any ideas?
Thanks in forward
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
pmin
选择指定列的第一个日期。您可以使用以下代码:输出:
You can use
pmin
to select the first date of the assigned columns. You can use the following code:Output:
如果您添加
rowwise()
(即行分组),则将获得所需的行最小值:If you add
rowwise()
(i.e. grouping by rows) you will get the required row-minimum: