使用if语句

时间:2018-11-28 13:57:15

标签: r

对于示例数据框:

df1 <- structure(list(name = c("a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
"v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
"v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
"v", "w", "x", "y", "z"), amount = c(5.5, 5.4, 5.2, 5.3, 5.1, 
5.1, 5, 5, 4.9, 4.5, 6, 5.9, 5.7, 5.4, 5.3, 5.1, 5.6, 5.4, 5.3, 
5.6, 4.6, 4.2, 4.5, 4.2, 4, 3.8, 6, 5.8, 5.7, 5.6, 5.3, 5.6, 
5.4, 5.5, 5.4, 5.1, 9, 8.8, 8.6, 8.4, 8.2, 8, 7.8, 7.6, 7.4, 
7.2, 6, 5.75, 5.5, 5.25, 5, 4.75, 10, 8.9, 7.8, 6.7, 5.6, 4.5, 
3.4, 2.3, 1.2, 0.1, 6, 5.8, 5.7, 5.6, 5.5, 5.5, 5.4, 5.6, 5.8, 
5.1, 6, 5.5, 5.4, 5.3, 5.2, 5.1), decile = c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L), time = c(2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L)), .Names = c("name", "amount", 
"decile", "time"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-78L), spec = structure(list(cols = structure(list(name = structure(list(), class = c("collector_character", 
"collector")), amount = structure(list(), class = c("collector_double", 
"collector")), decile = structure(list(), class = c("collector_integer", 
"collector")), time = structure(list(), class = c("collector_integer", 
"collector"))), .Names = c("name", "amount", "decile", "time"
)), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

我最终希望生成一个ggplot图表,其中按五分位数详细说明每年的平均“金额”(即,每年数据的5个小条形图)。

要实现这一点,我需要能够计算五分位数(将十分位1和2、3和4、5和6、7和8以及9和10中的所有值取平均值,并且还包括95%CI

我过去曾尝试过滤我的数据,但是我正在努力地使用if语句来对此概念化。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

您可以使用dplyr函数通过管道执行此操作,通过将除以2并四舍五入将十分位数转换为五分位数。在这里,我只是做了一个非常快而肮脏的置信区间,即2 x标准偏差,但是您可能需要其他方法。

library(dplyr)
library(ggplot2)

plot_data <- df1 %>% 
  mutate(quintile = ceiling(decile/2)) %>% 
  group_by(time, quintile) %>% 
  summarize(average_amount = mean(amount),
            sd_amount = sd(amount),
            ci_min = average_amount - 2 * sd_amount,
            ci_max = average_amount + 2 * sd_amount)

这是一个(丑陋的)ggplot,其中有按年和五分位数划分的条形图。

ggplot(plot_data, aes(x = quintile, y = average_amount)) + 
  geom_col() + 
  geom_errorbar(aes(ymin = ci_min, ymax = ci_max)) +
  facet_wrap(~ time)

enter image description here

答案 1 :(得分:1)

如果您只是在寻找平均值,请尝试以下操作:

library(tidyverse)

df1 %>% 
  mutate(quintile = floor((decile - 1) / 2) + 1) %>% 
  group_by(time, quintile) %>% 
  summarise(AvgAmount = mean(amount)) %>% 
  ggplot(aes(quintile, AvgAmount)) + 
  geom_bar(stat = "identity") + 
  facet_grid(time ~ .)

my plot

如果您想更好地了解五分位数内的分布,我们可以使用箱形图:

df1 %>% 
  mutate(quintile = floor((decile - 1) / 2) + 1) %>% 
  ggplot(aes(quintile, amount, group = quintile)) + 
  geom_boxplot() + 
  facet_grid(time ~ .)

box plot