R中的分组数据和汇总数据按日期排序

时间:2018-09-07 01:59:46

标签: r

我的父亲有不同的城市和州以及每周级别值(1&2)。

structure(list(city_id = c("IR130", "IR130", 
"IR130", "IR130", "IR130", "IR130", "IR130", "IR130", "IR130", 
"IR130", "IR130", "IR130", "IR130", "IR130", "IR130", "IR130", 
"IR130", "IR130", "IR130", "IR130", "IR130", "IR130", "IR130", 
"IR130", "IR130", "IR130", "IR130", "IR130", "IR130", "IR130", 
"IR130", "IR130", "IR130", "IR130", "IR130", "IR130", "IR130", 
"IR130", "IR130", "IR130", "IR130", "IR130", "IR130", "IR130", 
"IR130", "IR130"), state_id = c(333L, 333L, 333L, 
333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 
333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 
333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 
333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L, 333L), 
    weekly_date = structure(c(1517702400, 1527379200, 
    1509235200, 1523145600, 1524960000, 1531612800, 1526774400, 
    1520121600, 1510444800, 1532217600, 1530403200, 1514678400, 
    1529798400, 1512259200, 1506211200, 1508025600, 1513468800, 
    1516492800, 1522540800, 1506816000, 1505606400, 1521936000, 
    1518307200, 1531008000, 1523750400, 1512864000, 1529193600, 
    1511654400, 1511049600, 1515283200, 1517097600, 1519516800, 
    1525564800, 1521331200, 1508630400, 1518912000, 1505001600, 
    1526169600, 1509840000, 1507420800, 1514073600, 1528588800, 
    1520726400, 1515888000, 1524355200, 1527984000), class = c("POSIXct", 
    "POSIXt")), value_1 = c(0.183820871041667, 
    0.180725895208333, 0.35, 0.200932068125, 0.171669985625, 
    0.225808075208333, 0.198545789791666, 0.189855414375, 0.35, 
    0.277725830833333, 0.201674817708333, 0.35, 0.226923487291666, 
    0.35, 0.35, 0.35, 0.35, 0.35, 0.18633958, 0.35, 0.35, 0.188240067708333, 
    0.189944218125, 0.210746212708333, 0.183896871041667, 0.35, 
    0.211807967291666, 0.35, 0.35, 0.35, 0.35, 0.176269423125, 
    0.171773345833333, 0.185080020416667, 0.35, 0.181930044166667, 
    0.35, 0.184190593541666, 0.35, 0.35, 0.35, 0.215020983541666, 
    0.182612662916667, 0.35, 0.186923380833333, 0.211272482916666
    ), value_2 = c(0.160883679767442, 
    0.186424108837209, 0.35, 0.182523738139535, 0.227294089534884, 
    0.487345912857143, 0.167833364418605, 0.458898800697674, 
    0.35, 0.42808395877551, 0.254013390408163, 0.35, 0.25048365122449, 
    0.35, 0.35, 0.35, 0.35, 0.35, 0.350976408139535, 0.35, 0.35, 
    0.42162227, 0.356722947906977, 0.24491883755102, 0.234551652325581, 
    0.35, 0.283487755102041, 0.35, 0.35, 0.35, 0.35, 0.403158770697674, 
    0.273511963488372, 0.423378554186047, 0.35, 0.384183884418605, 
    0.35, 0.346561459069767, 0.35, 0.35, 0.35, 0.205802955306122, 
    0.422308468372093, 0.35, 0.20876661744186, 0.330639838372093
    )), class = c("data.table", "data.frame"), row.names = c(NA, 
-46L), .internal.selfref = <pointer: 0x000000000b7d1ef0>)

我尝试下面的代码按州,城市分组,然后按日期排序,然后汇总到value_1和value_2的月度数据,但是它不起作用

monthly <- dat   %>%
  group_by(state_id,city_id) %>%
  order_by(weekly_date)%>%
  tq_transmute(select  = c("value_1","value_2")
                           mutate_fun = apply.monthly,
                          FUN        = mean)

有人可以在R中帮助我吗 编辑:我的最终输出应按城市分组并按monthly_data排序

0 个答案:

没有答案