为什么日期格式更改为双倍

发布于 2025-01-25 00:12:38 字数 1958 浏览 3 评论 0原文

这个问题与此问题有关-earliest-and“ by ID和drug(彼此之间使用日期< 100天)进行最早和最新日期

数据集为:

mydata = data.frame (Id =c(1,1,1,1,1,1,1,1,1,1),
                     Date = c("2000-01-01","2000-01-05","2000-02-02", "2000-02-12", 
                              "2000-02-14","2000-05-13", "2000-05-15", "2000-05-17", 
                              "2000-05-16", "2000-05-20"),
                     drug = c("A","A","B","B","B","A","A","A","C","C"))

   Id       Date drug
1   1 2000-01-01    A
2   1 2000-01-05    A
3   1 2000-02-02    B
4   1 2000-02-12    B
5   1 2000-02-14    B
6   1 2000-05-13    A
7   1 2000-05-15    A
8   1 2000-05-17    A
9   1 2000-05-16    C
10  1 2000-05-20    C

使用此代码:

library(lubridate)
library(dplyr)

mydata %>% 
  group_by(Id, drug) %>% 
  mutate(Date = ymd(Date),
         Diff = as.numeric(Date - lag(Date, default = Date[1])),
         startDate = min(Date, na.rm = T),
         endDate = max(Date, na.rm = T),
         startDate =  ifelse(Diff > 100, Date, startdate)
         )

      Id Date       drug   Diff startDate endDate   
   <dbl> <date>     <chr> <dbl>     <dbl> <date>    
 1     1 2000-01-01 A         0     17257 2000-05-17
 2     1 2000-01-05 A         4     17257 2000-05-17
 3     1 2000-02-02 B         0     17257 2000-02-14
 4     1 2000-02-12 B        10     17257 2000-02-14
 5     1 2000-02-14 B         2     17257 2000-02-14
 6     1 2000-05-13 A       129     11090 2000-05-17
 7     1 2000-05-15 A         2     17257 2000-05-17
 8     1 2000-05-17 A         2     17257 2000-05-17
 9     1 2000-05-16 C         0     17257 2000-05-20
10     1 2000-05-20 C         4     17257 2000-05-20

startDate column在最后一行更改类从datedouble,我不明白为什么。

我尝试过onect =“ 1970-01-01as.dateymd ...

所以我的问题是为什么会发生这种情况?

This question is related to this Group by id and drug (with dates <100 days of each other) take the earliest and latest date

The dataset is:

mydata = data.frame (Id =c(1,1,1,1,1,1,1,1,1,1),
                     Date = c("2000-01-01","2000-01-05","2000-02-02", "2000-02-12", 
                              "2000-02-14","2000-05-13", "2000-05-15", "2000-05-17", 
                              "2000-05-16", "2000-05-20"),
                     drug = c("A","A","B","B","B","A","A","A","C","C"))

   Id       Date drug
1   1 2000-01-01    A
2   1 2000-01-05    A
3   1 2000-02-02    B
4   1 2000-02-12    B
5   1 2000-02-14    B
6   1 2000-05-13    A
7   1 2000-05-15    A
8   1 2000-05-17    A
9   1 2000-05-16    C
10  1 2000-05-20    C

With this code:

library(lubridate)
library(dplyr)

mydata %>% 
  group_by(Id, drug) %>% 
  mutate(Date = ymd(Date),
         Diff = as.numeric(Date - lag(Date, default = Date[1])),
         startDate = min(Date, na.rm = T),
         endDate = max(Date, na.rm = T),
         startDate =  ifelse(Diff > 100, Date, startdate)
         )

      Id Date       drug   Diff startDate endDate   
   <dbl> <date>     <chr> <dbl>     <dbl> <date>    
 1     1 2000-01-01 A         0     17257 2000-05-17
 2     1 2000-01-05 A         4     17257 2000-05-17
 3     1 2000-02-02 B         0     17257 2000-02-14
 4     1 2000-02-12 B        10     17257 2000-02-14
 5     1 2000-02-14 B         2     17257 2000-02-14
 6     1 2000-05-13 A       129     11090 2000-05-17
 7     1 2000-05-15 A         2     17257 2000-05-17
 8     1 2000-05-17 A         2     17257 2000-05-17
 9     1 2000-05-16 C         0     17257 2000-05-20
10     1 2000-05-20 C         4     17257 2000-05-20

the startDate column changes at the last line the class from date to double and I don't understand why.

I have tried origin= "1970-01-01, as.Date, ymd ...

So my question is why does this happen?

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

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

发布评论

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

评论(1

生活了然无味 2025-02-01 00:12:38

ifelse()将类从更改为 double 的原因是help> help(“ ifelse”)

结果的模式可能取决于测试的值(请参见示例),并且结果的类属性(请参见OldClass)是从测试中获取的,并且可能不适合从Yes和否中选择的值。 /p>

也许,dplyr :: if_else()可能更合适:

mydata %>% 
  group_by(Id, drug) %>% 
  mutate(Date = lubridate::ymd(Date),
         Diff = as.numeric(Date - lag(Date, default = Date[1])),
         startDate = min(Date, na.rm = T),
         endDate = max(Date, na.rm = T),
         startDate =  if_else(Diff > 100, Date, startDate)
  )

返回

 #a tibble:10×6
#组:ID,毒品[3]
      ID日期药物差异开始末日   
   &lt; dbl&gt; &lt; date&gt; &lt; fct&gt; &lt; dbl&gt; &lt; date&gt; &lt; date&gt;    
 1 1 2000-01-01 A 0 2000-01-01 2000-05-17
 2 1 2000-01-05 A 4 2000-01-01 2000-05-17
 3 1 2000-02-02 B 0 2000-02-02 2000-02-14
 4 1 2000-02-12 B 10 2000-02-02 2000-02-14
 5 1 2000-02-14 B 2 2000-02-02 2000-02-14
 6 1 2000-05-13 A 129 2000-05-13 2000-05-17
 7 1 2000-05-15 A 2 2000-01-01 2000-05-17
 8 1 2000-05-17 A 2 2000-01-01 2000-05-17
 9 1 2000-05-16 C 0 2000-05-16 2000-05-20
10 1 2000-05-20 C 4 2000-05-16 2000-05-20
 

The reason for ifelse() changing the class from date to double is documented in help("ifelse"):

The mode of the result may depend on the value of test (see the examples), and the class attribute (see oldClass) of the result is taken from test and may be inappropriate for the values selected from yes and no.

Perhaps, dplyr::if_else() might be more appropriate here:

mydata %>% 
  group_by(Id, drug) %>% 
  mutate(Date = lubridate::ymd(Date),
         Diff = as.numeric(Date - lag(Date, default = Date[1])),
         startDate = min(Date, na.rm = T),
         endDate = max(Date, na.rm = T),
         startDate =  if_else(Diff > 100, Date, startDate)
  )

returns

# A tibble: 10 × 6
# Groups:   Id, drug [3]
      Id Date       drug   Diff startDate  endDate   
   <dbl> <date>     <fct> <dbl> <date>     <date>    
 1     1 2000-01-01 A         0 2000-01-01 2000-05-17
 2     1 2000-01-05 A         4 2000-01-01 2000-05-17
 3     1 2000-02-02 B         0 2000-02-02 2000-02-14
 4     1 2000-02-12 B        10 2000-02-02 2000-02-14
 5     1 2000-02-14 B         2 2000-02-02 2000-02-14
 6     1 2000-05-13 A       129 2000-05-13 2000-05-17
 7     1 2000-05-15 A         2 2000-01-01 2000-05-17
 8     1 2000-05-17 A         2 2000-01-01 2000-05-17
 9     1 2000-05-16 C         0 2000-05-16 2000-05-20
10     1 2000-05-20 C         4 2000-05-16 2000-05-20
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文