我有一个名为player.i.team.j.coach.k
的整数值,其中i
的范围超过值1-11,j
范围超过值1-30和k
范围超过值1-10。
我正在尝试将每个团队的11名玩家j
和教练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))}}}
错误是“分配目标扩展到非语言对象”。
注意:我使用player.i.team.j.coach.k
和循环创建了对象assign()
,这就是它们存储在列表中的原因。
答案 0 :(得分:-1)
x = c(3,5,6)
x[2] = 9
x # [1] 3 9 6
这有帮助吗? (将值重新分配给列表的第i个索引,而不指定并使用循环)
答案 1 :(得分:-2)
好吧,经过几次挣扎,看着我的名声因为几次挫折而下厕所后,我设法解决了我的问题。我在循环中使用变量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