如何将时间序列数据(Dates)预处理成两个对象?

How to preprocess time-series data (Dates) into two objects?

我想操作时间序列数据。但是,我的 R 技能非常有限。 这是我的复制代码的输出。

我的目标:

首先,我想从 “调查创建日期” 列中计算 一天 的观察次数。像这样 ---> ("2/6/2018 14:33", "2/6/2018 16:20", “2/6/2018 18:54”、“2/6/2018 20:08”、“2/6/2018 22:29”)是 5 个观察值。第二天是4,等等。这样函数就循环了。

此外,我想计算观察到的天数 --> 2/6/2018 直到 2/22/2018 (mdy)。 或者甚至可以将 2/6/2018 创建为自 1/1/2018 以来的天数。

我该怎么做? 我尝试将其转换为 as.Date,并使用了 as.POSIXct,但不知何故我犯了错误,我总是收到错误。

    structure(list(`Survey Creation Date` = c("2/6/2018 14:33", "2/6/2018 16:20", 
"2/6/2018 18:54", "2/6/2018 20:08", "2/6/2018 22:29", "2/7/2018 8:43", 
"2/7/2018 10:52", "2/7/2018 12:21", "2/7/2018 14:56", "2/7/2018 16:20"
), `Survey Completion Date` = c("2/6/2018 14:56", "2/6/2018 16:22", 
"2/6/2018 18:58", "2/6/2018 20:22", "2/6/2018 22:46", "2/7/2018 8:44", 
"2/7/2018 11:23", "2/7/2018 12:26", "2/7/2018 14:58", "2/7/2018 16:21"
), `Since your last survey; how many alcoholic drinks have you had?` = c(0, 
3, 0, 0, 0, 0, 0, 0, 0, 0), `I feel comfortable in my current location` = c(88, 
81, 88, 89, 95, 94, 62, 82, 63, 80), `I feel stressed` = c(10, 
12, 69, 34, 16, 6, 27, 35, 56, 28), `I feel down/depressed` = c(14, 
18, 15, 18, 5, 2, 8, 4, 0, 11)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

使用tidyverse我们可以做到:

library(tidyverse)

df %>%
  mutate(Date = as.Date(`Survey Creation Date`, format = "%m/%d/%Y")) %>%
  group_by(Date) %>%
  count() %>%
  mutate(Days_since_Jan_1 = Date - as.Date("2018-01-01"))
#> # A tibble: 2 x 3
#> # Groups:   Date [2]
#>   Date           n Days_since_Jan_1
#>   <date>     <int> <drtn>          
#> 1 2018-02-06     5 36 days         
#> 2 2018-02-07     5 37 days

reprex package (v2.0.1)

于 2022-05-08 创建