Susan Switzer
Susan Switzer

Reputation: 1922

Subset vector: remove one element, based on value rather than position

library(tidyverse)

view first 15 records in starwars dataset

head(starwars, 15)

remove the 10th record ("Obi-Wan Kenobi"), based on position in the vector "Obi-Wan Kenobi" is in the 10th position

starwarsNames <- unique(starwars$name)[-10]

remove the 10th record ("Obi-Wan Kenobi"), based on value in the vector I see Error in -"Obi-Wan Kenobi" : invalid argument to unary operator when I attempt the following:

starwarsNames <- unique(starwars$name)[-"Obi-Wan Kenobi"]

Upvotes: 1

Views: 61

Answers (1)

akrun
akrun

Reputation: 887118

We can use setdiff

setdiff(unique(starwars$name), "Obi-Wan Kenobi")

Or another option is logical

un1  <- unique(starwars$name)
un1[un1 != "Obi-Wan Kenobi"]

Upvotes: 1

Related Questions