如何保持数字序列直到达到某个行号

时间:2022-05-06 21:26:57

I have been trying to assign numbers with sequence. I would like to further add to repeat the sequence until the certain row numbers reached. For example repeat the sequence for every 44th row.

我一直试图用序列分配数字。我想进一步添加重复序列,直到达到某些行号。例如,每隔44行重复一次序列。

Here is what I mean

这就是我的意思

test_table <- data.frame(col=rep(0:10,each=11),  row=c(rev(0:10)))

and assigning cumulative numbers in this way

并以这种方式分配累积数字

> library(dplyr)
 test_table%>%
 mutate(No=(row_number() - 1) %/% 11)

test_table  
        col row No
    1     0  10  0
    2     0   9  0
    3     0   8  0
    4     0   7  0
    5     0   6  0
    6     0   5  0
    7     0   4  0
    8     0   3  0
    9     0   2  0
    10    0   1  0
    11    0   0  0
    12    1  10  1
    13    1   9  1
    14    1   8  1
    15    1   7  1
    16    1   6  1
    17    1   5  1
    18    1   4  1
    19    1   3  1
    20    1   2  1
    21    1   1  1
    22    1   0  1
    23    2  10  2
    24    2   9  2
    25    2   8  2
    26    2   7  2
    27    2   6  2
    28    2   5  2
    29    2   4  2
    30    2   3  2
    31    2   2  2
    32    2   1  2
    33    2   0  2
    34    3  10  3
    35    3   9  3
    36    3   8  3
    37    3   7  3
    38    3   6  3
    39    3   5  3
    40    3   4  3
    41    3   3  3
    42    3   2  3
    43    3   1  3
    44    3   0  3
    45    4  10  4
    46    4   9  4
    47    4   8  4
    48    4   7  4
    49    4   6  4
    50    4   5  4
    51    4   4  4
    52    4   3  4
    53    4   2  4
    54    4   1  4
    55    4   0  4
    56    5  10  5
    57    5   9  5
    58    5   8  5
    59    5   7  5
    60    5   6  5
    61    5   5  5
    62    5   4  5
    63    5   3  5
    64    5   2  5
    65    5   1  5
    66    5   0  5
    67    6  10  6
    68    6   9  6
    69    6   8  6
    70    6   7  6
    71    6   6  6
    72    6   5  6
    73    6   4  6
    74    6   3  6
    75    6   2  6
    76    6   1  6
    77    6   0  6
    78    7  10  7
    79    7   9  7
    80    7   8  7
    81    7   7  7
    82    7   6  7
    83    7   5  7
    84    7   4  7
    85    7   3  7
    86    7   2  7
    87    7   1  7
    88    7   0  7
    89    8  10  8
    90    8   9  8
    91    8   8  8
    92    8   7  8
    93    8   6  8
    94    8   5  8
    95    8   4  8
    96    8   3  8
    97    8   2  8
    98    8   1  8
    99    8   0  8
    100   9  10  9
    101   9   9  9
    102   9   8  9
    103   9   7  9
    104   9   6  9
    105   9   5  9
    106   9   4  9
    107   9   3  9
    108   9   2  9
    109   9   1  9
    110   9   0  9
    111  10  10 10
    112  10   9 10
    113  10   8 10
    114  10   7 10
    115  10   6 10
    116  10   5 10
    117  10   4 10
    118  10   3 10
    119  10   2 10
    120  10   1 10
    121  10   0 10

Ok. Good! But I would like to keep the sequence for example 0 and 1 until the 44th row reached. After that, start to the new sequence from 2 and go 88th row like this.

好。好!但是我希望将序列保持为0和1,直到达到第44行。之后,从2开始到新序列并像这样进入第88行。

So the expected output will be

所以预期的产出将是

test_table  
      col row No
    1     0  10  0
    2     0   9  0
    3     0   8  0
    4     0   7  0
    5     0   6  0
    6     0   5  0
    7     0   4  0
    8     0   3  0
    9     0   2  0
    10    0   1  0
    11    0   0  0
    12    1  10  1
    13    1   9  1
    14    1   8  1
    15    1   7  1
    16    1   6  1
    17    1   5  1
    18    1   4  1
    19    1   3  1
    20    1   2  1
    21    1   1  1
    22    1   0  1
    23    2  10  0
    24    2   9  0
    25    2   8  0
    26    2   7  0
    27    2   6  0
    28    2   5  0
    29    2   4  0
    30    2   3  0
    31    2   2  0
    32    2   1  0
    33    2   0  0
    34    3  10  1
    35    3   9  1
    36    3   8  1
    37    3   7  1
    38    3   6  1
    39    3   5  1
    40    3   4  1
    41    3   3  1
    42    3   2  1
    43    3   1  1
    44    3   0  1
    45    4  10  2
    46    4   9  2
    47    4   8  2
    48    4   7  2
    49    4   6  2
    50    4   5  2
    51    4   4  2
    52    4   3  2
    53    4   2  2
    54    4   1  2
    55    4   0  2
    56    5  10  3
    57    5   9  3
    58    5   8  3
    59    5   7  3
    60    5   6  3
    61    5   5  3
    62    5   4  3
    63    5   3  3
    64    5   2  3
    65    5   1  3
    66    5   0  3
    67    6  10  2
    68    6   9  2
    69    6   8  2
    70    6   7  2
    71    6   6  2
    72    6   5  2
    73    6   4  2
    74    6   3  2
    75    6   2  2
    76    6   1  2
    77    6   0  2
    78    7  10  3
    79    7   9  3
    80    7   8  3
    81    7   7  3
    82    7   6  3
    83    7   5  3
    84    7   4  3
    85    7   3  3
    86    7   2  3
    87    7   1  3
    88    7   0  3
    89    8  10  4
    90    8   9  4
    91    8   8  4
    92    8   7  4
    93    8   6  4
    94    8   5  4
    95    8   4  4
    96    8   3  4
    97    8   2  4
    98    8   1  4
    99    8   0  4
    100   9  10  5
    101   9   9  5
    102   9   8  5
    103   9   7  5
    104   9   6  5
    105   9   5  5
    106   9   4  5
    107   9   3  5
    108   9   2  5
    109   9   1  5
    110   9   0  5
    111  10  10  4
    112  10   9  4
    113  10   8  4
    114  10   7  4
    115  10   6  4
    116  10   5  4
    117  10   4  4
    118  10   3  4
    119  10   2  4
    120  10   1  4
    121  10   0  4

How can we do that ? Thanks in advance!

我们怎么能这样做?提前致谢!

3 个解决方案

#1


1  

This would do it in more general way

这将以更一般的方式进行

num.seq = 11L            # total number of sequences in the first column
num.rows = N * num.seq   # total number of rows
seq.length.3 = 44        # length of the pattern in the 3rd column

# number of paterns in the 3rd column
num.seq.3 = ( num.rows - 1 )  %/% seq.length.3 +1   

# starting number in the sequence of the 3rd column
nseq=0

# vector for the 3rd column (could be done right in data frame def.)             
No = (rep(rep( nseq:(nseq+1), each = N, times= 2), times=num.seq.3) + 
      rep(0:(num.seq.3 -1)*2, each= seq.length.3)) [1:num.rows]

test_table <- data.frame(col=rep(0:10,each=11),  
                         row=c(rev(0:10)),
                         No=No)

An alternative way:

另一种方式:

library(dplyr)
dt2 <- test_table%>%
  mutate(No = (row_number() - 1) %/% 11) 
dt2$No <- dt2$No %% 2 + (rep(0:num.seq.3, each =44, times=num.seq.3 )*2) 
          [1:num.rows]

#2


1  

The arithmetic, which is totally dependent on row numbers, seems right this way.

算术完全依赖于行号,这种方式似乎是正确的。

test_table%>%
  mutate(No=((row_number() - 1) %/% 11) %% 2) %>% # alternating 11 rows of 0's and 1's 
  mutate(No = No + ((row_number() - 1) %/% 44) * 2) # add 2 every after 44 rows

Here is the result, as intended.

这是预期的结果。

structure(list(col = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L), row = c(10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, 
10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, 10L, 9L, 8L, 7L, 
6L, 5L, 4L, 3L, 2L, 1L, 0L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 
2L, 1L, 0L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, 10L, 
9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, 10L, 9L, 8L, 7L, 6L, 
5L, 4L, 3L, 2L, 1L, 0L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 
1L, 0L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, 10L, 9L, 
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, 10L, 9L, 8L, 7L, 6L, 5L, 
4L, 3L, 2L, 1L, 0L), No = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4)), class = "data.frame", .Names = c("col", "row", 
"No"), row.names = c(NA, -121L))

#3


0  

This would deliver what I understand to be the requested vector (except I think your sequencing"skipped a beat"):

这将提供我理解为请求的向量(除了我认为您的排序“跳过一个节拍”):

 c( rep( c(1,2,1,2), each=11) , rep(c(3,4,3,4), each=11), rep(c(5,6,5,6), each=11) )
  [1] 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 3 3 3
 [48] 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5
 [95] 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6

A more general way:

更通用的方式:

c( sapply( seq(1, 6, by=2), function(start) { 
                                    rep( rep(start:(start+1) , 2), each=11) }))

The outer c() will remove the matrix character that sapply defaults to.

外部c()将删除sapply默认的矩阵字符。

#1


1  

This would do it in more general way

这将以更一般的方式进行

num.seq = 11L            # total number of sequences in the first column
num.rows = N * num.seq   # total number of rows
seq.length.3 = 44        # length of the pattern in the 3rd column

# number of paterns in the 3rd column
num.seq.3 = ( num.rows - 1 )  %/% seq.length.3 +1   

# starting number in the sequence of the 3rd column
nseq=0

# vector for the 3rd column (could be done right in data frame def.)             
No = (rep(rep( nseq:(nseq+1), each = N, times= 2), times=num.seq.3) + 
      rep(0:(num.seq.3 -1)*2, each= seq.length.3)) [1:num.rows]

test_table <- data.frame(col=rep(0:10,each=11),  
                         row=c(rev(0:10)),
                         No=No)

An alternative way:

另一种方式:

library(dplyr)
dt2 <- test_table%>%
  mutate(No = (row_number() - 1) %/% 11) 
dt2$No <- dt2$No %% 2 + (rep(0:num.seq.3, each =44, times=num.seq.3 )*2) 
          [1:num.rows]

#2


1  

The arithmetic, which is totally dependent on row numbers, seems right this way.

算术完全依赖于行号,这种方式似乎是正确的。

test_table%>%
  mutate(No=((row_number() - 1) %/% 11) %% 2) %>% # alternating 11 rows of 0's and 1's 
  mutate(No = No + ((row_number() - 1) %/% 44) * 2) # add 2 every after 44 rows

Here is the result, as intended.

这是预期的结果。

structure(list(col = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,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row.names = c(NA, -121L))

#3


0  

This would deliver what I understand to be the requested vector (except I think your sequencing"skipped a beat"):

这将提供我理解为请求的向量(除了我认为您的排序“跳过一个节拍”):

 c( rep( c(1,2,1,2), each=11) , rep(c(3,4,3,4), each=11), rep(c(5,6,5,6), each=11) )
  [1] 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 3 3 3
 [48] 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5
 [95] 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6

A more general way:

更通用的方式:

c( sapply( seq(1, 6, by=2), function(start) { 
                                    rep( rep(start:(start+1) , 2), each=11) }))

The outer c() will remove the matrix character that sapply defaults to.

外部c()将删除sapply默认的矩阵字符。