Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to turn a vector into a set in r

Tags:

r

I would like to see a list of all the possible values, without repetition, in a column of a data frame. Something like:

as.set(series["begin_year"][,1])

for the column "begin_year" although as.set doesn't exist.

like image 745
Alex Avatar asked Oct 31 '11 12:10

Alex


2 Answers

unique() [or levels(), if the column is a factor].

Here's the reproducible example:

dat <- OrchardSprays
dat$rowpos
unique(dat$rowpos)
dat$treatment
unique(dat$treatment)
levels(dat$treatment) 

EDIT Note that levels() will return unique levels of the factor, even if the level is unused. Consider:

dat2 <- subset(dat, treatment != "A")
unique(dat2$treatment)
# [1] D E B H G F C
# Levels: A B C D E F G H
levels(dat2$treatment) 
# [1] "A" "B" "C" "D" "E" "F" "G" "H"

You can get rid of the unused levels with droplevels():

dat2$treatment <- droplevels(dat2$treatment)
levels(dat2$treatment)
# [1] "B" "C" "D" "E" "F" "G" "H"
like image 113
jthetzel Avatar answered Sep 22 '22 22:09

jthetzel


The unique function should do this, and there's also a few other set-related functions: union, intersect, setdiff, setequal and is.element that are documented on the help(union) page.

like image 26
Spacedman Avatar answered Sep 18 '22 22:09

Spacedman