Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Modifying Vectors | Basic Data Types and Vectors
R Introduction: Part I
course content

Course Content

R Introduction: Part I

R Introduction: Part I

1. Basic Syntax and Operations
2. Basic Data Types and Vectors
3. Factors

book
Modifying Vectors

Excellent! You now know how to create a vector, name its values, and extract elements from it. The next step we will explore is how to modify a vector by adding new items to it or deleting existing ones.

There are a couple of methods to add a new value to an existing vector. Suppose we have a vector named vec and we wish to append new_value to it, with the name 'new_name' assigned to this new value. The diagram below illustrates both methods.

For instance, let’s apply these methods using the grades example by adding a new grade, 60, for the subject 'Philosophy'. The first method involves utilizing vectors:

1234567
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new value using vectors grades <- c(grades, 60) # Add new value names(grades)[length(grades)] <- 'Philosophy' # Add name grades # Output the vector
copy

Now, let’s try the second method where we assign a name to the value as we add it:

1234567
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new value grades['Philosophy'] <- 60 # Add value with name grades # Output the vector
copy

Thus, if your vector does not have names, the first method is more appropriate. However, if your vector already has names, the second method is preferable.

With the second method, you can add new values and also reassign existing ones. For example, if you execute grades['Math'] <- 100, the original grade of 80 for Math will be updated to 100. You can achieve the same result by referring to the index, like this: grades[1] <- 100.

Task
test

Swipe to show code editor

  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.

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
toggle bottom row

book
Modifying Vectors

Excellent! You now know how to create a vector, name its values, and extract elements from it. The next step we will explore is how to modify a vector by adding new items to it or deleting existing ones.

There are a couple of methods to add a new value to an existing vector. Suppose we have a vector named vec and we wish to append new_value to it, with the name 'new_name' assigned to this new value. The diagram below illustrates both methods.

For instance, let’s apply these methods using the grades example by adding a new grade, 60, for the subject 'Philosophy'. The first method involves utilizing vectors:

1234567
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new value using vectors grades <- c(grades, 60) # Add new value names(grades)[length(grades)] <- 'Philosophy' # Add name grades # Output the vector
copy

Now, let’s try the second method where we assign a name to the value as we add it:

1234567
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new value grades['Philosophy'] <- 60 # Add value with name grades # Output the vector
copy

Thus, if your vector does not have names, the first method is more appropriate. However, if your vector already has names, the second method is preferable.

With the second method, you can add new values and also reassign existing ones. For example, if you execute grades['Math'] <- 100, the original grade of 80 for Math will be updated to 100. You can achieve the same result by referring to the index, like this: grades[1] <- 100.

Task
test

Swipe to show code editor

  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.

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
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt