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

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

Rename multiple lists items in for loop

问题

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

创建具有项目名称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)))
}
}


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

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

#create 3 lists with items names a, b, c
list1 &lt;- list(a=c(1:10), b=c(1:5), c=c(1:3))
list2 &lt;- list(a=&quot;a&quot;, b=&quot;b&quot;, c=&quot;c&quot;)
list3 &lt;- list(a=runif(4), b=runif(2), c=runif(4))


# wanted names of list items 
names &lt;- c(&quot;first&quot;, &quot;second&quot;, &quot;third&quot;)

new_list &lt;- list()

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

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

答案1

得分: 3

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

lapply(list(list1, list2, list3), \(x) setNames(x, names))
#> [[1]]
#> [[1]]$first
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> [[1]]$second
#> [1] 1 2 3 4 5
#> 
#> [[1]]$third
#> [1] 1 2 3
#> 
#> 
#> [[2]]
#> [[2]]$first
#> [1] "a"
#> 
#> [[2]]$second
#> [1] "b"
#> 
#> [[2]]$third
#> [1] "c"
#> 
#> 
#> [[3]]
#> [[3]]$first
#> [1] 0.7475211 0.9317233 0.1603680 0.7952381
#> 
#> [[3]]$second
#> [1] 0.5450439 0.8351545
#> 
#> [[3]]$third
#> [1] 0.1782355 0.9909339 0.2366660 0.7104271

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

来自 OP 的数据

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

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

new_list <- list()

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

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

英文:

We can use lapply() and setNames():

lapply(list(list1, list2, list3), \(x) setNames(x, names))
#&gt; [[1]]
#&gt; [[1]]$first
#&gt;  [1]  1  2  3  4  5  6  7  8  9 10
#&gt; 
#&gt; [[1]]$second
#&gt; [1] 1 2 3 4 5
#&gt; 
#&gt; [[1]]$third
#&gt; [1] 1 2 3
#&gt; 
#&gt; 
#&gt; [[2]]
#&gt; [[2]]$first
#&gt; [1] &quot;a&quot;
#&gt; 
#&gt; [[2]]$second
#&gt; [1] &quot;b&quot;
#&gt; 
#&gt; [[2]]$third
#&gt; [1] &quot;c&quot;
#&gt; 
#&gt; 
#&gt; [[3]]
#&gt; [[3]]$first
#&gt; [1] 0.7475211 0.9317233 0.1603680 0.7952381
#&gt; 
#&gt; [[3]]$second
#&gt; [1] 0.5450439 0.8351545
#&gt; 
#&gt; [[3]]$third
#&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

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

names &lt;- c(&quot;first&quot;, &quot;second&quot;, &quot;third&quot;)

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

new_list &lt;- list()

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

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

答案2

得分: 1

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

names(list1)
# [1] "a" "b" "c"

list2env(lapply(mget(ls(pattern = "^list")), 
                function(i) setNames(i, names)), globalenv())

names(list1)
# [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:

names(list1)
# [1] &quot;a&quot; &quot;b&quot; &quot;c&quot;

list2env(lapply(mget(ls(pattern = &quot;^list&quot;)), 
                function(i) setNames(i, names)), globalenv())

names(list1)
# [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:

确定