You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中展开嵌套列表末层不等长向量并保留子列表名称

问题:处理嵌套列表中不等长末层向量,提取值与所属位置

首先创建示例嵌套列表:

preallocated_vector_quad <- c(1:2)
preallocated_vector_lin  <- c(3:5)
specification_list       <- list(quadratic = preallocated_vector_quad, linear = preallocated_vector_lin)
effects_list             <- list(fixed     = specification_list,        mixed = specification_list)
object_list              <- list(fit       = effects_list,              draws = effects_list)

用str()查看列表结构:

str(object_list)

List of 2
 $ fit  :List of 2
  ..$ fixed:List of 2
  .. ..$ quadratic: int [1:2] 1 2
  .. ..$ linear   : int [1:3] 3 4 5
  ..$ mixed:List of 2
  .. ..$ quadratic: int [1:2] 1 2
  .. ..$ linear   : int [1:3] 3 4 5
 $ draws:List of 2
  ..$ fixed:List of 2
  .. ..$ quadratic: int [1:2] 1 2
  .. ..$ linear   : int [1:3] 3 4 5
  ..$ mixed:List of 2
  .. ..$ quadratic: int [1:2] 1 2
  .. ..$ linear   : int [1:3] 3 4 5

期望输出

需要提取末层所有数值,同时保留其在列表中的所属位置(例如值3对应fixed.linear),支持两种格式:

长格式

value         type
    [1]  1        'fixed.quadratic' 
    [2]  2        'fixed.quadratic'  
    [3]  3        'fixed.linear'  
    [4]  4        'fixed.linear'  
    [5]  5        'fixed.linear'  
    [6]  1        'mixed.quadratic'  
    [7]  2        'mixed.quadratic'  
    [8]  3        'mixed.linear'  
    [9]  4        'mixed.linear'  
    [10] 5        'mixed.linear'  

宽格式

value     fixed.quadratic fixed.linear mixed.quadratic mixed.linear
[1]  1               1               0               0               0
[2]  2               1               0               0               0
[3]  3               0               1               0               0
[4]  4               0               1               0               0
[5]  5               0               1               0               0
[6]  1               0               0               1               0
[7]  2               0               0               1               0
[8]  3               0               0               0               1
[9]  4               0               0               0               1
[10] 5               0               0               0               1

已尝试方法及问题

  1. 部分展开列表得到命名子列表,但使用dplyr::bind_rows会因向量长度不等报错:
> unlist(object_list$draws, recursive = FALSE)

$fixed.quadratic
[1] 1 2

$fixed.linear
[1] 3 4 5

$mixed.quadratic
[1] 1 2

$mixed.linear
[1] 3 4 5
> do.call(dplyr::bind_rows,unlist(object_list$draws, recursive = FALSE) )
Error:
! Tibble columns must have compatible sizes.
* Size 2: Columns `fixed.quadratic` and `mixed.quadratic`.
* Size 3: Columns `fixed.linear` and `mixed.linear`.
i Only values of size one are recycled.
  1. 完全展开得到命名向量,但名称后会追加索引,处理效率低:
> unlist((unlist(object_list$draws, recursive = FALSE)))
fixed.quadratic1 fixed.quadratic2    fixed.linear1    fixed.linear2 
               1                2                3                4 
   fixed.linear3 mixed.quadratic1 mixed.quadratic2    mixed.linear1 
               5                1                2                3 
   mixed.linear2    mixed.linear3 
               4                5 

解决方案(仅用基础R函数)

1. 生成长格式输出

提取目标层级的命名列表,遍历每个子列表为数值匹配对应的类型名称:

# 提取目标层级的命名列表(以object_list$draws为例)
target_list <- unlist(object_list$draws, recursive = FALSE)

# 生成长格式数据框
long_df <- do.call(rbind, lapply(names(target_list), function(type) {
  data.frame(
    value = target_list[[type]],
    type = type,
    stringsAsFactors = FALSE
  )
}))

print(long_df)

输出结果:

value             type
1      1 fixed.quadratic
2      2 fixed.quadratic
3      3    fixed.linear
4      4    fixed.linear
5      5    fixed.linear
6      1 mixed.quadratic
7      2 mixed.quadratic
8      3    mixed.linear
9      4    mixed.linear
10     5    mixed.linear

2. 生成宽格式输出

基于长格式数据框,构造指示变量列生成宽格式:

# 获取所有唯一类型
all_types <- unique(long_df$type)

# 构造宽格式数据框
wide_df <- cbind(
  value = long_df$value,
  sapply(all_types, function(t) as.integer(long_df$type == t))
)

print(wide_df)

输出结果:

value fixed.quadratic fixed.linear mixed.quadratic mixed.linear
1      1               1            0               0            0
2      2               1            0               0            0
3      3               0            1               0            0
4      4               0            1               0            0
5      5               0            1               0            0
6      1               0            0               1            0
7      2               0            0               1            0
8      3               0            0               0            1
9      4               0            0               0            1
10     5               0            0               0            1

内容的提问来源于stack exchange,提问作者Kuku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 22:44:54