Модифікація
Чудово! Тепер ви знаєте, як створити вектор, назвати його значення та витягти елементи з нього. Наступним кроком, який ми розглянемо, буде зміна вектора шляхом додавання нових елементів до нього або видалення існуючих.
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
Наприклад, застосуємо ці методи, використовуючи приклад із оцінками, додавши нову оцінку 60
для предмету 'Філософія'
. Перший метод полягає у використанні векторів:
12345678# 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
Тепер спробуємо другий метод, де ми присвоюємо ім'я значенню, коли додаємо його.
Swipe to start coding
- Add a new item named
'Desk'
with a price of135
to the end of theprices
vector 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
.
Рішення
Дякуємо за ваш відгук!
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
How do I remove an element from a vector?
Can I add multiple new values to a vector at once?
What happens if I add a value with a name that already exists?
Awesome!
Completion rate improved to 2.27
Модифікація
Свайпніть щоб показати меню
Чудово! Тепер ви знаєте, як створити вектор, назвати його значення та витягти елементи з нього. Наступним кроком, який ми розглянемо, буде зміна вектора шляхом додавання нових елементів до нього або видалення існуючих.
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
Наприклад, застосуємо ці методи, використовуючи приклад із оцінками, додавши нову оцінку 60
для предмету 'Філософія'
. Перший метод полягає у використанні векторів:
12345678# 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
Тепер спробуємо другий метод, де ми присвоюємо ім'я значенню, коли додаємо його.
Swipe to start coding
- Add a new item named
'Desk'
with a price of135
to the end of theprices
vector 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
.
Рішення
Дякуємо за ваш відгук!
Awesome!
Completion rate improved to 2.27single