Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Modifying Vector Elements | Section
Practice
Projects
Quizzes & Challenges
Quizer
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
Oppgave

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.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 18
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookModifying Vector Elements

Sveip for å vise menyen

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
Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 18
single

single

some-alt