Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

permutation - rearrange a vector with specific skip pattern in r

I have a result from permutation as below;

abc <- gtools::permutations(4,3,repeats.allowed = T)

I am trying to rearrange this response so that the vector abc[,1] remains as it is but vector abc[,2] and abc[,3] should be as follows;

abc[,2] should be rearranged to 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4

abc[,3] should be rearranged to 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is highly manual, so not sure if you are looking for a more automated sorting approch:

abc[,2] <- rep(rep(1:4, each = 2), length.out = nrow(abc))
abc[,3] <- rep(c(rep(1:2, 2), rep(3:4, 2)), length.out = nrow(abc))

gives:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...