Reputation: 547
I have a nested list;
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_list$ARANUD$predictor_2_list$ARANUD_spectral_predictors_2
is ultimately empty, though. It returns named list()
, but has no contents.
Given that my_list$ARANUD
is ultimately a dead-end, I would like to remove it.
The desired result would look like;
my_list_reduced <- 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))))))
The ideal solution would be able to be applied using any level of nesting and would cut the list back to the point where a dead end begins.
Therefore, given my_list2
, no named list()
dead ends would be present but no actual data would be lost.
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))))
This question is similar to Remove NULL elements from list of lists but I do not have list elements that are NULL
. Instead, my lists dead end in named list()
that is empty.
Upvotes: 1
Views: 50
Reputation: 6234
Using rrapply
in the rrapply
-package it suffices to set how = "prune"
to prune all empty list components:
rrapply::rrapply(my_list, how = "prune")
#> $ACESAC
#> $ACESAC$predictor_2_list
#> $ACESAC$predictor_2_list$ACESAC_spectral_predictors_2
#> $ACESAC$predictor_2_list$ACESAC_spectral_predictors_2$`NDVI_10;MCARI_MTVI`
#> spectral_2 species
#> 1 0.507 ACESAC
For my_list2
the complete predictor_1_list
component is pruned as it consists of only empty lists:
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
Upvotes: 1