Acsor
Acsor

Reputation: 1061

Does R have a Set data structure?

I have a large vector variable holding exactly 5000 elements, and would like to know what these are, knowing that there are several repetitions. An introduction to R doesn't seem to say anything besides basic data structures, and I don't know if R offers this feature as built-in.

If there is no such data structure, is there some built-in function to filter out repeated elements in a vector or list?

Upvotes: 13

Views: 15475

Answers (3)

PritamJ
PritamJ

Reputation: 377

unique() function will work.

unique("name of vector")

Upvotes: 3

Felix Jassler
Felix Jassler

Reputation: 1531

I'd also recommend looking into the sets library. Install it with install.packages('sets') and see, if the following works for you.

sets::as.set(c(1, 1, 2, 4, 3, 5, 5, 5))
# output: {1, 2, 3, 4, 5}

Upvotes: 3

loki
loki

Reputation: 10350

To remove multiple occurrences of a value within a vector use duplicated()

an example would be

x <- c(1,2,3,3,4,5,5,6)
x[!duplicated(x)]
# [1] 1 2 3 4 5 6

This is returning all values of x which are not (!) duplicated.

This will also work for more complex data structures like data.frames. See ?duplicated for further information.

unique(x) provides all values occurring in the vector.

table(x) shows the unqiue values and their number of occurrences in vector x

table(x)
# x
# 1 2 3 4 5 6 
# 1 1 2 1 2 1 

Upvotes: 7

Related Questions