Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Видалення Елементів | Вектори
Вступ до R: Частина 1

book
Видалення Елементів

Оскільки ви навчились додавати елементи до вектора, логічно також навчитися їх видаляти. Все йде до цього.

# Vector of grades and names
grades <- c(80, 75, 95, 100)
names(grades) <- c('Math', 'Physics', 'English', 'Literature')
# Remove the third element
grades[-3]
12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the third element grades[-3]
copy

Similarly, to eliminate the second and fourth elements from the initial grades vector, you would proceed as follows:

# Vector of grades and names
grades <- c(80, 75, 95, 100)
names(grades) <- c('Math', 'Physics', 'English', 'Literature')
# Remove the second and fourth elements
grades[-c(2,4)]
12345
# Vector of grades and names grades <- c(80, 75, 95, 100) names(grades) <- c('Math', 'Physics', 'English', 'Literature') # Remove the second and fourth elements grades[-c(2,4)]
copy
Завдання

Swipe to start coding

From the previous task, you have the prices vector with the prices of 6 items. Your task is:

  1. Remove the 'Dining chair' item from the prices vector. This item is the fourth in the vector. Save the changes by reassignment.
  2. Display the modified prices vector.

Рішення

# Vectors of prices and names
prices <- c(340, 150, 115, 45, 180, 135)
items <- c('Sofa', 'Armchair', 'Dining table', 'Dining chair', 'Bookshelf', 'Desk')
names(prices) <- items
# Remove the Dining chair price
prices <- prices[-4]
# Output modified vector
prices
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 9
# Vectors of prices and names
prices <- c(340, 150, 115, 45, 180, 135)
items <- c('Sofa', 'Armchair', 'Dining table', 'Dining chair', 'Bookshelf', 'Desk')
names(prices) <- items
# Remove the Dining chair price
___ <- ___[___]
# Output modified vector
___

Запитати АІ

expand
ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt