用两个变量计算和的除法,一个月的差异

时间:2018-05-08 15:01:29

标签: r

我想计算两个变量之和的除法,它们有一个月的差异。

跟随一个想法的图像:

enter image description here

所以它会是这样的:MEASURE = SUM(B-BEING MONTH = FEBRUARY)/ SUM(A-BEING MONTH = 1月)

有人可以帮我吗?

这里是R synthax中的数据样本:

structure(list(Date = structure(c(1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 1427770800, 
1427770800, 1427770800, 1427770800, 1425092400, 1425092400, 1425092400, 
1425092400), class = c("POSIXct", "POSIXt"), tzone = ""), B = c(885.47, 
932.29, 1147.56, 839.18, 143.5, 308.91, 129.35, 393.27, 747.68, 
758.03, 683.24, 734.25, 333.65, 456.04, 54.47, 549.96, 476.54, 
802.85, 33.3, 549.58, 941.29, 1270.92, 745.64, 924.08, 297.29, 
569.52, 263.01, 843.08, 229.77, 847.28, 108.05, 998.12, 354.05, 
843.32, 389.3, 1181.88, 432.65, 67.87, 1144.33, 1103.32, 728.96, 
359.21, 656.1, 732.25, 303.79, 1123.2, 332.43, 165.81, 531.95, 
547.24, 626.07, 450.87, 505, 195.59, 375.29, 422.92, 227.93, 
1439.66, 251.98, 965.9, 324.33, 2471.1, 338.03, 1006.5, 919.09, 
1104.9, 634.14, 619.69, 658.4, 292.41, 135.23, 653.88, 439.43, 
200.86, 502.94, 476.11, 0.09, 55.26, 362.23, 770.03, 843.99, 
1019.97, 12.61, 778.81, 1069.12, 573.75, 498.88, 112.68, 519.09, 
2092.89, 744.16, 691.79, 1137.73, 759.54, 133.37, 863.36, 1785.42, 
494.15, 787.58, 1150.87), C = structure(c(4L, 8L, 5L, 8L, 1L, 
1L, 1L, 7L, 8L, 1L, 9L, 8L, 1L, 2L, 1L, 8L, 1L, 1L, 1L, 8L, 2L, 
8L, 1L, 8L, 1L, 1L, 1L, 4L, 8L, 1L, 8L, 7L, 1L, 8L, 8L, 1L, 8L, 
8L, 9L, 8L, 9L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 5L, 8L, 8L, 8L, 
1L, 8L, 8L, 1L, 9L, 6L, 1L, 8L, 8L, 2L, 1L, 5L, 1L, 8L, 1L, 3L, 
1L, 2L, 1L, 8L, 1L, 1L, 1L, 1L, 1L, 2L, 9L, 8L, 8L, 1L, 8L, 8L, 
1L, 1L, 1L, 6L, 3L, 1L, 2L, 8L, 9L, 1L, 8L, 6L, 10L, 1L, 10L), .Label = c("A 000", 
"B 1-30", "C 31-60", "D 61-90", "E 91-120", "F 121-150", "G 151-180", 
"H 181-360", "I 361-390", "J >390"), class = "factor"), A = c(796.92, 
0, 803.29, 0, 139.2, 299.64, 125.47, 117.98, 0, 735.29, 0, 0, 
323.64, 442.36, 52.84, 0, 462.24, 778.76, 32.3, 0, 913.05, 0, 
723.27, 0, 288.37, 552.43, 255.12, 758.77, 0, 821.86, 0, 299.44, 
343.43, 0, 0, 1146.42, 0, 0, 0, 0, 0, 348.43, 636.42, 710.28, 
294.68, 1089.5, 322.46, 160.84, 515.99, 383.07, 0, 0, 0, 189.72, 
0, 0, 221.09, 0, 125.99, 936.92, 0, 0, 327.89, 976.31, 643.36, 
1071.75, 0, 601.1, 638.65, 283.64, 131.17, 634.26, 0, 194.83, 
487.85, 461.83, 0.09, 53.6, 351.36, 0, 0, 0, 12.23, 0, 0, 556.54, 
483.91, 109.3, 259.55, 2030.1, 721.84, 671.04, 0, 0, 129.37, 
0, 892.71, 0, 763.95, 0)), .Names = c("Date", "B", "C", "A"), row.names = c(NA, 
100L), class = "data.frame")

谢谢!

1 个答案:

答案 0 :(得分:0)

调用您的数据dat,这应该可以扩展到额外的几个月:

library(dplyr)
dat %>% group_by(Date) %>%
    summarize(A = sum(A), B = sum(B)) %>%
    arrange(Date) %>%
    mutate(prev_date = lag(Date)) %>%
    inner_join(x = select(., -prev_date), y = select(., -Date), by = c("Date" = "prev_date")) %>%
  mutate(result = B.y / A.x) %>%
  select(Date, result)
# # A tibble: 1 x 2
#                  Date   result
#                <dttm>    <dbl>
# 1 2015-02-27 22:00:00 35.77827