下午好,
我试图根据以下数据在R Studio中创建一些地块:
structure(list(Transaction = c(1L, 2L, 2L, 3L, 3L, 3L), Item = c("Bread",
"Scandinavian", "Scandinavian", "Hot chocolate", "Jam", "Cookies"
), period_day = c("morning", "morning", "morning", "morning",
"morning", "morning"), weekday_weekend = c("weekend", "weekend",
"weekend", "weekend", "weekend", "weekend"), Month = c("October",
"October", "October", "October", "October", "October"), Time = c("09",
"10", "10", "10", "10", "10")), row.names = c(NA, 6L), class = "data.frame")我正在尝试创建条形图以直观地探索数据,例如,我想要绘制:
例如:
我怎样才能开始制作这些情节?我尝试了以下代码:
SalesByTime <- bb_raw %>%
group_by(Time, Item) %>%
summarise(Transactions = sum(Item))但我相信这是交易数字的总和,而不是销售频率(见下文)。
structure(list(Time = c("01", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
), Transactions = c(4090L, 59889L, 3143112L, 9168556L, 12679593L,
15159832L, 14129139L, 13231165L, 13633823L, 11081386L, 7053231L,
1969450L, 289382L, 223385L, 104967L, 16411L, 76746L, 22825L)), row.names = c(NA,
-18L), class = c("tbl_df", "tbl", "data.frame"))有什么建议吗?任何帮助都将不胜感激。如果我能提供更多的信息,请告诉我。
发布于 2021-01-09 18:40:58
如果我理解你的话,你想知道每个时间段内每组观察的数量。您可以使用dplyr::n()来完成这个任务。
df %>%
group_by(Time, Item) %>%
summarise(Transactions = n())
# Time Item Transactions
# <chr> <chr> <int>
# 1 09 Bread 1
# 2 10 Cookies 1
# 3 10 Hot chocolate 1
# 4 10 Jam 1
# 5 10 Scandinavian 2若要仅按时间段分组,则按所需时间段分组。
df %>%
group_by(Time) %>%
summarise(Transactions = n())
# Time Transactions
# <chr> <int>
# 1 09 1
# 2 10 5https://stackoverflow.com/questions/65646004
复制相似问题