简体   繁体   English

创建一个带中断的数字序列

[英]Create a sequence of numbers with breaks

I want to create a sequence of numbers like this:我想创建一个这样的数字序列:

X=22+1
Y=x+2
Z=x+3
A=x+4
B=X+5

1,2,X,3,4,Y,5,6,Z,7,8,A,10,11,B #and so on...
1,2,23,3,4,25,5,6,26,7,8,27,10,11,28  #and so on...

How do this with R?用 R 怎么做? there's a function to do this?有一个功能可以做到这一点?

We can do我们可以做的

unlist(Map(c, split(v1, as.integer(gl(length(v1), 2, 
      length(v1)))), c(X, Y, Z, A, B)), use.names = FALSE)
#[1]  1  2 23  3  4 25  5  6 26  7  8 27  9 10 28

data数据

v1 <- 1:10
X <- 23
Y <- X + 2
Z <- X + 3
A <- X + 4
B <- X + 5

You can create a duplicated record at specific position and replace them with another sequence.您可以在特定位置创建重复记录并用另一个序列替换它们。

seq1 <- 1:10
seq2 <- c(23, 25:28)
seq3 <- sort(c(seq1, seq(2, 10, 2)))
seq3[duplicated(seq3)] <- seq2
seq3
#[1]  1  2 23  3  4 25  5  6 26  7  8 27  9 10 28

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

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