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.