How to create a replicated list of a list in R?

R ProgrammingServer Side ProgrammingProgramming

Sometimes we want to created repeated values, this is helpful in different scenarios such as measuring an effect of a constant on multiple variables. The list values can be also replicated for similar purpose of analysis. The replication of list of a list can be created by using rep function. For example, if we have a list called x and we want to create five times replicated list of this list then we can use the code rep(list(x),5).

Example1

Live Demo

> List1<-list(x1=1:50,x2=letters[1:20])
> List1

Output

$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

Example

> rep(list(List1),5)

Output

[[1]]
[[1]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[1]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[2]]
[[2]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[2]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[3]]
[[3]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[3]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[4]]
[[4]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[4]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[5]]
[[5]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[5]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

Example

> rep(list(List1),10)

Output

[[1]]
[[1]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[1]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[2]]
[[2]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[2]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[3]]
[[3]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[3]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[4]]
[[4]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[4]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[5]]
[[5]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[5]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[6]]
[[6]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[6]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[7]]
[[7]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[7]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[8]]
[[8]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[8]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[9]]
[[9]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[9]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

[[10]]
[[10]]$x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[[10]]$x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"

Example2

Live Demo

> List2<-list(y1=rnorm(20,5,2.4),y2=rpois(20,3),y3=rexp(20,1.4),y4=runif(20,2,5))
> List2

Output

$y1
[1] 3.079458 3.589123 3.902639 5.963523 5.244280 3.549689 6.566025 6.325017
[9] 5.332337 1.640506 5.265804 3.886971 3.203786 2.532067 4.734062 2.680252
[17] 4.223528 9.250713 5.815792 4.531292

$y2
[1] 1 3 2 6 3 3 4 4 3 2 4 3 1 4 2 1 8 5 3 4

$y3
[1] 0.96467045 0.82096525 0.36900807 0.65093944 0.08277442 0.44907291
[7] 0.05562078 1.60547741 1.31813850 0.40487347 0.53284797 0.47320076
[13] 0.41547343 0.01057435 1.11501319 0.31536054 0.28896671 0.51575956
[19] 0.32843798 0.08961490

$y4
[1] 3.851959 2.828836 3.249421 2.202290 3.962181 3.891896 4.700026 4.652121
[9] 3.936827 4.678050 3.325526 4.656404 4.971151 2.315932 3.841990 3.823890
[17] 3.140251 2.831781 2.375118 2.396929

Example

> rep(list(List2),4)

Output

[[1]]
[[1]]$y1
[1] 3.079458 3.589123 3.902639 5.963523 5.244280 3.549689 6.566025 6.325017
[9] 5.332337 1.640506 5.265804 3.886971 3.203786 2.532067 4.734062 2.680252
[17] 4.223528 9.250713 5.815792 4.531292

[[1]]$y2
[1] 1 3 2 6 3 3 4 4 3 2 4 3 1 4 2 1 8 5 3 4

[[1]]$y3
[1] 0.96467045 0.82096525 0.36900807 0.65093944 0.08277442 0.44907291
[7] 0.05562078 1.60547741 1.31813850 0.40487347 0.53284797 0.47320076
[13] 0.41547343 0.01057435 1.11501319 0.31536054 0.28896671 0.51575956
[19] 0.32843798 0.08961490

[[1]]$y4
[1] 3.851959 2.828836 3.249421 2.202290 3.962181 3.891896 4.700026 4.652121
[9] 3.936827 4.678050 3.325526 4.656404 4.971151 2.315932 3.841990 3.823890
[17] 3.140251 2.831781 2.375118 2.396929

[[2]]
[[2]]$y1
[1] 3.079458 3.589123 3.902639 5.963523 5.244280 3.549689 6.566025 6.325017
[9] 5.332337 1.640506 5.265804 3.886971 3.203786 2.532067 4.734062 2.680252
[17] 4.223528 9.250713 5.815792 4.531292

[[2]]$y2
[1] 1 3 2 6 3 3 4 4 3 2 4 3 1 4 2 1 8 5 3 4

[[2]]$y3
[1] 0.96467045 0.82096525 0.36900807 0.65093944 0.08277442 0.44907291
[7] 0.05562078 1.60547741 1.31813850 0.40487347 0.53284797 0.47320076
[13] 0.41547343 0.01057435 1.11501319 0.31536054 0.28896671 0.51575956
[19] 0.32843798 0.08961490

[[2]]$y4
[1] 3.851959 2.828836 3.249421 2.202290 3.962181 3.891896 4.700026 4.652121
[9] 3.936827 4.678050 3.325526 4.656404 4.971151 2.315932 3.841990 3.823890
[17] 3.140251 2.831781 2.375118 2.396929

[[3]]
[[3]]$y1
[1] 3.079458 3.589123 3.902639 5.963523 5.244280 3.549689 6.566025 6.325017
[9] 5.332337 1.640506 5.265804 3.886971 3.203786 2.532067 4.734062 2.680252
[17] 4.223528 9.250713 5.815792 4.531292

[[3]]$y2
[1] 1 3 2 6 3 3 4 4 3 2 4 3 1 4 2 1 8 5 3 4

[[3]]$y3
[1] 0.96467045 0.82096525 0.36900807 0.65093944 0.08277442 0.44907291
[7] 0.05562078 1.60547741 1.31813850 0.40487347 0.53284797 0.47320076
[13] 0.41547343 0.01057435 1.11501319 0.31536054 0.28896671 0.51575956
[19] 0.32843798 0.08961490

[[3]]$y4
[1] 3.851959 2.828836 3.249421 2.202290 3.962181 3.891896 4.700026 4.652121
[9] 3.936827 4.678050 3.325526 4.656404 4.971151 2.315932 3.841990 3.823890
[17] 3.140251 2.831781 2.375118 2.396929

[[4]]
[[4]]$y1
[1] 3.079458 3.589123 3.902639 5.963523 5.244280 3.549689 6.566025 6.325017
[9] 5.332337 1.640506 5.265804 3.886971 3.203786 2.532067 4.734062 2.680252
[17] 4.223528 9.250713 5.815792 4.531292

[[4]]$y2
[1] 1 3 2 6 3 3 4 4 3 2 4 3 1 4 2 1 8 5 3 4

[[4]]$y3
[1] 0.96467045 0.82096525 0.36900807 0.65093944 0.08277442 0.44907291
[7] 0.05562078 1.60547741 1.31813850 0.40487347 0.53284797 0.47320076
[13] 0.41547343 0.01057435 1.11501319 0.31536054 0.28896671 0.51575956
[19] 0.32843798 0.08961490

[[4]]$y4
[1] 3.851959 2.828836 3.249421 2.202290 3.962181 3.891896 4.700026 4.652121
[9] 3.936827 4.678050 3.325526 4.656404 4.971151 2.315932 3.841990 3.823890
[17] 3.140251 2.831781 2.375118 2.396929
raja
Published on 21-Nov-2020 05:25:23
Advertisements