Course Content
R Introduction: Part I
R Introduction: Part I
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:
# 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
Now, let’s try the second method where we assign a name to the value as we add it:
# 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
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
.
Swipe to show code editor
- Add a new item named
'Desk'
with a price of135
to the end of theprices
vector using the second method (assigning the name while adding the value). - Update the price of the
'Bookshelf'
to180
. You can use either the index or the name to do this. - Display the modified vector
prices
.
Thanks for your feedback!
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:
# 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
Now, let’s try the second method where we assign a name to the value as we add it:
# 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
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
.
Swipe to show code editor
- Add a new item named
'Desk'
with a price of135
to the end of theprices
vector using the second method (assigning the name while adding the value). - Update the price of the
'Bookshelf'
to180
. You can use either the index or the name to do this. - Display the modified vector
prices
.
Thanks for your feedback!