ZeekDSA ZeekDSA - 19 days ago 8
R Question

in R - repeatation of numbers in two column - unable to get the exact result

I wanted to repeat certain numbers and I am using the following code.

list1 <- 7101:7118
list2 <- rep(c(1,2,3,4,21,21,23,24,25,26,27,28,45,46,47,48), length(list1))
list3 <- cbind(list1, list2)


Unfortunately, I am not getting the result like the following what I want:

7101 1
7101 2
7101 3
7101 4
7101 21
7101 22
7101 23
7101 24
7101 25
7101 26
7101 27
7101 28
7101 45
7101 46
7101 47
7101 48
7102 1
7102 2
7102 3
7102 4
7102 21
7102 22
7102 23
7102 24
7102 25
7102 26
7102 27
7102 28
7102 45
7102 46
7102 47
7102 48
7103 1
7103 2
7103 3
7103 4


please let me know where I did wrong and how can i solve it?

Answer

You should use each

list2 <- rep(c(1,2,3,4,21,21,23,24,25,26,27,28,45,46,47,48), each=length(list1))
Comments