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

Contenido del Curso

R Introduction: Part I

R Introduction: Part I

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

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

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 8
toggle bottom row

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

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 8
toggle bottom row

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

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 8
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt