总结x值相似但不相同的变量

问题描述 投票:0回答:1

我有一个包含三个重复的数据集。我想为每个X变量绘制平均Y变量。但是,我的x值并不相同。

这里是我的意思的一个小例子:

Time, Value, repeat_name
0,    5,     repeat1
0,    5,     repeat2
0,    5,     repeat3
3.1,  7,     repeat1
3.25, 8,     repeat2
3,    9,     repeat3
6.2,  5,     repeat1
6.5,  5,     repeat2
6,    5,     repeat3
9.3,  5,     repeat1
9.75, 5,     repeat2
9,    5,     repeat3

现在我希望可以以某种方式对时间进行分箱,然后将所有与分箱后的值匹配的值放入内部

所以我有以下垃圾箱:

Time
0-4 (values (5,5,5,7,8,9))
4-8 (values (5,5,5))
8-12 (values (5,5,5))

然后我可以取这些箱的平均值,并用ggplot绘制直方图。但是,我不确定如何实现目标。

我也不确定是否有更好的方法。

先谢谢您。

r ggplot2 dplyr
1个回答
0
投票

只是一些建议。装箱当然是挑战。您可以四舍五入,也可以创建切割。这完全取决于您要显示的内容。

我将显示两个选项,一个使用plyr::round_any,另一个使用cut

library(tidyverse)

foo <- read_csv("Time, Value, repeat_name
0,    5,     repeat1
0,    5,     repeat2
0,    5,     repeat3
3.1,  7,     repeat1
3.25, 8,     repeat2
3,    9,     repeat3
6.2,  5,     repeat1
6.5,  5,     repeat2
6,    5,     repeat3
9.3,  5,     repeat1
9.75, 5,     repeat2
9,    5,     repeat3")

foo %>% 
  mutate(time_round = plyr::round_any(Time, 5)) %>% 
  ggplot(aes(time_round, Value)) +
  stat_summary() +
  scale_y_continuous(limits =  c(0, NA))
#> No summary function supplied, defaulting to `mean_se()`

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS85REhBbE1yLnBuZyJ9” alt =“”>


foo %>% 
  mutate(time_cut = cut(Time, c(-Inf,4,8,Inf))) %>% 
  ggplot(aes(time_cut, Value)) +
  stat_summary() +
  scale_y_continuous(limits =  c(0, NA))
#> No summary function supplied, defaulting to `mean_se()`

“”

reprex package(v0.3.0)在2020-04-15创建

© www.soinside.com 2019 - 2024. All rights reserved.