简体   繁体   中英

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?

由于您有因子变量,您可以使用levels来获得唯一级别,然后使用setdiff删除c('Africa', 'Asia', 'Europe')

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

i would do this:

library(dplyr)

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

sort(unique(x$Location))
library(dplyr)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM