英文:
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 <- 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))
# wanted names of list items
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)))
}
}
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))
#> [[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
<sup>Created on 2023-03-07 by the reprex package (v2.0.1)</sup>
Data from 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")
We could also use a for
loop with setNames)()
:
new_list <- list()
for (i in 1:3){
new_list[[i]] <- setNames(get(paste0("list", 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] "a" "b" "c"
list2env(lapply(mget(ls(pattern = "^list")),
function(i) setNames(i, names)), globalenv())
names(list1)
# [1] "first" "second" "third"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论