ComenzarEmpieza gratis

Access elements in a list

Subsetting a list is similar to subsetting a vector or data frame, with one extra useful operation.

To access the elements in the list, use [ ]. This will always return another list.

my_list[1]

$my_words
[1] "I <3 R"

my_list[c(1,2)]

$my_words
[1] "I <3 R"

$my_numbers
[1] 42 24

To pull out the data inside each element of your list, use [[ ]].

my_list[[1]]

[1] "I <3 R"

If your list is named, you can use the $ operator: my_list$my_words. This is the same as using [[ ]] to return the inner data.

Este ejercicio forma parte del curso

Introduction to R for Finance

Ver curso

Instrucciones del ejercicio

  • The portfolio named list is available for use.
  • Access the second and third elements of portfolio using [ ] and c().
  • Use $ to access the correlation data.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Second and third elements of portfolio


# Use $ to get the correlation data
Editar y ejecutar código