使用dplyr根据特定因素和日期以及值汇总进行计算

时间:2019-04-24 19:43:09

标签: r dplyr apply summarize

我有一个数据框,其中包含离岸一定距离(DOS)的特定日期特定日期的不同船舶计数的数据,例如0-12nm和0-100nm-我想从0-100nm中减去0-12nm DOS内的飞船,以便我可以计算出例如每个日期“乘员”舰只在12至100海里。一旦完成,我想计算整个时间段内每个DOS内总共有多少乘客,货物等船舶……我可以找到一种非常费力的方法来完成此操作,但是我很确定变异和总结dplyr中的功能,有一种更有效的方式来运行此...

这是一个虚拟数据帧:

df<- structure(list(date = structure(c(17622, 17623, 17624, 17625, 
17626, 17627, 17622, 17623, 17624, 17625, 17626, 17627), class = "Date"), 
    `Passenger(6X)` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    `Cargo(7X)` = c(2, 0, 2, 2, 2, 3, 5, 4, 7, 6, 7, 4), `Tanker(8X)` = c(0, 
    0, 0, 0, 0, 0, 0, 3, 1, 0, 1, 0), Otherb = c(`5` = 0, `6` = 0, 
    `7` = 0, `8` = 0, `9` = 0, `10` = 0, `144` = 0, `154` = 0, 
    `164` = 0, `174` = 0, `184` = 0, `194` = 0), DOS = c("0-12nm", 
    "0-12nm", "0-12nm", "0-12nm", "0-12nm", "0-12nm", "0-100nm", 
    "0-100nm", "0-100nm", "0-100nm", "0-100nm", "0-100nm")), class = "data.frame", row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 1454L, 1455L, 1456L, 1457L, 1458L, 1459L))

在此示例中,2018年4月1日12-100nm的货船应为3-输出可能采用新列等形式....在我的真实数据集中,我实际上有4个不同的近海距离超过一年的约会....所以我认为dplyr是实现此目的的最佳方法-任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

选项1:

df %>% 
       group_by(date) %>% 
       summarise_at(
          vars(`Cargo(7X)`, `Tanker(8x)`), 
          funs(.[DOS == '0-100nm'] - .[DOS == '0-12nm'])
    )

#   date       `Cargo(7X)`  `Tanker(8x)`
# 1 2018-04-01           3            0
# 2 2018-04-02           4            3
# 3 2018-04-03           5            1
# 4 2018-04-04           4            0
# 5 2018-04-05           5            1
# 6 2018-04-06           1            0

选项2:

df %>% 
   group_by(date, DOS) %>% 
   summarise_at(vars(`Cargo(7X)`, `Tanker(8x)`), funs(sum)) %>% 
   gather(-(date:DOS), key = Ship, value = Value) %>%
   spread(key = DOS, value = Value) %>% 
   mutate('12-100nm' = `0-100nm`- `0-12nm`)

#    date       Ship       `0-100nm` `0-12nm` `12-100nm`
#  1 2018-04-01 Cargo(7X)          5        2          3
#  2 2018-04-01 Tanker(8X)         0        0          0
#  3 2018-04-02 Cargo(7X)          4        0          4
#  4 2018-04-02 Tanker(8X)         3        0          3
#  5 2018-04-03 Cargo(7X)          7        2          5
#  6 2018-04-03 Tanker(8X)         1        0          1
#  7 2018-04-04 Cargo(7X)          6        2          4
#  8 2018-04-04 Tanker(8X)         0        0          0
#  9 2018-04-05 Cargo(7X)          7        2          5
# 10 2018-04-05 Tanker(8X)         1        0          1
# 11 2018-04-06 Cargo(7X)          4        3          1
# 12 2018-04-06 Tanker(8X)         0        0          0

答案 1 :(得分:1)

如果我很了解您的问题,那么您应该可以使用dplyr来解决这个问题。 下例中的diff字段:

library(dplyr)
df %>%
  mutate(Total = `Passenger(6X)` + `Cargo(7X)` + `Tanker(8X)` + `Otherb`) %>%
  group_by(date) %>%
  mutate(diff = ifelse(row_number() == 1, Total, Total - lag(Total)))
   date       `Passenger(6X)` `Cargo(7X)` `Tanker(8X)` Otherb DOS     Total  diff
   <date>               <dbl>       <dbl>        <dbl>  <dbl> <chr>   <dbl> <dbl>
 1 2018-04-01               0           2            0      0 0-12nm      2     2
 2 2018-04-02               0           0            0      0 0-12nm      0     0
 3 2018-04-03               0           2            0      0 0-12nm      2     2
 4 2018-04-04               0           2            0      0 0-12nm      2     2
 5 2018-04-05               0           2            0      0 0-12nm      2     2
 6 2018-04-06               0           3            0      0 0-12nm      3     3
 7 2018-04-01               0           5            0      0 0-100nm     5     3
 8 2018-04-02               0           4            3      0 0-100nm     7     7
 9 2018-04-03               0           7            1      0 0-100nm     8     6
10 2018-04-04               0           6            0      0 0-100nm     6     4
11 2018-04-05               0           7            1      0 0-100nm     8     6
12 2018-04-06               0           4            0      0 0-100nm     4     1