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.