简体   繁体   English

拆分数据并根据拆分编号重新排列值

[英]split data and re-arrange values based on split number

I have this data:我有这个数据:

      row col
 [1,]   1   1
 [2,]   7   1
 [3,]   2   2
 [4,]   7   2
 [5,]  18   2
 [6,]   3   3
 [7,]   4   4
 [8,]   5   5
 [9,]  19   5
[10,]   6   6
[11,]   1   7
[12,]   2   7
[13,]   7   7
[14,]  18   7
[15,]   8   8
[16,]   9   9
[17,]  10  10
[18,]  11  11
[19,]  12  12
[20,]  13  13
[21,]  18  13
[22,]  14  14
[23,]  15  15
[24,]  16  16
[25,]  17  17
[26,]   2  18
[27,]   7  18
[28,]  13  18
[29,]  18  18
[30,]   5  19
[31,]  19  19
[32,]  20  20

I would like to split it based on some condition on the ordering.我想根据订购的某些条件对其进行拆分。 I can split it using:我可以使用以下方法拆分它:

split(m1[, 'row'], m1[, 'col'])

Which gives me this output:这给了我这个 output:

$`1`
[1] 1 7

$`2`
[1]  2  7 18

$`3`
[1] 3

$`4`
[1] 4

$`5`
[1]  5 19

$`6`
[1] 6

$`7`
[1]  1  2  7 18

$`8`
[1] 8

$`9`
[1] 9

$`10`
[1] 10

$`11`
[1] 11

$`12`
[1] 12

$`13`
[1] 13 18

$`14`
[1] 14

$`15`
[1] 15

$`16`
[1] 16

$`17`
[1] 17

$`18`
[1]  2  7 13 18

$`19`
[1]  5 19

$`20`
[1] 20

However I would like to keep some ordering.但是,我想保留一些订单。 Splits 1 - 6 are correct since the first value in split 1 is 1 (the second being 7 ).拆分1 - 6是正确的,因为拆分1中的第一个值是1 (第二个是7 )。 The first value in split 2 is 2 (the second is 7 and third is 18 ). split 2中的第一个值是2 (第二个是7 ,第三个是18 )。 The pattern continues until it breaks on split 7 .该模式一直持续到它在拆分7处中断。 I would like split 7 to look like:我希望拆分7看起来像:

 $`7`
[1]  7  1  2  18

Split 8 to 17 are all fine also.拆分817也都很好。 Since the first number corresponds to the split number.由于第一个数字对应于拆分数字。 I woulld like split 18 and 19 to look like:我希望拆分1819看起来像:

$`18`
[1]  18  2  7 13

$`19`
[1]  19  5

How can I split the data using this struture?如何使用这种结构拆分数据?

Data:数据:

m1 <- structure(c(1L, 7L, 2L, 7L, 18L, 3L, 4L, 5L, 19L, 6L, 1L, 2L, 
7L, 18L, 8L, 9L, 10L, 11L, 12L, 13L, 18L, 14L, 15L, 16L, 17L, 
2L, 7L, 13L, 18L, 5L, 19L, 20L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 5L, 
5L, 6L, 7L, 7L, 7L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 13L, 14L, 
15L, 16L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 20L), .Dim = c(32L, 
2L), .Dimnames = list(NULL, c("row", "col")))

Starting from your split , you can:从您的split开始,您可以:

x<-split(m1[, 'row'], m1[, 'col'])
Map(function(a,b) b[order(match(b,a))], as.integer(names(x)), x)

An idea is to split it as data frame only on col so we have them as data frame elements.一个想法是仅在col上将其拆分为数据框,因此我们将它们作为数据框元素。 Using row , we can find the number in col that is the same as row , put that first and follow the others, ie使用row ,我们可以在col中找到与row相同的数字,将其放在第一位并跟随其他数字,即

lapply(split(data.frame(m1), m1[, 'col']), function(i) {
                                        i1 <- which(i$col == i$row); 
                                        i$row[c(i1, seq(nrow(i))[-i1])]})

Checking the order in the ones that failed,检查失败的订单,

lapply(split(data.frame(m1), m1[, 'col']), function(i) {i1 <- which(i$col == i$row); i$row[c(i1, seq(nrow(i))[-i1])]})[7]
#$`7`
#[1]  7  1  2 18

lapply(split(data.frame(m1), m1[, 'col']), function(i) {i1 <- which(i$col == i$row); i$row[c(i1, seq(nrow(i))[-i1])]})[18]
#$`18`
#[1] 18  2  7 13

lapply(split(data.frame(m1), m1[, 'col']), function(i) {i1 <- which(i$col == i$row); i$row[c(i1, seq(nrow(i))[-i1])]})[19]
#$`19`
#[1] 19  5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM