我正在学习r,我想基于每个因素的转数来建立直方图,具体取决于数据框中的日期。
这是我的数据框:
dat <- data.frame(
time = factor(c("Breakfast","Breakfast","Breakfast","Breakfast","Lunch","Lunch","Lunch","Lunch","Dinner","Dinner","Dinner","Dinner"), levels=c("Breakfast","Lunch","Dinner")),
date=c("2020-01-20","2020-01-21","2020-01-22","2020-01-23","2020-01-20","2020-01-21","2020-01-22","2020-01-23","2020-01-20","2020-01-21","2020-01-22","2020-01-23"),
total_bill = c(12.75,13.5,25.5,27.4,18.3,19.9,27.8,28.6,15.7,17.4,19.5,24.2)
)
[我的目标是找到例如:Breakfast
因子,我想像13.5 - 12.75
,25.5 - 13.5
,27.4 - 25.5
一样获得它的公转数,并且我想对Lunch
,Dinner
进行同样的处理,然后使用通过使用ggplot
在3个不同的图中绘制这些差值。
对此将提供任何帮助。谢谢!!!
我正在学习r,我想基于每个因素的转数来建立直方图,具体取决于数据框中的日期。这是我的数据帧:dat
我们通过指定cut
通过breaks
创建一个组>
library(dplyr)
dat %>%
mutate(grp = cut(total_bill, breaks = c(-Inf, 12.75, 13.5, 25.5, 27.4, Inf)))