嗨,我是一个R的初学者,所以不知道多少功能去执行这个操作,即使在我的头脑中,我知道该做什么,只是不知道怎么做。
所以我有骑行时长的数据,我想总结平日和周末的数据,并与年度会员和临时会员进行比较。我已经使用wday()将日期转换为“1”到“7”。现在我想过滤掉'2‘到'6’(工作日),并对ride_lenth求和,过滤出'1‘和'7’(周末),并对该ride_length求和,然后使用aggregate()将它们与临时和年度成员使用量进行比较。这就是我的决定。
member_type ride_length date month day year day_of_week weekday_num
casual 5280 2020-07-01 Jul 01 2020 Wednesday 4
casual 9840 2020-07-01 Jul 01 2020 Wednesday 4任何其他实现这一目标的途径也是受欢迎的。
发布于 2021-08-09 16:14:40
不幸的是,由于缺少输入和所需的输出,我无法测试代码。但是你应该能够让这些代码行得通:
library(dplyr)
# your data.frame/tibble
df %>%
# create variable to indicate weekend or not (check the weekend day names)
dplyr::mutate(day_type = ifelse(day_of_week %in% c("Saturday", "Sunday"), "WEEKEND","WEEK")) %>%
# build gouping by member type and day type
dplyr::group_by(member_type, day_type) %>%
# summarise total ride length
dplyr::summarize(total_ride_length = sum(ride_length, na.rm = TRUE))仅仅作为一个建议:也许你应该考虑一些假日,因为它们可能是在工作日,但表现出周末的行为(由于大多数人有空闲时间租车和骑自行车,或者如果人们主要是租车上下班,则相反)
https://stackoverflow.com/questions/68715243
复制相似问题