How can i access list of list

how should i do to access list of list i have created

str(cv_results)

List of 10
 $ Fold01:List of 3
  ..$ : Named num 96.4
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 83.3
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold02:List of 3
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold03:List of 3
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold04:List of 3
  ..$ : Named num 96.4
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 83.3
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold05:List of 3
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold06:List of 3
  ..$ : Named num 96.4
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 83.3
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold07:List of 3
  ..$ : Named num 96.3
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 83.3
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold08:List of 3
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold09:List of 3
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"
 $ Fold10:List of 3
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Accuracy"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Sensitivity"
  ..$ : Named num 100
  .. ..- attr(*, "names")= chr "Specificity"

i want the output just "Accuracy" ... Thanks

Hi, can you start with providing a reprex? This would help a lot in solving your problem.

In general, all things list can be either solved with *apply functions (sapply, for example) or with purrr::map_*. In you case you should take a look at purrr::map_dbl.

1 Like

You need to iterate through the list to operate on the element you want.

You can also select to filter the list. Here are two ways:

sample <- list(
  Fold01 = list(Accuracy = 96.4, 
                Sensitivity = 83.3,
                Specificity = 100),
  Fold02 = list(Accuracy = 100, 
                Sensitivity = 100,
                Specificity = 100)
)

# base R solution
sapply(sample, function(x) x$Accuracy)
#> Fold01 Fold02 
#>   96.4  100.0

# tidyverse equivalent with purrr (safer)
library(purrr)
# a list 
sample %>% map("Accuracy")
#> $Fold01
#> [1] 96.4
#> 
#> $Fold02
#> [1] 100
# a vector of numeric
sample %>% map_dbl("Accuracy")
#> Fold01 Fold02 
#>   96.4  100.0

Created on 2018-12-16 by the reprex package (v0.2.1)

For list manipulation, purrr is really a good tool! You should look into it.

1 Like

Great :slight_smile: noted .. Thanks

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.