根据r纵向数据的日期创建虚拟变量

发布于 2025-02-11 05:08:48 字数 1440 浏览 4 评论 0原文

我在R中有纵向患者数据。我想创建新的_Dummy变量,就像我在下表中所示。如果consultation_date变量小于registration_date变量,则我希望dummy_varia -varia -varia -varia -variable值为0,如果咨询_date变量大于registration_date变量,则为1。

patidConsultation_dateregistration_datenew_dummy
107/07/201607/07/20180
107/07/07/201907/07/07/20181
214/08/08/201607/09/2016 07/09/20160
307/05/2015/201519/02/02/02/02/02/02/02/02/02/ 20160
302/12/201619/02/20161

I have longitudinal patient data in R. I would like to create the new_dummy variable just like I demonstrated in the table below. I would like the dummy_variable value to be 0 if the consultation_date variable is smaller than the registration_date variable and to be 1 if the consultation_date variable is bigger than the registration_date variable.

patidconsultation_dateregistration_datenew_dummy
107/07/201607/07/20180
107/07/201907/07/20181
214/08/201607/09/20160
307/05/201519/02/20160
302/12/201619/02/20161

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

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

发布评论

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

评论(1

奶茶白久 2025-02-18 05:08:48

如果您的列在正确的类(date)中,则可以简单地

df$new_dummy <- as.numeric(df$consultation_date < df$registration_date)

以正确的格式转换它们,它是:(

df$consultation_date <- as.Date(df$consultation_date, "%d/%m/%Y")
df$registration_date <- as.Date(df$registration_date, "%d/%m/%Y")

感谢@akrun的日期格式)

If your columns are in the right class (Date), you can simply use

df$new_dummy <- as.numeric(df$consultation_date < df$registration_date)

To convert them in the right format, it's:

df$consultation_date <- as.Date(df$consultation_date, "%d/%m/%Y")
df$registration_date <- as.Date(df$registration_date, "%d/%m/%Y")

(thanks @akrun for the date format)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文