为什么日期格式更改为双倍
这个问题与此问题有关-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在最后一行更改类从date
到double
,我不明白为什么。
我尝试过onect =“ 1970-01-01
,as.date
,ymd
...
所以我的问题是为什么会发生这种情况?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ifelse()
将类从更改为 double 的原因是
help> help(“ ifelse”)
:也许,
dplyr :: if_else()
可能更合适:返回
The reason for
ifelse()
changing the class fromdate
todouble
is documented inhelp("ifelse")
:Perhaps,
dplyr::if_else()
might be more appropriate here:returns