Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Modifying Vector Elements | Data Types and Vectors
R Introduction

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
Task

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.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 8
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 2.27

bookModifying Vector Elements

Swipe to show menu

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
Task

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.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 8
single

single

some-alt