在 for 循环中重命名多个列表项。

huangapple go评论156阅读模式
英文:

Rename multiple lists items in for loop

问题

  1. 我有多个具有相同数量项目的列表。我想在循环中重命名所有列表项。例如,从列表项名称“abc”,我想将它们重命名为“第一个,第二个,第三个”:

创建具有项目名称a、b、c的3个列表

list1 <- list(a=c(1:10), b=c(1:5), c=c(1:3))
list2 <- list(a="a", b="b", c="c")
list3 <- list(a=runif(4), b=runif(2), c=runif(4))

列表项的期望名称

names <- c("first", "second", "third")

new_list <- list()

for (i in 1:3){
for(j in seq_along(names)) {
n <- paste0(names[[j]])
new_list[[n]] <- assign(paste0("list_", i), get(paste0("list", i)))
}
}

  1. 但结果只重命名了第三个列表。我应该如何一次重命名所有列表项?
英文:

I have multiple list with the same amount of items. I want to rename all lists items in loop. For example from lists items names "a, b, c", I want to rename them to "first, second, third":

  1. #create 3 lists with items names a, b, c
  2. list1 &lt;- list(a=c(1:10), b=c(1:5), c=c(1:3))
  3. list2 &lt;- list(a=&quot;a&quot;, b=&quot;b&quot;, c=&quot;c&quot;)
  4. list3 &lt;- list(a=runif(4), b=runif(2), c=runif(4))
  5. # wanted names of list items
  6. names &lt;- c(&quot;first&quot;, &quot;second&quot;, &quot;third&quot;)
  7. new_list &lt;- list()
  8. for (i in 1:3){
  9. for(j in seq_along(names)) {
  10. n &lt;- paste0(names[[j]])
  11. new_list[[n]] &lt;- assign(paste0(&quot;list_&quot;, i), get(paste0(&quot;list&quot;, i)))
  12. }
  13. }

But the result rename only third list.. How should I rename all lists items at once?

答案1

得分: 3

我们可以使用 lapply()setNames()

  1. lapply(list(list1, list2, list3), \(x) setNames(x, names))
  2. #> [[1]]
  3. #> [[1]]$first
  4. #> [1] 1 2 3 4 5 6 7 8 9 10
  5. #>
  6. #> [[1]]$second
  7. #> [1] 1 2 3 4 5
  8. #>
  9. #> [[1]]$third
  10. #> [1] 1 2 3
  11. #>
  12. #>
  13. #> [[2]]
  14. #> [[2]]$first
  15. #> [1] "a"
  16. #>
  17. #> [[2]]$second
  18. #> [1] "b"
  19. #>
  20. #> [[2]]$third
  21. #> [1] "c"
  22. #>
  23. #>
  24. #> [[3]]
  25. #> [[3]]$first
  26. #> [1] 0.7475211 0.9317233 0.1603680 0.7952381
  27. #>
  28. #> [[3]]$second
  29. #> [1] 0.5450439 0.8351545
  30. #>
  31. #> [[3]]$third
  32. #> [1] 0.1782355 0.9909339 0.2366660 0.7104271

创建于2023年3月7日,由 reprex package (v2.0.1) 创建

来自 OP 的数据

  1. list1 <- list(a=c(1:10), b=c(1:5), c=c(1:3))
  2. list2 <- list(a="a", b="b", c="c")
  3. list3 <- list(a=runif(4), b=runif(2), c=runif(4))
  4. names <- c("first", "second", "third")

我们还可以使用 for 循环和 setNames()

  1. new_list <- list()
  2. for (i in 1:3){
  3. new_list[[i]] <- setNames(get(paste0("list", i)), names)
  4. }

创建于2023年3月7日,由 reprex package (v2.0.1) 创建

英文:

We can use lapply() and setNames():

  1. lapply(list(list1, list2, list3), \(x) setNames(x, names))
  2. #&gt; [[1]]
  3. #&gt; [[1]]$first
  4. #&gt; [1] 1 2 3 4 5 6 7 8 9 10
  5. #&gt;
  6. #&gt; [[1]]$second
  7. #&gt; [1] 1 2 3 4 5
  8. #&gt;
  9. #&gt; [[1]]$third
  10. #&gt; [1] 1 2 3
  11. #&gt;
  12. #&gt;
  13. #&gt; [[2]]
  14. #&gt; [[2]]$first
  15. #&gt; [1] &quot;a&quot;
  16. #&gt;
  17. #&gt; [[2]]$second
  18. #&gt; [1] &quot;b&quot;
  19. #&gt;
  20. #&gt; [[2]]$third
  21. #&gt; [1] &quot;c&quot;
  22. #&gt;
  23. #&gt;
  24. #&gt; [[3]]
  25. #&gt; [[3]]$first
  26. #&gt; [1] 0.7475211 0.9317233 0.1603680 0.7952381
  27. #&gt;
  28. #&gt; [[3]]$second
  29. #&gt; [1] 0.5450439 0.8351545
  30. #&gt;
  31. #&gt; [[3]]$third
  32. #&gt; [1] 0.1782355 0.9909339 0.2366660 0.7104271

<sup>Created on 2023-03-07 by the reprex package (v2.0.1)</sup>

Data from OP

  1. list1 &lt;- list(a=c(1:10), b=c(1:5), c=c(1:3))
  2. list2 &lt;- list(a=&quot;a&quot;, b=&quot;b&quot;, c=&quot;c&quot;)
  3. list3 &lt;- list(a=runif(4), b=runif(2), c=runif(4))
  4. names &lt;- c(&quot;first&quot;, &quot;second&quot;, &quot;third&quot;)

We could also use a for loop with setNames)():

  1. new_list &lt;- list()
  2. for (i in 1:3){
  3. new_list[[i]] &lt;- setNames(get(paste0(&quot;list&quot;, i)), names)
  4. }

<sup>Created on 2023-03-07 by the reprex package (v2.0.1)</sup>

答案2

得分: 1

将所有的列表放入一个列表中,循环遍历并分配新的名称,然后将它们放回环境中,注意我们正在覆盖原始列表:

  1. names(list1)
  2. # [1] "a" "b" "c"
  3. list2env(lapply(mget(ls(pattern = "^list")),
  4. function(i) setNames(i, names)), globalenv())
  5. names(list1)
  6. # [1] "first" "second" "third"
英文:

Put all lists into a list, loop through and assign new names, and put them back into environment, notice we are overwriting original lists:

  1. names(list1)
  2. # [1] &quot;a&quot; &quot;b&quot; &quot;c&quot;
  3. list2env(lapply(mget(ls(pattern = &quot;^list&quot;)),
  4. function(i) setNames(i, names)), globalenv())
  5. names(list1)
  6. # [1] &quot;first&quot; &quot;second&quot; &quot;third&quot;

huangapple
  • 本文由 发表于 2023年3月7日 21:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定