【问题标题】:Change dates within a tibble to a categorical variable [duplicate]将小标题中的日期更改为分类变量[重复]
【发布时间】:2021-10-02 22:27:35
【问题描述】:

我正在处理一个相当大的数据集,并尝试使用 mixor 函数运行混合效应多级回归。有人建议我将日期切换为分类变量,因为当前格式需要相当长的时间才能运行。我的数据存储在 tibble“arr_full”中,日期作为该 tibble 中的列存储为“rec_date”作为 POSIXct 数据类型。

我不想更改任何行的顺序,我想可能有一些方法可以简单地使用lubridate 来做到这一点。基本上,我希望将每个月的 15 天范围内的任何日期存储为分类变量。因此,2020 年 4 月 15 日至 30 日之间的任何日期都将为“1”,2020 年 5 月 1 日至 14 日为“2”,依此类推,直到 2021 年 4 月 1 日至 14 日为最后一个类别“24”。

我对 R 还很陌生,所以任何关于如何解决这个问题以及为什么会非常有帮助的解释。 提前感谢您的帮助!

**edit:我已经按照我尝试执行此操作的方式在下面添加了我的代码。诚然,实现它有点迂回,但这是我能想到的方法。我从我的有序日期列rec_date 中创建了一个单独的列,并创建了我想用指定变量替换的rec_date1。 我在运行代码时没有收到任何错误,但是当我运行 arr_full$rec_date1 时,它仍然输出原始日期,而不是替换的变量。

arr_full$rec_date1 <- ifelse(lubridate::month(arr_full$rec_date1) ==4 & lubridate::day(arr_full$rec_date1) >=15 , 1,
                          ifelse(lubridate::month(arr_full$rec_date1) ==5 & lubridate::day(arr_full$rec_date1) <15 , 2,
                                 ifelse(lubridate::month(arr_full$rec_date1) ==5 & lubridate::day(arr_full$rec_date1) >=15 , 3,
                                        ifelse(lubridate::month(arr_full$rec_date1) ==6 & lubridate::day(arr_full$rec_date1) <15 , 4, 
                          ifelse(lubridate::month(arr_full$rec_date1) ==6 & lubridate::day(arr_full$rec_date1) >=15 , 5,
                                  ifelse(lubridate::month(arr_full$rec_date1) ==7 & lubridate::day(arr_full$rec_date1) <15 , 6,
                                        ifelse(lubridate::month(arr_full$rec_date1) ==7 & lubridate::day(arr_full$rec_date1) >=15 , 7,
                          ifelse(lubridate::month(arr_full$rec_date1) ==8 & lubridate::day(arr_full$rec_date1) <15 , 8,
                                  ifelse(lubridate::month(arr_full$rec_date1) ==8 & lubridate::day(arr_full$rec_date1) >=15 , 9,
                                        ifelse(lubridate::month(arr_full$rec_date1) ==9 & lubridate::day(arr_full$rec_date1) <15 , 10,
                          ifelse(lubridate::month(arr_full$rec_date1) ==9 & lubridate::day(arr_full$rec_date1) >=15 , 11,
                                  ifelse(lubridate::month(arr_full$rec_date1) ==10 & lubridate::day(arr_full$rec_date1) <15 , 12,
                                        ifelse(lubridate::month(arr_full$rec_date1) ==10 & lubridate::day(arr_full$rec_date1) >=15 , 13,
                          ifelse(lubridate::month(arr_full$rec_date1) ==11 & lubridate::day(arr_full$rec_date1) <15 , 14,
                                  ifelse(lubridate::month(arr_full$rec_date1) ==11 & lubridate::day(arr_full$rec_date1) >=15 , 15,
                                        ifelse(lubridate::month(arr_full$rec_date1) ==12 & lubridate::day(arr_full$rec_date1) <15 , 16,
                          ifelse(lubridate::month(arr_full$rec_date1) ==12 & lubridate::day(arr_full$rec_date1) >=15 , 17,
                                  ifelse(lubridate::month(arr_full$rec_date1) ==1 & lubridate::day(arr_full$rec_date1) <15 , 18,
                                        ifelse(lubridate::month(arr_full$rec_date1) ==1 & lubridate::day(arr_full$rec_date1) >=15 , 19,
                          ifelse(lubridate::month(arr_full$rec_date1) ==2 & lubridate::day(arr_full$rec_date1) <15 , 20,
                                  ifelse(lubridate::month(arr_full$rec_date1) ==2 & lubridate::day(arr_full$rec_date1) >=15 , 21,
                                        ifelse(lubridate::month(arr_full$rec_date1) ==3 & lubridate::day(arr_full$rec_date1) <15 , 22,
                          ifelse(lubridate::month(arr_full$rec_date1) ==3 & lubridate::day(arr_full$rec_date1) >=15 , 23,
                                  ifelse(month(arr_full$rec_date1) ==4 & day(arr_full$rec_date1) <15 , 24, NA)))))))))))))))))))))))

> arr_full$rec_date1
   [1] "2020-06-12 GMT" "2020-07-25 GMT" "2020-09-04 GMT" "2020-12-07 GMT" "2020-06-12 GMT" "2020-07-25 GMT"
   [7] "2020-09-07 GMT" "2020-12-07 GMT" "2021-03-11 GMT" "2020-06-12 GMT" "2020-07-25 GMT" "2020-09-08 GMT"
  [13] "2020-12-07 GMT" "2021-03-08 GMT" "2020-06-20 GMT" "2020-07-26 GMT" "2020-06-20 GMT" "2020-09-11 GMT"...

【问题讨论】:

  • 我看不出这会让事情运行得更快。如果这是你唯一的目标,我认为这不会有帮助。谁给了这个建议?如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能的解决方案的示例输入和所需输出,那么为您提供帮助会更容易。
  • 您好弗里克先生,感谢您的反馈。我目前正在写一篇论文,所以我的主管根据她在类似数据集上工作的经验提出了建议。我当前的数据集有超过 22,000 行数据,根据向我解释的内容,R 可能难以解析出这么多日期。我将在后续注释中包含一些示例代码。

标签: r date variables lubridate categorical-data


【解决方案1】:

如果您的数据名为 df,其中有一个 Date 列属于 Date 类,您可以使用 cut,休息时间为 15 天。

df$group <- cut(df$Date, '15 days',labels = FALSE)

例子-

x <- Sys.Date() + 1:365
y <- cut(x, '15 days',labels = FALSE)

【讨论】:

  • 嗨 Ronak,这似乎可以解决问题。我对在代码中的何处使用它感到有些困惑,但早先使用它似乎工作得很好。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2019-09-07
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
相关资源
最近更新 更多