r - R - 按日期绘制列中参数的移动平均值
问题描述
假设您有一个表,其中 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 包创建于 2021-05-16 (v2.0.0 )
推荐阅读
- python - Kivy + Python + Raspberry - 如何实现倒计时
- sql - 使用连接创建视图
- java - Java Rest web 服务中缺少子对象
- python-3.x - 尝试从 python 列表中附加并打印已删除的项目
- oracle - 我在这个过程中做错了什么
- android - android - 无法获取gps坐标
- sorting - 如何使用 sort 命令根据 unix 中的最后一列对文件进行排序?
- reactjs - API 请求返回页面标记
- javascript - 通过使用 JS 单击另一个按钮类型输入将值从输入复制到剪贴板
- kubernetes - Kubernetes健康检查工作进入谷歌云平台