R 根据条件跨列搜索最小日期

发布于 2025-01-20 10:25:43 字数 1197 浏览 1 评论 0原文

我有以下数据框架,其中有几个日期变量。

    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))

我想将date1date2和生成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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆里有你的影子 2025-01-27 10:25:43

您可以使用 pmin 选择指定列的第一个日期。您可以使用以下代码:

library(dplyr)
x %>% 
  mutate(DN = case_when(
    s1 == 1 | s2 == 1 ~ pmin(date1, date2, na.rm = T),
    s1 == 0 | s2 == 0 ~ date,
    is.na(s1) & is.na(s2) ~ NA_real_
  ))

输出:

# A tibble: 4 × 7
     id date          s1 date1         s2 date2      DN        
  <dbl> <date>     <dbl> <date>     <dbl> <date>     <date>    
1     1 2020-12-15     0 2018-12-15     0 2018-12-15 2020-12-15
2     2 2016-01-01     1 2015-01-15     0 NA         2015-01-15
3     3 2016-01-01     1 2014-12-12     1 2013-06-13 2013-06-13
4     4 2018-01-01    NA NA            NA NA         NA 

You can use pmin to select the first date of the assigned columns. You can use the following code:

library(dplyr)
x %>% 
  mutate(DN = case_when(
    s1 == 1 | s2 == 1 ~ pmin(date1, date2, na.rm = T),
    s1 == 0 | s2 == 0 ~ date,
    is.na(s1) & is.na(s2) ~ NA_real_
  ))

Output:

# A tibble: 4 × 7
     id date          s1 date1         s2 date2      DN        
  <dbl> <date>     <dbl> <date>     <dbl> <date>     <date>    
1     1 2020-12-15     0 2018-12-15     0 2018-12-15 2020-12-15
2     2 2016-01-01     1 2015-01-15     0 NA         2015-01-15
3     3 2016-01-01     1 2014-12-12     1 2013-06-13 2013-06-13
4     4 2018-01-01    NA NA            NA NA         NA 
等往事风中吹 2025-01-27 10:25:43

如果您添加rowwise()(即行分组),则将获得所需的行最小值:

x %>% 
  rowwise() %>% 
  mutate(date = as.Date(date),
         date1 =  as.Date(date1),
         date2 =  as.Date(date2),
         DN = case_when(
           s1 == 1 | s2 == 1 ~ pmin(date1, date2, na.rm = T),
           s1 == 0 | s2 == 0 ~ date,
           is.na(s1) & is.na(s2) ~ NA_real_
         ))

If you add rowwise() (i.e. grouping by rows) you will get the required row-minimum:

x %>% 
  rowwise() %>% 
  mutate(date = as.Date(date),
         date1 =  as.Date(date1),
         date2 =  as.Date(date2),
         DN = case_when(
           s1 == 1 | s2 == 1 ~ pmin(date1, date2, na.rm = T),
           s1 == 0 | s2 == 0 ~ date,
           is.na(s1) & is.na(s2) ~ NA_real_
         ))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文