【问题标题】:Count mean subgroup occurrence within subgroup计算子组内的平均子组出现次数
【发布时间】:2018-10-07 13:40:52
【问题描述】:

我有以下数据框:

date        hour_of_day    distance  weather_of_the_day
2017-06-13   6             10.32     1
2017-06-13   8             2.32      1
2017-06-14   10            4.21      2
2017-06-15   7             4.56      4
2017-06-15   7             8.92      4
2017-06-16   22            2.11      3


structure(list(startdat = structure(c(17272, 17272, 17272, 17272,17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272,17272, 17272, 17272, 17273, 17273, 17273, 17273), class = "Date"),    hOfDay = c(22L, 16L, 12L, 13L, 18L, 19L, 19L, 16L, 22L, 10L, 
10L, 16L, 11L, 20L, 9L, 15L, 18L, 12L, 16L, 18L), tripDKM = c(0.2, 
6.4, 3.4, 0.8, 2.4, 2.2, 2.2, 7.3, 2.6, 3.8, 7.5, 5.8, 3.7, 
2.1, 2.6, 5.2, 2.9, 1.7, 3.2, 3.1), totDMIN = c(1.85, 27.4, 
8.2, 4.21666666666667, 15.65, 8.91666666666667, 11.5666666666667, 
29.5166666666667, 7.01666666666667, 12.2166666666667, 15.8833333333333, 
19.5666666666667, 21.7166666666667, 8.66666666666667, 11.2333333333333, 
13.4, 7.58333333333333, 10.6166666666667, 6.76666666666667, 
17.7), weather_day = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("1", 
"2", "3", "4"), class = "factor")), row.names = c(1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L, 17L, 19L, 20L, 21L, 22L), class = "data.frame")

我的最终目标是制作一条线 ggplot,其中 x 轴显示小时数,y 轴代表平均出现次数。最终,这些线条应该代表 4 种天气条件。所以一条线应该代表weather_of_the_day = 1,y轴显示平均而言weather_day = 1出现hour_of_day = 6(例如)的频率,依此类推为7、8等。我想要什么,不仅是出现次数,而且是平均出现次数。

我已经为此苦苦挣扎了 2 天。我尝试了不同的方法,包括 for 循环和子分组。但他们都没有带来可用的解决方案。非常感谢您提前提供的帮助!

【问题讨论】:

  • 请在您的数据上使用dput() 并在此处发布
  • 请显示您想要的输出,这可能需要像 Paint、Paintbrush 或 Pinta 这样的图像软件。

标签: r for-loop ggplot2 grouping frequency


【解决方案1】:

您发布的数据集有点小,但这是我的建议。不过,只有更多的数据点才有意义。 df 是您发布的集合。

library(dplyr)
library(ggplot2)

df_plot <- df %>% 
  mutate(weather_of_the_day = factor(weather_of_the_day)) %>% 
  group_by(hour_of_day, weather_of_the_day) %>% 
  summarize(occurances = n())

 ggplot(data = df_plot, 
        aes(x = hour_of_day, 
            y = occurances, 
            group = weather_of_the_day, 
            color = weather_of_the_day)) +
  geom_line()+
  geom_point()

【讨论】:

    【解决方案2】:

    我不完全确定这是否符合您想要的输出,但我试了一下:

    #Importing packages
    library(dplyr)
    library(ggplot2)
    
    d <- structure(list(startdat = structure(c(17272, 17272, 17272, 17272,17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272,17272, 17272, 17272, 17273, 17273, 17273, 17273), 
                                             class = "Date"),
                        hOfDay = c(22L, 16L, 12L, 13L, 18L, 19L, 19L, 16L, 22L, 10L, 10L, 16L, 11L, 20L, 9L, 15L, 18L, 12L, 16L, 18L), 
                        tripDKM = c(0.2, 6.4, 3.4, 0.8, 2.4, 2.2, 2.2, 7.3, 2.6, 3.8, 7.5, 5.8, 3.7, 2.1, 2.6, 5.2, 2.9, 1.7, 3.2, 3.1), 
                        totDMIN = c(1.85, 27.4, 8.2, 4.21666666666667, 15.65, 8.91666666666667, 11.5666666666667, 29.5166666666667, 7.01666666666667, 12.2166666666667, 15.8833333333333, 19.5666666666667, 21.7166666666667, 8.66666666666667, 11.2333333333333, 13.4, 7.58333333333333, 10.6166666666667, 6.76666666666667, 17.7), 
                        weather_day = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), 
                                                .Label = c("1", "2", "3", "4"), 
                                                class = "factor")), 
                   row.names = c(1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L, 17L, 19L, 20L, 21L, 22L), 
                   class = "data.frame")
    
    #Count how often every weather_day occurs during every hOfDay
    plot_data <- d %>% 
      group_by(hOfDay, weather_day) %>% 
      summarize(n_occurences = n())
    
    #Create plot
    ggplot(plot_data, aes(x = hOfDay, y = n_occurences)) + 
      geom_line(aes(col = weather_day))
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2023-04-08
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多