Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Indexing List Elements | Section
Practice
Projects
Quizzes & Challenges
Quizer
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

bookIndexing List Elements

List elements can be accessed in several ways. And just like in vectors, indexing starts at 1.

Access by Index

Elements in a list can be accessed with either single or double brackets:

  • Using single brackets ([ ]) returns the element as a list;
  • Using double brackets ([[ ]]) returns the value itself.

Example

123456
test <- list(text = "Text", number = 42, logical = TRUE) # Extract as list test[2] # Extract as value test[[2]]
copy

Access by Label

If a list has labels, you can extract elements using those labels. Just like with indices, you can use single brackets ([ ]) to return a list or double brackets ([[ ]]) to return the value. Additionally, the dollar sign ($) provides a shorthand for accessing values.

Example

1234567
test <- list(text = "Text", number = 42, logical = TRUE) # Extract as list test["text"] # Extract as value test[["text"]] test$text
copy
Oppgave

Swipe to start coding

You have a list info with course information.

You task is to:

  1. Extract the first element as a list.
  2. Output the class of this element with the class() function.
  3. Extract the fourth element as a numeric type.
  4. Output the class of this element with the class() function.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 43
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookIndexing List Elements

Sveip for å vise menyen

List elements can be accessed in several ways. And just like in vectors, indexing starts at 1.

Access by Index

Elements in a list can be accessed with either single or double brackets:

  • Using single brackets ([ ]) returns the element as a list;
  • Using double brackets ([[ ]]) returns the value itself.

Example

123456
test <- list(text = "Text", number = 42, logical = TRUE) # Extract as list test[2] # Extract as value test[[2]]
copy

Access by Label

If a list has labels, you can extract elements using those labels. Just like with indices, you can use single brackets ([ ]) to return a list or double brackets ([[ ]]) to return the value. Additionally, the dollar sign ($) provides a shorthand for accessing values.

Example

1234567
test <- list(text = "Text", number = 42, logical = TRUE) # Extract as list test["text"] # Extract as value test[["text"]] test$text
copy
Oppgave

Swipe to start coding

You have a list info with course information.

You task is to:

  1. Extract the first element as a list.
  2. Output the class of this element with the class() function.
  3. Extract the fourth element as a numeric type.
  4. Output the class of this element with the class() function.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 43
single

single

some-alt