用几列与小组创建小时的日期

发布于 2025-02-13 18:51:21 字数 694 浏览 1 评论 0原文

嗨,我有一个带有4列的表DF_IN,如下所示。我在尝试每小时使用几列的小时创建日期时会出现错误。

Gender = c("Male","Female")
Sec = c("AA","BB")
min_date = c("12/1/2018","12/9/2018")
max_date = c("12/3/2018","12/10/2018")
    
df_in = data.frame(Gender,Sec,min_date,max_date)

对于第一行号,行将为72(3天 * 24小时),对于第二行48行(2天 * 24小时),

df_out看起来像

“在此处输入图像说明”

我已经尝试了以下代码,

df_out = df_in %>% group_by(Gender, Sec) %>% mutate (DateSeries = seq(as.POSIXct(min_date), as.POSIXct(max_date), by="hour"))

谢谢。

Hi I have a table df_in with 4 columns as shown below.I am getting error while trying to create dateseries on hourly basis with group by few columns.

Gender = c("Male","Female")
Sec = c("AA","BB")
min_date = c("12/1/2018","12/9/2018")
max_date = c("12/3/2018","12/10/2018")
    
df_in = data.frame(Gender,Sec,min_date,max_date)

for 1st row number of rows will be 72 (3 days * 24 hours) and for 2nd row 48 rows (2 days * 24 hours)

df_out will look like

enter image description here

I have tried with below code

df_out = df_in %>% group_by(Gender, Sec) %>% mutate (DateSeries = seq(as.POSIXct(min_date), as.POSIXct(max_date), by="hour"))

Thanks in advance.

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

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

发布评论

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

评论(1

墨洒年华 2025-02-20 18:51:21

这会产生您需要的东西吗?

df_in %>% 
  group_by(Gender, Sec) %>% 
  expand(
    nesting(Gender,Sec), 
    DateSeries = seq(as.POSIXct(min_date), as.POSIXct(max_date), by="hour")
  )
# A tibble: 2,162 x 3
# Groups:   Gender, Sec [2]
   Gender Sec   DateSeries         
   <chr>  <chr> <dttm>             
 1 Female BB    0012-09-20 00:00:00
 2 Female BB    0012-09-20 01:00:00
 3 Female BB    0012-09-20 02:00:00
 4 Female BB    0012-09-20 03:00:00
 5 Female BB    0012-09-20 04:00:00
 6 Female BB    0012-09-20 05:00:00
 7 Female BB    0012-09-20 06:00:00
 8 Female BB    0012-09-20 07:00:00
 9 Female BB    0012-09-20 08:00:00
10 Female BB    0012-09-20 09:00:00
# ... with 2,152 more rows

您在突变上的第一次努力无法使用,因为您正在尝试将721个元素向量添加到只有一行的数据框架中。 展开是您需要的功能:它创建其输入的“所有可能组合”版本。

Does this produce what you need?

df_in %>% 
  group_by(Gender, Sec) %>% 
  expand(
    nesting(Gender,Sec), 
    DateSeries = seq(as.POSIXct(min_date), as.POSIXct(max_date), by="hour")
  )
# A tibble: 2,162 x 3
# Groups:   Gender, Sec [2]
   Gender Sec   DateSeries         
   <chr>  <chr> <dttm>             
 1 Female BB    0012-09-20 00:00:00
 2 Female BB    0012-09-20 01:00:00
 3 Female BB    0012-09-20 02:00:00
 4 Female BB    0012-09-20 03:00:00
 5 Female BB    0012-09-20 04:00:00
 6 Female BB    0012-09-20 05:00:00
 7 Female BB    0012-09-20 06:00:00
 8 Female BB    0012-09-20 07:00:00
 9 Female BB    0012-09-20 08:00:00
10 Female BB    0012-09-20 09:00:00
# ... with 2,152 more rows

Your first effort with mutate won't work because you're trying to add a 721 element vector to a data frame with only one row. expand is the function you need: it creates and "all possible combinations" version of its inputs.

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