Modifying Vectors | Basic Data Types and Vectors
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:

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

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`.

Tarefa

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`.

Tudo estava claro?

Seção 2. Capítulo 8

Conteúdo do Curso

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:

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

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`.

Tarefa

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`.

Tudo estava claro?

Seção 2. Capítulo 8