如何在 R 中创建重复的列表列表?


有时我们希望创建重复的值,这在比如衡量常数对多个变量的影响等不同场景中很有帮助。列表值也可以出于类似的分析目的进行复制。可以通过使用 rep 函数来创建列表的列表的复制。例如,如果我们有一个名为 x 的列表,并且我们希望创建此列表的五次重复列表,那么我们可以使用代码 rep(list(x),5)。

示例 1

实时演示

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

输出

$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"

示例

> rep(list(List1),5)

输出

[[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"

示例

> rep(list(List1),10)

输出

[[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"

示例 2

实时演示

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

输出

$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

示例

> rep(list(List2),4)

输出

[[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

更新日期: 21-11-2020

2K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始
广告