Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Indexing in 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

Indexing in Vectors

Great! Now you understand how to create a vector and assign names to its values.

To access specific elements within a vector, you can use indices. The index of the first element is 1, the second element is 2, and so on. To retrieve the n-th element from a vector vec, use the syntax vec[n]. Additionally, if the vector elements have assigned names, you can extract the values by their names by enclosing the name in quotes and square brackets.

Now, let’s revisit the grades example:

We'll extract the grade for Math and the grade for Literature.

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the first grade grades[1] # Output Literature grade grades['Literature']
copy

It's also possible to access multiple elements simultaneously:

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the second and the third grades grades[c(2,3)] # Output Math and Physics grades grades[c('Math','Physics')]
copy

Tarea

Continuing from the previous exercise, you have a vector named prices with each price assigned to a corresponding item name.

  1. Retrieve the price of 'Armchair' using its name rather than its index.
  2. Retrieve the prices for both the 'Dining table' and 'Dining chair' using their indices.

Tarea

Continuing from the previous exercise, you have a vector named prices with each price assigned to a corresponding item name.

  1. Retrieve the price of 'Armchair' using its name rather than its index.
  2. Retrieve the prices for both the 'Dining table' and 'Dining chair' using their indices.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

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

Indexing in Vectors

Great! Now you understand how to create a vector and assign names to its values.

To access specific elements within a vector, you can use indices. The index of the first element is 1, the second element is 2, and so on. To retrieve the n-th element from a vector vec, use the syntax vec[n]. Additionally, if the vector elements have assigned names, you can extract the values by their names by enclosing the name in quotes and square brackets.

Now, let’s revisit the grades example:

We'll extract the grade for Math and the grade for Literature.

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the first grade grades[1] # Output Literature grade grades['Literature']
copy

It's also possible to access multiple elements simultaneously:

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the second and the third grades grades[c(2,3)] # Output Math and Physics grades grades[c('Math','Physics')]
copy

Tarea

Continuing from the previous exercise, you have a vector named prices with each price assigned to a corresponding item name.

  1. Retrieve the price of 'Armchair' using its name rather than its index.
  2. Retrieve the prices for both the 'Dining table' and 'Dining chair' using their indices.

Tarea

Continuing from the previous exercise, you have a vector named prices with each price assigned to a corresponding item name.

  1. Retrieve the price of 'Armchair' using its name rather than its index.
  2. Retrieve the prices for both the 'Dining table' and 'Dining chair' using their indices.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

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

Indexing in Vectors

Great! Now you understand how to create a vector and assign names to its values.

To access specific elements within a vector, you can use indices. The index of the first element is 1, the second element is 2, and so on. To retrieve the n-th element from a vector vec, use the syntax vec[n]. Additionally, if the vector elements have assigned names, you can extract the values by their names by enclosing the name in quotes and square brackets.

Now, let’s revisit the grades example:

We'll extract the grade for Math and the grade for Literature.

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the first grade grades[1] # Output Literature grade grades['Literature']
copy

It's also possible to access multiple elements simultaneously:

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the second and the third grades grades[c(2,3)] # Output Math and Physics grades grades[c('Math','Physics')]
copy

Tarea

Continuing from the previous exercise, you have a vector named prices with each price assigned to a corresponding item name.

  1. Retrieve the price of 'Armchair' using its name rather than its index.
  2. Retrieve the prices for both the 'Dining table' and 'Dining chair' using their indices.

Tarea

Continuing from the previous exercise, you have a vector named prices with each price assigned to a corresponding item name.

  1. Retrieve the price of 'Armchair' using its name rather than its index.
  2. Retrieve the prices for both the 'Dining table' and 'Dining chair' using their indices.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Great! Now you understand how to create a vector and assign names to its values.

To access specific elements within a vector, you can use indices. The index of the first element is 1, the second element is 2, and so on. To retrieve the n-th element from a vector vec, use the syntax vec[n]. Additionally, if the vector elements have assigned names, you can extract the values by their names by enclosing the name in quotes and square brackets.

Now, let’s revisit the grades example:

We'll extract the grade for Math and the grade for Literature.

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the first grade grades[1] # Output Literature grade grades['Literature']
copy

It's also possible to access multiple elements simultaneously:

1234567
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Output the second and the third grades grades[c(2,3)] # Output Math and Physics grades grades[c('Math','Physics')]
copy

Tarea

Continuing from the previous exercise, you have a vector named prices with each price assigned to a corresponding item name.

  1. Retrieve the price of 'Armchair' using its name rather than its index.
  2. Retrieve the prices for both the 'Dining table' and 'Dining chair' using their indices.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 7
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt