创建一个数据 table,日期为周,一列为条目数
Creating a data table with dates as weeks and a column for number of entries
我正在尝试在 R 中创建一个新数据 sheet,将日期分组为一个月内的周数,例如6 月 1 日、6 月 2 日、6 月 3 日……然后是一个新栏,总结每个栏中的条目数。比如我现在的数据集是:
我正在尝试隔离所有 'Vigilent' 的条目,我已经使用 data2 <- data2 %>% filter(data2$Activity == "Vigilent")
完成了这些条目,但是我现在需要创建一个新的数据集,将条目分组到一个月中的几周。
我已经使用以下方法将数据分组为一年中的几周:
x <- date2week(data2$Date, week_start = "Sunday", floor_day = TRUE)
table(x)
x
2008-W24 2008-W25 2008-W26
1 3 4
但我希望它采用 'Week 1 June, Week 2 June etc' 格式,并在单独的列中显示每周的条目数。这可能吗?
Here is the output from dput()
structure(list(Date = c("14/06/2008", "18/06/2008", "19/06/2008",
"20/06/2008", "23/06/2008", "25/06/2008", "26/06/2008", "27/06/2008",
"28/06/2008"), Location = c("Park", "Park", "Grassland", "Grassland",
"Grassland", "Grassland", "Grassland", "Forest", "Park"), Age = c("Adult M",
"Adult F", "Adult F", "Adult F", "Adult M", "Adult M", "Adult M",
"Adult M", "Adult F"), Activity = c("Vigilient", "Vigilient",
"Vigilient", "Vigilient", "Vigilient", "Vigilient", "Vigilient",
"Vigilient", "Foraging")), class = "data.frame", row.names = c(NA,
-9L))
My desired output is as follows (count values are fictional):
例如我其他列可以打折,我只想知道在 7 月的给定一周内发生了多少次 Vigilent
library(tidyverse)
library(lubridate)
df %>%
dplyr::filter(Activity == "Vigilient") %>%
dplyr::mutate(Date = dmy(Date),
Month = month(Date, label = T, abbr = F),
Month_Week = week(floor_date(Date, unit = "week", week_start = 7))- week(floor_date(Date, unit = "month")) + 1,
Label = sprintf("Week %i %s", Month_Week, Month)) %>%
dplyr::count(Label, name = "Vigilence Count")
输出
Label Vigilence Count
1 Week 2 June 1
2 Week 3 June 3
3 Week 4 June 4
- 大多数日期函数来自
lubridate
库:dmy
、month
、week
.
Date
被此代码覆盖。它成为一个日期对象。如果这不是您想要的 behavior/you 需要保留原始 Date
列,那么您可能想在 mutate
函数中将其重命名为其他名称。
我正在尝试在 R 中创建一个新数据 sheet,将日期分组为一个月内的周数,例如6 月 1 日、6 月 2 日、6 月 3 日……然后是一个新栏,总结每个栏中的条目数。比如我现在的数据集是:
我正在尝试隔离所有 'Vigilent' 的条目,我已经使用 data2 <- data2 %>% filter(data2$Activity == "Vigilent")
完成了这些条目,但是我现在需要创建一个新的数据集,将条目分组到一个月中的几周。
我已经使用以下方法将数据分组为一年中的几周:
x <- date2week(data2$Date, week_start = "Sunday", floor_day = TRUE)
table(x)
x
2008-W24 2008-W25 2008-W26
1 3 4
但我希望它采用 'Week 1 June, Week 2 June etc' 格式,并在单独的列中显示每周的条目数。这可能吗?
Here is the output from dput()
structure(list(Date = c("14/06/2008", "18/06/2008", "19/06/2008",
"20/06/2008", "23/06/2008", "25/06/2008", "26/06/2008", "27/06/2008",
"28/06/2008"), Location = c("Park", "Park", "Grassland", "Grassland",
"Grassland", "Grassland", "Grassland", "Forest", "Park"), Age = c("Adult M",
"Adult F", "Adult F", "Adult F", "Adult M", "Adult M", "Adult M",
"Adult M", "Adult F"), Activity = c("Vigilient", "Vigilient",
"Vigilient", "Vigilient", "Vigilient", "Vigilient", "Vigilient",
"Vigilient", "Foraging")), class = "data.frame", row.names = c(NA,
-9L))
My desired output is as follows (count values are fictional):
例如我其他列可以打折,我只想知道在 7 月的给定一周内发生了多少次 Vigilent
library(tidyverse)
library(lubridate)
df %>%
dplyr::filter(Activity == "Vigilient") %>%
dplyr::mutate(Date = dmy(Date),
Month = month(Date, label = T, abbr = F),
Month_Week = week(floor_date(Date, unit = "week", week_start = 7))- week(floor_date(Date, unit = "month")) + 1,
Label = sprintf("Week %i %s", Month_Week, Month)) %>%
dplyr::count(Label, name = "Vigilence Count")
输出
Label Vigilence Count
1 Week 2 June 1
2 Week 3 June 3
3 Week 4 June 4
- 大多数日期函数来自
lubridate
库:dmy
、month
、week
. Date
被此代码覆盖。它成为一个日期对象。如果这不是您想要的 behavior/you 需要保留原始Date
列,那么您可能想在mutate
函数中将其重命名为其他名称。