如何使用循环将值分配给列表的第 i 个索引?

How to assign values to the i-th index of a list using a loop?

我有称为 player.i.team.j.coach.k 的整数值,其中 i 范围超过值 1-11,j 范围超过值 1-30 和 k 范围在值 1-10.

我正在尝试将每支球队 j 的 11 名球员和教练 k 存储在一个列表中(每个教练将被分配到 30 支球队),例如

team.j.coach.k <- c(player.1.team.j.coach.k, player.2.team.j.coach.k,
                    player.3.team.j.coach.k, player.4.team.j.coach.k,
                    player.5.team.j.coach.k, player.6.team.j.coach.k, 
                    player.7.team.j.coach.k, player.8.team.j.coach.k, 
                    player.9.team.j.coach.k, player.10.team.j.coach.k, 
                    player.11.team.j.coach.k)

我正在尝试为此使用循环。问题是我的代码不工作:

首先我定义了我将存储我的团队的空列表:

for (j in 1:30) {
  for (k in 1:10) {
    assign(paste0("team.",j,".coach.",k),c())
  }
}

例如

> team.1.coach.1
NULL

但是现在下面的代码会报错

for (i in 1:11) {
  for (j in 1:30) {
    for (k in 1:10) {
      assign(get(paste0("team.", j, ".coach.", k))[i], 
             get(paste0("player.",i,".team.",j,".coach.",k)))
    }
  }
}

错误是“第一个参数无效”。或者,以下代码也会引发错误

for (i in 1:11) {
  for (j in 1:30) {
    for (k in 1:10) {
      get(paste0("team.", j, ".coach.", k))[i] <- get(paste0("player.", i, ".team.", j, ".coach.", k))
    }
  }
}  

错误是“赋值目标扩展到非语言对象”。

注意:我使用 assign() 和一个循环创建了对象 player.i.team.j.coach.k,这就是它们存储在列表中的原因。

x = c(3,5,6)
x[2] = 9
x  # [1] 3 9 6

这有帮助吗? (重新分配一个值给列表的 i-th 索引而不分配并使用循环)

好吧,在经历了如此多的挣扎并看着我的名声因几次反对票而一落千丈之后,我设法解决了我的问题。我在循环中使用了一个变量 count 将所有内容存储为一个列表,然后将我的列表拆分为我想要的列表:

my.list<-c()

count=0

for (c in 1:10){for (b in 1:30){for (a in 1:11){assign(paste0("player.",a,".team.",b,".coach.",c),a+b+c);count=count+1;my.list[count]<-get(paste0("player.",a,".team.",b,".coach.",c))}}}

现在我拆分 my.list 以获得我想要的列表:

for (c in 1:10){for (b in 1:30){assign(paste0("team.",b,".coach.",c),my.list[(11*(b+30*(c-1))-10):(11*(b+30*(c-1)))])}}

我得到了我想要的:

> for (b in 1:30){for (c in 1:10){print(get(paste0("team.",b,".coach.",c)))}}
 [1]  3  4  5  6  7  8  9 10 11 12 13
 [1]  4  5  6  7  8  9 10 11 12 13 14
 [1]  5  6  7  8  9 10 11 12 13 14 15
 [1]  6  7  8  9 10 11 12 13 14 15 16
 [1]  7  8  9 10 11 12 13 14 15 16 17
 [1]  8  9 10 11 12 13 14 15 16 17 18
 [1]  9 10 11 12 13 14 15 16 17 18 19
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1]  4  5  6  7  8  9 10 11 12 13 14
 [1]  5  6  7  8  9 10 11 12 13 14 15
 [1]  6  7  8  9 10 11 12 13 14 15 16
 [1]  7  8  9 10 11 12 13 14 15 16 17
 [1]  8  9 10 11 12 13 14 15 16 17 18
 [1]  9 10 11 12 13 14 15 16 17 18 19
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1]  5  6  7  8  9 10 11 12 13 14 15
 [1]  6  7  8  9 10 11 12 13 14 15 16
 [1]  7  8  9 10 11 12 13 14 15 16 17
 [1]  8  9 10 11 12 13 14 15 16 17 18
 [1]  9 10 11 12 13 14 15 16 17 18 19
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1]  6  7  8  9 10 11 12 13 14 15 16
 [1]  7  8  9 10 11 12 13 14 15 16 17
 [1]  8  9 10 11 12 13 14 15 16 17 18
 [1]  9 10 11 12 13 14 15 16 17 18 19
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1]  7  8  9 10 11 12 13 14 15 16 17
 [1]  8  9 10 11 12 13 14 15 16 17 18
 [1]  9 10 11 12 13 14 15 16 17 18 19
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1]  8  9 10 11 12 13 14 15 16 17 18
 [1]  9 10 11 12 13 14 15 16 17 18 19
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1]  9 10 11 12 13 14 15 16 17 18 19
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 10 11 12 13 14 15 16 17 18 19 20
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 11 12 13 14 15 16 17 18 19 20 21
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 12 13 14 15 16 17 18 19 20 21 22
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 13 14 15 16 17 18 19 20 21 22 23
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 14 15 16 17 18 19 20 21 22 23 24
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 15 16 17 18 19 20 21 22 23 24 25
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 16 17 18 19 20 21 22 23 24 25 26
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 17 18 19 20 21 22 23 24 25 26 27
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 18 19 20 21 22 23 24 25 26 27 28
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 19 20 21 22 23 24 25 26 27 28 29
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 20 21 22 23 24 25 26 27 28 29 30
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 21 22 23 24 25 26 27 28 29 30 31
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 22 23 24 25 26 27 28 29 30 31 32
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 23 24 25 26 27 28 29 30 31 32 33
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 24 25 26 27 28 29 30 31 32 33 34
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 25 26 27 28 29 30 31 32 33 34 35
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 26 27 28 29 30 31 32 33 34 35 36
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 35 36 37 38 39 40 41 42 43 44 45
 [1] 27 28 29 30 31 32 33 34 35 36 37
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 35 36 37 38 39 40 41 42 43 44 45
 [1] 36 37 38 39 40 41 42 43 44 45 46
 [1] 28 29 30 31 32 33 34 35 36 37 38
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 35 36 37 38 39 40 41 42 43 44 45
 [1] 36 37 38 39 40 41 42 43 44 45 46
 [1] 37 38 39 40 41 42 43 44 45 46 47
 [1] 29 30 31 32 33 34 35 36 37 38 39
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 35 36 37 38 39 40 41 42 43 44 45
 [1] 36 37 38 39 40 41 42 43 44 45 46
 [1] 37 38 39 40 41 42 43 44 45 46 47
 [1] 38 39 40 41 42 43 44 45 46 47 48
 [1] 30 31 32 33 34 35 36 37 38 39 40
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 35 36 37 38 39 40 41 42 43 44 45
 [1] 36 37 38 39 40 41 42 43 44 45 46
 [1] 37 38 39 40 41 42 43 44 45 46 47
 [1] 38 39 40 41 42 43 44 45 46 47 48
 [1] 39 40 41 42 43 44 45 46 47 48 49
 [1] 31 32 33 34 35 36 37 38 39 40 41
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 35 36 37 38 39 40 41 42 43 44 45
 [1] 36 37 38 39 40 41 42 43 44 45 46
 [1] 37 38 39 40 41 42 43 44 45 46 47
 [1] 38 39 40 41 42 43 44 45 46 47 48
 [1] 39 40 41 42 43 44 45 46 47 48 49
 [1] 40 41 42 43 44 45 46 47 48 49 50
 [1] 32 33 34 35 36 37 38 39 40 41 42
 [1] 33 34 35 36 37 38 39 40 41 42 43
 [1] 34 35 36 37 38 39 40 41 42 43 44
 [1] 35 36 37 38 39 40 41 42 43 44 45
 [1] 36 37 38 39 40 41 42 43 44 45 46
 [1] 37 38 39 40 41 42 43 44 45 46 47
 [1] 38 39 40 41 42 43 44 45 46 47 48
 [1] 39 40 41 42 43 44 45 46 47 48 49
 [1] 40 41 42 43 44 45 46 47 48 49 50
 [1] 41 42 43 44 45 46 47 48 49 50 51