Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Modifying Vector Elements | Section
Practice
Projects
Quizzes & Challenges
Questionários
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

bookModifying Vector Elements

Vectors can be modified by adding new elements or updating existing ones. This is useful when the data structure needs to grow or when values need to be corrected.

Adding Elements with Functions

Use the c() function or the append() function to add a new value to a vector. If the vector is named, you can then assign a label to the new element.

Example

12345678
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades <- c(grades, 60) names(grades)[length(grades)] <- 'Philosophy' grades
copy

Adding Elements with Names

If the vector already has names, you can add a new element by assigning a value directly to a new name.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades['Philosophy'] <- 60 grades
copy

Updating Elements

You can also modify existing values either by name or by index.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Update second grade grades[2] <- 60 # Update Math grade by name grades["Math"] <- 100
copy
Tarefa

Swipe to start coding

  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.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 18
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookModifying Vector Elements

Deslize para mostrar o menu

Vectors can be modified by adding new elements or updating existing ones. This is useful when the data structure needs to grow or when values need to be corrected.

Adding Elements with Functions

Use the c() function or the append() function to add a new value to a vector. If the vector is named, you can then assign a label to the new element.

Example

12345678
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades <- c(grades, 60) names(grades)[length(grades)] <- 'Philosophy' grades
copy

Adding Elements with Names

If the vector already has names, you can add a new element by assigning a value directly to a new name.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades['Philosophy'] <- 60 grades
copy

Updating Elements

You can also modify existing values either by name or by index.

Example

1234567
grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Update second grade grades[2] <- 60 # Update Math grade by name grades["Math"] <- 100
copy
Tarefa

Swipe to start coding

  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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 18
single

single

some-alt