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

bookModifying Vector Elements

Vectors can be modified by adding new elements or updating existing ones. This is useful when the data structure needs to grow or when values need to be corrected.

Adding Elements with Functions

Use the c() function or the append() function to add a new value to a vector. If the vector is named, you can then assign a label to the new element.

Example

12345678
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades <- c(grades, 60) names(grades)[length(grades)] <- 'Philosophy' grades
copy

Adding Elements with Names

If the vector already has names, you can add a new element by assigning a value directly to a new name.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades['Philosophy'] <- 60 grades
copy

Updating Elements

You can also modify existing values either by name or by index.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Update second grade grades[2] <- 60 # Update Math grade by name grades["Math"] <- 100
copy
Taak

Swipe to start coding

  1. Add a new item named 'Desk' with a price of 135 to the end of the prices vector using the second method (assigning the name while adding the value).
  2. Update the price of the 'Bookshelf' to 180. You can use either the index or the name to do this.
  3. Display the modified vector prices.

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 18
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

close

bookModifying Vector Elements

Veeg om het menu te tonen

Vectors can be modified by adding new elements or updating existing ones. This is useful when the data structure needs to grow or when values need to be corrected.

Adding Elements with Functions

Use the c() function or the append() function to add a new value to a vector. If the vector is named, you can then assign a label to the new element.

Example

12345678
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades <- c(grades, 60) names(grades)[length(grades)] <- 'Philosophy' grades
copy

Adding Elements with Names

If the vector already has names, you can add a new element by assigning a value directly to a new name.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades['Philosophy'] <- 60 grades
copy

Updating Elements

You can also modify existing values either by name or by index.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Update second grade grades[2] <- 60 # Update Math grade by name grades["Math"] <- 100
copy
Taak

Swipe to start coding

  1. Add a new item named 'Desk' with a price of 135 to the end of the prices vector using the second method (assigning the name while adding the value).
  2. Update the price of the 'Bookshelf' to 180. You can use either the index or the name to do this.
  3. Display the modified vector prices.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 18
single

single

some-alt