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

bookModifying List Elements

Lists are flexible: you can add, remove, or change their elements.

Adding Elements

To add a new element, assign a value to a new index or label.

Example

12345678
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Add new value with a label test["integer"] <- 23L # Add new value with an index test[[5]] <- "new element" test
copy
Note
Note

When adding by label, use quotes (e.g., "integer").

You can also merge two lists with the c() function, which combines them into one:

123456
list1 <- list("first", 10) list2 <- list("second", 20) # Merge lists list12 <- c(list1, list2) list12
copy

Removing Elements

To delete an element, assign NULL to it.

Example

12345
test <- list(text = "Text", number = 42, logical = TRUE) # Remove element test$logical <- NULL test
copy

Updating Elements

To change an element's value, simply reassign it.

Example

12345
test <- list(text = "Text", number = 42, logical = TRUE) # Update element test[1] <- "word" test
copy
Aufgabe

Swipe to start coding

You have a list info with course information.

Your tasks is to:

  1. Update the fourth element to 44 (this chapter is the 44th).
  2. Remove the third element (which is 1).
  3. Add a new element named Level with the value of 'Beginner'.
  4. Output modified list info.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 44
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookModifying List Elements

Swipe um das Menü anzuzeigen

Lists are flexible: you can add, remove, or change their elements.

Adding Elements

To add a new element, assign a value to a new index or label.

Example

12345678
# Creating a list test <- list(text = "Text", number = 42, logical = TRUE) # Add new value with a label test["integer"] <- 23L # Add new value with an index test[[5]] <- "new element" test
copy
Note
Note

When adding by label, use quotes (e.g., "integer").

You can also merge two lists with the c() function, which combines them into one:

123456
list1 <- list("first", 10) list2 <- list("second", 20) # Merge lists list12 <- c(list1, list2) list12
copy

Removing Elements

To delete an element, assign NULL to it.

Example

12345
test <- list(text = "Text", number = 42, logical = TRUE) # Remove element test$logical <- NULL test
copy

Updating Elements

To change an element's value, simply reassign it.

Example

12345
test <- list(text = "Text", number = 42, logical = TRUE) # Update element test[1] <- "word" test
copy
Aufgabe

Swipe to start coding

You have a list info with course information.

Your tasks is to:

  1. Update the fourth element to 44 (this chapter is the 44th).
  2. Remove the third element (which is 1).
  3. Add a new element named Level with the value of 'Beginner'.
  4. Output modified list info.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 44
single

single

some-alt