相对于数字的列序列拆分数据帧

时间:2018-10-22 14:11:58

标签: r

我想根据序列列将以下一个数据帧切掉

 sequence       Var1         Var2                Var3              Var4
    5    0.003838776  0.003687773         0.003838776       0.003838776
    4    0.002551022  0.001226242         0.002551022       0.002551022
    3   -0.011531198 -0.011090687        -0.011497312      -0.011518828
    2    0.012804272  0.011090687         0.012540644       0.012704180
    1    0.010126669  0.009756175         0.010041170       0.010092812
    0    0.010025147  0.009661911         0.009913382       0.009978835
    5              z            z                   z                 z
    4    0.002551022  0.001226242         0.002551022       0.002551022
    3   -0.011531198 -0.011090687        -0.011497312      -0.011518828
    2    0.012804272  0.011090687         0.012540644       0.012704180
    1    0.010126669  0.009756175         0.010041170       0.010092812
    0    0.010025147  0.009661911         0.009913382       0.009978835
    5              a            b                   c       0.003838776
    3              a  0.011090687        -0.011497312      -0.011518828
    2    0.012804272  0.011090687         0.012540644       0.012704180
    1    0.010126669  0.009756175         0.010041170       0.010092812
    0    0.010025147  0.009661911         0.009913382       0.009978835

我想从5到0的列顺序中包含多个数据框:

例如:

 sequence       Var1         Var2                Var3              Var4
        5    0.003838776  0.003687773         0.003838776       0.003838776
        4    0.002551022  0.001226242         0.002551022       0.002551022
        3   -0.011531198 -0.011090687        -0.011497312      -0.011518828
        2    0.012804272  0.011090687         0.012540644       0.012704180
        1    0.010126669  0.009756175         0.010041170       0.010092812
        0    0.010025147  0.009661911         0.009913382       0.009978835

将是第一个,

        5              z            z                   z                 z
        4    0.002551022  0.001226242         0.002551022       0.002551022
        3   -0.011531198 -0.011090687        -0.011497312      -0.011518828
        2    0.012804272  0.011090687         0.012540644       0.012704180
        1    0.010126669  0.009756175         0.010041170       0.010092812
        0    0.010025147  0.009661911         0.009913382       0.009978835

将是第二个,

    5              a            b                   c       0.003838776
    3              a  0.011090687        -0.011497312      -0.011518828
    2    0.012804272  0.011090687         0.012540644       0.012704180
    1    0.010126669  0.009756175         0.010041170       0.010092812
    0    0.010025147  0.009661911         0.009913382       0.009978835

将是第三个:

有时候缺少0到5之间的行,就像最后一个拆分的第4行丢失了一样。

1 个答案:

答案 0 :(得分:0)

如果不缺少序列0,您可以尝试:

library(zoo)
library(dplyr)
df %>% mutate(x=ifelse(sequence!=0,NA,row_number()),
              x=na.locf(x,fromLast=TRUE)) %>%
replyr::replyr_split("x")

#$`6`
#  sequence         Var1         Var2         Var3         Var4 x
#1        5  0.003838776  0.003687773  0.003838776  0.003838776 6
#2        4  0.002551022  0.001226242  0.002551022  0.002551022 6
#3        3 -0.011531198 -0.011090687 -0.011497312 -0.011518828 6
#4        2  0.012804272  0.011090687  0.012540644  0.012704180 6
#5        1  0.010126669  0.009756175  0.010041170  0.010092812 6
#6        0  0.010025147  0.009661911  0.009913382  0.009978835 6

#$`12`
#  sequence         Var1         Var2         Var3         Var4  x
#1        5            z            z            z            z 12
#2        4  0.002551022  0.001226242  0.002551022  0.002551022 12
#3        3 -0.011531198 -0.011090687 -0.011497312 -0.011518828 12
#4        2  0.012804272  0.011090687  0.012540644  0.012704180 12
#5        1  0.010126669  0.009756175  0.010041170  0.010092812 12
#6        0  0.010025147  0.009661911  0.009913382  0.009978835 12

#$`17`
#  sequence        Var1        Var2         Var3         Var4  x
#1        5           a           b            c  0.003838776 17
#2        3           a 0.011090687 -0.011497312 -0.011518828 17
#3        2 0.012804272 0.011090687  0.012540644  0.012704180 17
#4        1 0.010126669 0.009756175  0.010041170  0.010092812 17
#5        0 0.010025147 0.009661911  0.009913382  0.009978835 17