计算R

时间:2018-11-23 15:44:57

标签: 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"))

我希望计算每年十分位数1、5和10 BY的平均结果(2016、17等)。然后,我希望在第一列中创建一个最终年份表,详细说明年份,然后是十分位数1和10的平均结果之间的差距(即十分位数10结果减去十分位数1的结果),然后是十分位数5和10的平均结果之间的梯度。 10(即10个平均结果减去5个平均结果) 这是十分位5和十分位10在均值上的差异。

为了说明这一点,我为2016年的数据创建了一个工作示例。我列出了2016年的十分位1、5和10的值。然后使用这些值计算出间隙和梯度差。

summary2016 <- structure(list(`2016` = c(NA_character_, NA_character_, NA_character_, 
NA_character_), `1` = c("5", "10", "Gap", "Gradient"), `5.5` = c(5.1, 
4.5, 1.4, 0.3), `6` = c(5.3, 5.6, NA, NA), `11.5` = c(10.4, 10.1, 
NA, NA)), .Names = c("2016", "1", "5.5", "6", "11.5"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -4L), spec = structure(list(
    cols = structure(list(`2016` = structure(list(), class = c("collector_character", 
    "collector")), `1` = structure(list(), class = c("collector_character", 
    "collector")), `5.5` = structure(list(), class = c("collector_double", 
    "collector")), `6` = structure(list(), class = c("collector_double", 
    "collector")), `11.5` = structure(list(), class = c("collector_double", 
    "collector"))), .Names = c("2016", "1", "5.5", "6", "11.5"
    )), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

这可以一步完成吗,还是我需要分解一下?

1 个答案:

答案 0 :(得分:1)

library(tidyverse)
df1 %>% filter(decile %in% c(1, 5, 10)) %>% 
  group_by(time, decile) %>% summarise(mean = mean(amount)) %>% 
  mutate(gap1 = mean - mean[1], gap5 = mean - mean[2])

# A tibble: 9 x 5
# Groups:   time [3]
#    time decile  mean   gap1   gap5
#   <int>  <int> <dbl>  <dbl>  <dbl>
# 1  2016      1  5.75  0      0.55 
# 2  2016      5  5.20 -0.55   0    
# 3  2016     10  5.05 -0.7   -0.150
# 4  2017      1  6.4   0      0.775
# 5  2017      5  5.62 -0.775  0    
# 6  2017     10  6.15 -0.25   0.525
# 7  2018      1  7.33  0      1.90 
# 8  2018      5  5.43 -1.90   0    
# 9  2018     10  2.60 -4.73  -2.83 

数字与您的数字不同,所以也许您正在寻找其他类型的差距。您的示例summary2016的结构也有些不同寻常,而上面的解决方案所产生的效果超出了您的要求,但格式更好。

特别是,gap1平均值(十分位数i)-均值(十分位数1),其中i = 1、5、10,而gap5均值(十分位数i)-均值(十分位数5)