【问题标题】:Reordering a daily data series into weekly rows with Mon, Tue, Wed, Thur and Fri as the weekly columns将每日数据系列重新排序为每周行,其中周一、周二、周三、周四和周五作为每周列
【发布时间】:2020-02-16 15:43:05
【问题描述】:

我有一个包含工作日的每日时间序列,不包括周末。 我想对其重新排序,以便每列代表一周,随后的五行显示该周周一、周二、周三、周四和周五的数据。

我尝试使用cast(包:reshape),但在获取上述内容时遇到问题。

感谢您的帮助。

示例:

Date        Day Value
06/01/2020  mon 15
07/01/2020  tue 16
08/01/2020  wed 17
09/01/2020  thu 18
10/01/2020  fri 19
13/01/2020  mon 20
14/01/2020  tue 21
15/01/2020  wed 22
16/01/2020  thu 23
17/01/2020  fri 24

改写为:

Start of week   mon tue wed thu fri
06/01/2020      15  16  17  18  19
13/01/2020      20  21  22  23  24

【问题讨论】:

  • 您能否提供一个可重现的示例以便其他人学习?
  • 我刚刚更新了我的答案。现在输出与您的预期输出相同。

标签: r reshape


【解决方案1】:

-package 的另一个选项:

library(data.table)

# convert to a 'data.table'
# set the 'Date' and 'Day' columns in the right format
setDT(mydf)[, `:=` (Date = as.Date(Date, format = "%d/%m/%Y"),
                    Day = factor(Day, levels = c("mon","tue","wed","thu","fri")))]

# create a 'start_of_week' column
# transform from long to wide format
res <- mydf[, start_of_week := Date[1], by = cumsum(Day == "mon")
            ][, dcast(.SD, start_of_week ~ Day, value.var = "Value")]

给出:

> res
   start_of_week mon tue wed thu fri
1:    06/01/2020  15  16  17  18  19
2:    13/01/2020  20  21  22  23  24

使用过的数据:

mydf <- read.table(text="Date        Day Value
06/01/2020  mon 15
07/01/2020  tue 16
08/01/2020  wed 17
09/01/2020  thu 18
10/01/2020  fri 19
13/01/2020  mon 20
14/01/2020  tue 21
15/01/2020  wed 22
16/01/2020  thu 23
17/01/2020  fri 24", header=TRUE, stringsAsFactors=FALSE)

【讨论】:

  • 感谢您的解决方案。我有缺少值的数据,我收到一条错误消息:缺少聚合函数,默认为“长度”。有没有办法处理缺失值?
  • @adam.888 这是每天有不止一次观察(=行)的结果(不是由缺失数据引起的); see here for an explanation。您可以在 dcast 中使用 fun.aggregate = sum 来汇总一天的值。
【解决方案2】:

这是使用tidyverse 包的示例。我还使用lubridateDate 从字符类转换为日期类。

关键是使用tidyr包中的pivot_wider将数据转换为宽格式。

library(tidyverse)
library(lubridate)

dat2 <- dat %>%
  # Convert W to factor for ordering
  mutate(Day = factor(Day, levels = c("mon", "tue", "wed", "thu", "fri"))) %>%
  # Create a goruping variable to show the week number
  group_by(Day) %>%
  mutate(Group = 1:n()) %>%
  ungroup() %>%
  # Change the Date based on Group
  group_by(Group) %>%
  mutate(Date = min(dmy(Date))) %>%
  # Convert to wide format
  pivot_wider(names_from = Day, values_from = Value) %>%
  # Remove Group
  ungroup() %>%
  select(-Group)

dat2
# # A tibble: 2 x 6
#   Date         mon   tue   wed   thu   fri
#   <date>     <int> <int> <int> <int> <int>
# 1 2020-01-06    15    16    17    18    19
# 2 2020-01-13    20    21    22    23    24

数据

# Create example data frame
dat <- read.table(text = "Date Day Value

'06/01/2020' mon 15

'07/01/2020' tue 16

'08/01/2020' wed 17

'09/01/2020' thu 18

'10/01/2020' fri 19

'13/01/2020' mon 20

'14/01/2020' tue 21

'15/01/2020' wed 22

'16/01/2020' thu 23

'17/01/2020' fri 24",
                  header = TRUE, stringsAsFactors = FALSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2011-10-21
    • 2015-09-24
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多