Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does R have a Set data structure?

I am a beginner R learner, so bear with me if I say something incoherent.

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?

like image 357
Acsor Avatar asked Jul 21 '17 10:07

Acsor


People also ask

How many data structures are there in R?

Introduction to Data Structures in R. R has six types of basic data structures. We can organize these data structures according to their dimensions(1d, 2d, nd). We can also classify them as homogeneous or heterogeneous (can their contents be of different types or not).

What is set in R?

Sets define a 'collection' of objects, or things typically referred to as 'elements' or 'members. ' The concept of sets arises naturally when dealing with any collection of objects, whether it be a group of numbers or anything else.

Which data structure is used in set?

A set is a data structure that stores unique elements of the same type in a sorted order. Each value is a key, which means that we access each value using the value itself. With arrays, on the other hand, we access each value by its position in the container (the index). Accordingly, each value in a set must be unique.

Is data frame a data structure in R?

Data frame is a two dimensional data structure in R. It is a special case of a list which has each component of equal length.


2 Answers

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 
like image 193
loki Avatar answered Oct 26 '22 06:10

loki


unique() function will work.

unique("name of vector")

like image 39
PritamJ Avatar answered Oct 26 '22 08:10

PritamJ