How to order the ties in data with previously observed value appearing first with multiple sorting column

不打扰是莪最后的温柔 提交于 2019-12-13 10:29:26

问题


Disclaimer: Please note that this is extension, not a duplicate, to this topic: How to order the ties in data so that the previously observed value appears first. The difference is that now I don't have one, but many sorting column.

I need to sort attached data by min, then by sec, then by timestamp. Additionally, if there are any ties in order I would like to order those ties so that the same values of subgroup would be adjacent, i.e if two observations hava the same min, sec and timestamp, I would like to have as first this observation, that has the same subgroup as the value from previous min, sec,timestamp combination.

@Moody_Mudskipper provided excellent idea in the linked topic, however I don't know if it is applicable to my extended case. I tried to split based on all sorting variables, i.e. split(subgroup, list(min, sec, timestamp) but as my data is pretty large and I create all combinations of min, sec, timestamp it makes impossible to process that by my computer. So my question is - how can I tweak that solution? Is there any alternative?

structure(list(group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2), subgroup = c("C", "L", "L", "L", "L", "L", 
"C", "L", "C", "C", "C", "C", "C", "C", "C", "L", "C", "C", "L", 
"L", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U", 
"B", "U", "B", "B", "U", "U", "U", "U", "U", "U", "U", "U", "U", 
"U", "B", "U", "U", "B", "U", "U", "B", "B", "U", "U", "U", "B", 
"B", "B"), A = c(32, 32, 0, 0, 0, 0, 55, 2, 0, 0, 0, 0, 0, 0, 
0, 61, 0, 50, 7, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 
61, 49, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 3, 0, 12, 0, 0, 
49, 0, 49, 0, 0, 49, 0, 0), B = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 
1L, 0L, 1L, 1L, 1L), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L), sec = c(0L, 
0L, 1L, 2L, 6L, 11L, 13L, 13L, 33L, 36L, 39L, 42L, 43L, 44L, 
46L, 47L, 48L, 51L, 51L, 52L, 13L, 18L, 22L, 27L, 31L, 32L, 32L, 
33L, 35L, 37L, 38L, 39L, 40L, 41L, 43L, 43L, 46L, 46L, 47L, 49L, 
49L, 52L, 57L, 58L, 0L, 4L, 6L, 6L, 7L, 8L, 11L, 12L, 13L, 14L, 
17L, 20L, 23L, 27L, 43L, 52L), timestamp = structure(c(1515945641.69, 
1515945641.69, 1515945642.273, 1515945643.69, 1515945647.69, 
1515945652.202, 1515945654.354, 1515945654.354, 1515945674.224, 
1515945677.592, 1515945680.129, 1515945683.176, 1515945684.514, 
1515945685.921, 1515945687.289, 1515945689.66, 1515945689.553, 
1515945692.633, 1515945692.643, 1515945694.34, 1525465421.403, 
1525465426.1, 1525465429.586, 1525465435.347, 1525465438.739, 
1525465439.499, 1525465440.315, 1525465441.211, 1525465443.314, 
1525465444.754, 1525465385.252, 1525465386.252, 1525465387.252, 
1525465388.252, 1525465451.143, 1525465451.342, 1525465453.603, 
1525465453.763, 1525465454.865, 1525465457.363, 1525465936.564, 
1525465940.29, 1525465944.562, 1525465946.26, 1525465947.762, 
1525465952.283, 1525465954.87, 1525465954.97, 1525465954.939, 
1525465956.282, 1525465958.77, 1525465959.506, 1525465960.404, 
1525465962.74, 1525465964.699, 1525465968.194, 1525465971.1, 
1525465975.106, 1525465991.138, 1525466000.25), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), .Names = c("group", "subgroup", "A", 
"B", "min", "sec", "timestamp"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -60L))

Desired order should be:

c(1, 2, 3, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60)

回答1:


You can use the same solution, but first define a columns to identify your groups identified by all grouping variables. I used dplyr::group_indices for that.

library(tidyverse)
df2 <- df %>%
  mutate(group_ind = group_indices(.,group,min, sec, timestamp)) %>%
  group_by(group) %>%
  mutate(
    order = map2(
    split_ <- split(subgroup,group_ind),
    accumulate(split_, ~intersect(c(rev(.x),.y),.y)),
    match) %>% unlist) %>%
  arrange(group,group_ind,order) %>%
  ungroup %>%
  select(-order, - group_ind)


df3 <-df[c(1, 2, 3, 4, 5, 6, 8, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 
     34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
     50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60),]


identical(df2,df3)
# TRUE


来源:https://stackoverflow.com/questions/53022075/how-to-order-the-ties-in-data-with-previously-observed-value-appearing-first-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!