R从嵌套列表中删除命名列表()死角

R从嵌套列表中删除命名列表()死角,r,list,recursion,R,List,Recursion,我有一个嵌套列表 my_list <- list(ACESAC = list(predictor_2_list = list(ACESAC_spectral_predictors_2 = list( `NDVI_10;MCARI_MTVI` = structure(list(spectral_2 = 0.507, species = "ACESAC"), class = "data.frame", row.names = c

我有一个嵌套列表

my_list <- list(ACESAC = list(predictor_2_list = list(ACESAC_spectral_predictors_2 = list(
    `NDVI_10;MCARI_MTVI` = structure(list(spectral_2 = 0.507, 
        species = "ACESAC"), class = "data.frame", row.names = c(NA, 
    -1L))))), ARANUD = list(predictor_2_list = list(ARANUD_spectral_predictors_2 = structure(list(), .Names = character(0)))))
理想的解决方案将能够使用任何级别的嵌套应用,并将列表缩减到死角开始的位置

因此,给定
my_list2
,不会出现
命名列表()
死角,但不会丢失实际数据

my_list2 <- list(ABIBAL = list(predictor_1_list = list(ABIBAL_all_predictors_1 = structure(list(), .Names = character(0)), 
    ABIBAL_topo_predictors_1 = structure(list(), .Names = character(0)), 
    ABIBAL_spectral_predictors_1 = structure(list(), .Names = character(0)), 
    ABIBAL_topo_spectral_predictors_1 = structure(list(), .Names = character(0)), 
    ABIBAL_soils_predictors_1 = structure(list(), .Names = character(0))), 
    predictor_2_list = list(ABIBAL_all_predictors_2 = list(`B.Al;NDVI_10` = 0.656), 
        ABIBAL_topo_predictors_2 = list(`tpi25.MEAN;twi_dd.STD` = 0.644), 
        ABIBAL_spectral_predictors_2 = list(`NDVI_10;MCARI_MTVI` = 0.649), 
        ABIBAL_topo_spectral_predictors_2 = list(`TAS_mean.MEAN;solar_rad_total_20m` = 0.675), 
        ABIBAL_soils_predictors_2 = list(`B.Al;OA.pH` = 0.718)), 
    predictor_3_list = list(ABIBAL_all_predictors_3 = list(`B.Al;OA.P;NDVI_10` = 0.805), 
        ABIBAL_topo_predictors_3 = list(`TAS_mean.MEAN;tpi25.MEAN;solar_rad_total_20m` = 0.707), 
        ABIBAL_spectral_predictors_3 = list(`NDWI_10;NDVI_10;MCARI_MTVI` = 0.558), 
        ABIBAL_topo_spectral_predictors_3 = list(`TAS_mean.MEAN;solar_rad_total_20m;NDWI_10` = 0.729), 
        ABIBAL_soils_predictors_3 = list(`OA.Al;OA.pH;B.C` = 0.758))))

my_list2使用
rrapply
包中的
rrapply
,设置
how=“prune”
即可修剪所有空列表组件:

rrapply::rrapply(我的列表,how=“prune”)
#>$ACESAC
#>$ACESAC$predictor\u 2\u列表
#>$ACESAC$预测值\u 2\u列表$ACESAC\u光谱\u预测值\u 2
#>$ACESAC$预测值\ 2\列表$ACESAC\光谱\预测值\ 2$`NDVI\ 10;麦卡里乌姆特维`
#>光谱2种
#>1 0.507 ACESAC
对于
my_list2
完整的
predictor_1_list
组件将被删除,因为它只包含空列表:

str(rrapply::rrapply(my_list2, how = "prune"))

#> List of 1
#>  $ ABIBAL:List of 2
#>   ..$ predictor_2_list:List of 5
#>   .. ..$ ABIBAL_all_predictors_2          :List of 1
#>   .. .. ..$ B.Al;NDVI_10: num 0.656
#>   .. ..$ ABIBAL_topo_predictors_2         :List of 1
#>   .. .. ..$ tpi25.MEAN;twi_dd.STD: num 0.644
#>   .. ..$ ABIBAL_spectral_predictors_2     :List of 1
#>   .. .. ..$ NDVI_10;MCARI_MTVI: num 0.649
#>   .. ..$ ABIBAL_topo_spectral_predictors_2:List of 1
#>   .. .. ..$ TAS_mean.MEAN;solar_rad_total_20m: num 0.675
#>   .. ..$ ABIBAL_soils_predictors_2        :List of 1
#>   .. .. ..$ B.Al;OA.pH: num 0.718
#>   ..$ predictor_3_list:List of 5
#>   .. ..$ ABIBAL_all_predictors_3          :List of 1
#>   .. .. ..$ B.Al;OA.P;NDVI_10: num 0.805
#>   .. ..$ ABIBAL_topo_predictors_3         :List of 1
#>   .. .. ..$ TAS_mean.MEAN;tpi25.MEAN;solar_rad_total_20m: num 0.707
#>   .. ..$ ABIBAL_spectral_predictors_3     :List of 1
#>   .. .. ..$ NDWI_10;NDVI_10;MCARI_MTVI: num 0.558
#>   .. ..$ ABIBAL_topo_spectral_predictors_3:List of 1
#>   .. .. ..$ TAS_mean.MEAN;solar_rad_total_20m;NDWI_10: num 0.729
#>   .. ..$ ABIBAL_soils_predictors_3        :List of 1
#>   .. .. ..$ OA.Al;OA.pH;B.C: num 0.758

那么对于您的
my_list 2
您想要
predictor_1_list
出现吗?
str(rrapply::rrapply(my_list2, how = "prune"))

#> List of 1
#>  $ ABIBAL:List of 2
#>   ..$ predictor_2_list:List of 5
#>   .. ..$ ABIBAL_all_predictors_2          :List of 1
#>   .. .. ..$ B.Al;NDVI_10: num 0.656
#>   .. ..$ ABIBAL_topo_predictors_2         :List of 1
#>   .. .. ..$ tpi25.MEAN;twi_dd.STD: num 0.644
#>   .. ..$ ABIBAL_spectral_predictors_2     :List of 1
#>   .. .. ..$ NDVI_10;MCARI_MTVI: num 0.649
#>   .. ..$ ABIBAL_topo_spectral_predictors_2:List of 1
#>   .. .. ..$ TAS_mean.MEAN;solar_rad_total_20m: num 0.675
#>   .. ..$ ABIBAL_soils_predictors_2        :List of 1
#>   .. .. ..$ B.Al;OA.pH: num 0.718
#>   ..$ predictor_3_list:List of 5
#>   .. ..$ ABIBAL_all_predictors_3          :List of 1
#>   .. .. ..$ B.Al;OA.P;NDVI_10: num 0.805
#>   .. ..$ ABIBAL_topo_predictors_3         :List of 1
#>   .. .. ..$ TAS_mean.MEAN;tpi25.MEAN;solar_rad_total_20m: num 0.707
#>   .. ..$ ABIBAL_spectral_predictors_3     :List of 1
#>   .. .. ..$ NDWI_10;NDVI_10;MCARI_MTVI: num 0.558
#>   .. ..$ ABIBAL_topo_spectral_predictors_3:List of 1
#>   .. .. ..$ TAS_mean.MEAN;solar_rad_total_20m;NDWI_10: num 0.729
#>   .. ..$ ABIBAL_soils_predictors_3        :List of 1
#>   .. .. ..$ OA.Al;OA.pH;B.C: num 0.758