【问题标题】:Changing the order of days更改日期顺序
【发布时间】:2023-01-23 17:39:08
【问题描述】:

我无法按时间顺序显示我一周中的几天,并且在可视化数据时,它看起来不正确。

我尝试使用 ordered 语句,但显示错误,请帮助

这是我的代码 -

avg_ride_day <- cyclistic_data %>%
  filter(bike_type != "docked_bike") %>%
  group_by(user_type, day_of_week) %>%
  summarise(avg_time_min = mean(ride_length)/60, median_time = median(ride_length)/60, 
            max_time = max(ride_length)/60, min_time = min(ride_length)/60) %>%
  arrange(day_of_week)

这就是它的显示方式 - enter image description here

【问题讨论】:

  • ''day_of_week' 是字符类型,在输出中按字母顺序排序。如果您希望排序为 mo-tue-wed- 等,您可以在调用 'arrange' 之前将 'day_of_week' 转换为排序因子
  • 把它作为一个因素并提供水平,factor(day_of_week, levels= c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"))然后安排(如果你想让这些日子从星期一开始,把“Sun”移到最后)

标签: r dataframe sequence dayofweek


【解决方案1】:
expl <- data.frame(days = c("tue", "mon", "wed"))

library(dplyr)

expl %>% 
  mutate(days = factor(days, levels = c("mon", "tue", "wed"))) %>% 
  arrange(days)

回报:

  days
1  mon
2  tue
3  wed

【讨论】:

    猜你喜欢
    • 2022-07-22
    • 2018-03-12
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多