Modifying 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
12345678grades <- 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
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
1234567grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades['Philosophy'] <- 60 grades
Updating Elements
You can also modify existing values either by name or by index.
Example
1234567grades <- 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
Swipe to start coding
- Add a new item named
'Desk'with a price of135to the end of thepricesvector using the second method (assigning the name while adding the value). - Update the price of the
'Bookshelf'to180. You can use either the index or the name to do this. - Display the modified vector
prices.
Løsning
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 2.27
Modifying 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
12345678grades <- 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
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
1234567grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Add new grade grades['Philosophy'] <- 60 grades
Updating Elements
You can also modify existing values either by name or by index.
Example
1234567grades <- 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
Swipe to start coding
- Add a new item named
'Desk'with a price of135to the end of thepricesvector using the second method (assigning the name while adding the value). - Update the price of the
'Bookshelf'to180. You can use either the index or the name to do this. - Display the modified vector
prices.
Løsning
Takk for tilbakemeldingene dine!
single