R - 按日期绘制列中参数的移动平均值
R - Plot moving average of arguments in column by date
假设您有一个 table,其中 Result 可以在同一天取值 {a,b,c}。如何绘制 a、b 和 c 随时间变化的频率移动平均值?
示例:
Date Result
2018-11-23 a
2018-11-23 a
2018-11-23 b
2018-11-24 c
2018-11-24 b
2018-11-25 c
2018-11-25 c
2018-11-25 b
2018-11-26 c
2018-11-26 b
2018-11-26 a
...
我不确定你到底想达到什么目的。但是下面给出了每个日期每个字母的计数。
library(dplyr)
library(tidyr)
library(ggplot2)
x <- structure(list(date = structure(c(17858, 17858, 17858, 17859,
17859, 17860, 17860, 17860, 17861, 17861, 17861), class = "Date"),
letter = c("a", "a", "b", "c", "b", "c", "c", "b", "c", "b",
"a")), class = "data.frame", row.names = c(NA, -11L))
agg_x <- x %>%
group_by(date, letter) %>%
summarize(count = n()) %>%
ungroup()
agg_x %>%
spread(letter, count)
#> # A tibble: 4 x 4
#> date a b c
#> <date> <int> <int> <int>
#> 1 2018-11-23 2 1 NA
#> 2 2018-11-24 NA 1 1
#> 3 2018-11-25 NA 1 2
#> 4 2018-11-26 1 1 1
ggplot(data = agg_x, aes(x = date, y = count, color = letter)) +
geom_point() +
geom_line()
由 reprex package (v2.0.0)
于 2021-05-16 创建
假设您有一个 table,其中 Result 可以在同一天取值 {a,b,c}。如何绘制 a、b 和 c 随时间变化的频率移动平均值?
示例:
Date Result
2018-11-23 a
2018-11-23 a
2018-11-23 b
2018-11-24 c
2018-11-24 b
2018-11-25 c
2018-11-25 c
2018-11-25 b
2018-11-26 c
2018-11-26 b
2018-11-26 a
...
我不确定你到底想达到什么目的。但是下面给出了每个日期每个字母的计数。
library(dplyr)
library(tidyr)
library(ggplot2)
x <- structure(list(date = structure(c(17858, 17858, 17858, 17859,
17859, 17860, 17860, 17860, 17861, 17861, 17861), class = "Date"),
letter = c("a", "a", "b", "c", "b", "c", "c", "b", "c", "b",
"a")), class = "data.frame", row.names = c(NA, -11L))
agg_x <- x %>%
group_by(date, letter) %>%
summarize(count = n()) %>%
ungroup()
agg_x %>%
spread(letter, count)
#> # A tibble: 4 x 4
#> date a b c
#> <date> <int> <int> <int>
#> 1 2018-11-23 2 1 NA
#> 2 2018-11-24 NA 1 1
#> 3 2018-11-25 NA 1 2
#> 4 2018-11-26 1 1 1
ggplot(data = agg_x, aes(x = date, y = count, color = letter)) +
geom_point() +
geom_line()
由 reprex package (v2.0.0)
于 2021-05-16 创建