r - 是否可以重命名列表中的多个列表名称?
问题描述
我想在列表中重命名我的列表名称。
例如:
library(randNames) #using this package for the random names for the example
my_info <- rand_names(10)
my_second_info <- rand_names(10)
list1 <- list(A = c(my_info$name.first), B = c(rnorm(1:10)), C = c(rnorm(1:10)), D = c(rnorm(1:10)))
list2 <- list(A = c(my_second_info$name.first), B = c(rnorm(1:10)), C = c(rnorm(1:10)), D = c(rnorm(1:10)))
both_lists <- list(list1,list2)
现在,当我调用 both_lists 时,我将 [[1]] 作为第一个列表的名称,将 [[2]] 作为第二个列表的名称(如此处所示)。
我想将 list1 和 list2 的名称(以及可能添加到其中的所有其他名称)更改为名字随机名称。因此,列表 1 将命名为 both_lists[[1]]$A[1],而列表 2 将命名为 both_lists[[2]]$A[1]。
这可能吗?或者列表名称是否会丢失,例如在制作列表列表时它只是变成一个特定的数字?
解决方案
您可以使用从所有列表中的子列表中sapply
提取第一个值并将其分配为名称。A
names(both_lists) <- sapply(both_lists, function(x) x$A[1])
推荐阅读
- r - 如何按行数过滤嵌套的数据框列表并从 R 中的列表中删除过滤的数据框?
- apollo - dataIdFromObject 错误地覆盖来自其他查询的缓存
- git - 我需要帮助处理 GitHub 上的大文件来帮助我的学校
- antlr - 在 ANTLR 4 Parse Tree 中获取正确的令牌 StopIndex 的问题
- gobblin - 运行 cli 命令时没有加载 GobblinCli
- android - MediaStore.Files.FileColumns.DATA 已被弃用,改用什么?
- python - 以太网数据不总是包含IP数据包吗?
- python - Python 理论 - 没有 len 函数的列表比较
- pine-script - 交易者查看 Pine 脚本 - 会话/天的绿色和红色蜡烛计数
- mysql - 创建触发器以检查 MySQL 中的外键值