Jackinthebox98
Jackinthebox98

Reputation: 3

How can I exclude pieces of data in R?

I am new to R studio and having a little trouble. I am trying to output unique counties form a data set. So far I have

sort(unique(x$Location) )

[1] Africa Asia Carlow Cavan Cork Dublin Europe Galway

[9] Kildare Laois Louth Mayo Meath

13 Levels: Africa Asia Carlow Cavan Cork Dublin Europe ... Meath

I want that list but without "Africa", "Asia" and "Europe" What function should I use to remove them?

Upvotes: 0

Views: 412

Answers (3)

Pedro Cavalcante
Pedro Cavalcante

Reputation: 444

library(dplyr)

(x %>%
  filter(!Location %in% c("Africa", "Asia", "Europe")) %>%
  pull(Location) %>%
  unique() ->
  locations)

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388817

Since you have factor variable you can use levels to get unique levels and then remove c('Africa', 'Asia', 'Europe') from it using setdiff.

lvls <- setdiff(levels(x$Location), c('Africa', 'Asia', 'Europe'))

Upvotes: 2

hachiko
hachiko

Reputation: 747

i would do this:

library(dplyr)

x <- x %>%
filter(Location != "Africa", Location != "Asia", Location != "Europe")

sort(unique(x$Location))

Upvotes: 1

Related Questions