# Data Structures in R Programming

Instructor: Alexis Kypridemos

Alexis is a technical writer for an IT company and has worked in publishing as a writer, editor and web designer. He has a BA in Communication.

Data structures in R programming are explained as the elements used for storing multiple types of data. The basic data structures (list, data frame, vector, matrix, and factor) are discussed with examples.

## What are Data Structures?

Data structures in R programming are tools for holding multiple values. The basic data structures used in R include vectors, lists, matrices, data frames, and factors. Data structures are used to handle multiple values, which means that only rarely do we work with data using single values, such as a single number, like 23, or a single word or phrase, like ''twenty-three''. More often, we need to group multiple numbers, multiple words or even multiple values of different types. This is what data structures are for. They help us group these multiple values, making it easier for us to work with them.

Data structures are made up of different data types. A data type defines what kind of data is held in a value. The number 23 has a numeric data type, for example, and the word ''twenty-three'' has a character type. Remember to use double quotes ('') around character data (words or phrases). This type of data is often called 'string' in computer programming.

Now let's have a look at the different data structures.

## Types of Data Structures

### Vectors

Vectors are used to group together multiple values of the same data type, such as a range of numbers. Vectors can be created using the c() function, like this:

`thisVector <- c(1:60)`

Running the above code will create the vector named 'thisVector', containing 60 numbers, from 1 through 60.

Vectors can also hold character values, although each of those values would have to be explicitly stated using double quotes like so:

`characterVector <- c(''one'', ''two'', ''three'')`

Technically it's possible to store data of different types in a vector, but it's not advisable, as all the values are likely to be converted to the character type.

### Lists

Now, let's look at lists. Lists are very similar to vectors, but with the added bonus that they can store values of any data type. So, for example, you can have a list that contains 45 numbers, 26 words or phrases, and 33 vectors.

Create lists using the list() function, like this:

`myList <- list(1, 2, 3, ''one'', '''two'', ''three'', numericVector, characterVector, exampleDataFrame)`

The code above creates the list named 'myList', and stores into it 9 items: 3 numeric values, 3 character values, 2 vectors, and a data frame. You can even store a list inside another list.

### Data Frames

Data frames store data in two dimensions, rows and columns, much like a spreadsheet. Like lists, they can hold values of different data types, but each column in the data frame must hold values of the same type. For example, a single column can't hold both words and numbers. Also, each column must hold the same number of values. So, if column one has 10 values, column two must also have 10 values, and so on.

The following code creates 3 different vectors named 'monthID', 'monthName' and 'temperature', and copies them into the data frame named 'myTable'.

`monthID <- c(1:12)monthName <- c(''Jan'', ''Feb'', ''Mar'', ''Apr'', ''May'', ''Jun'', ''Jul'', ''Aug'', ''Sep'', ''Oct'', ''Nov'', ''Dec'')temperature <- c(3, 3, 7, 10, 14, 18, 21, 21, 18, 12, 7, 4)myTable <- data.frame(monthID, monthName, temperature)`

Running the above code will result in a data frame with 3 columns and 12 rows.

Columns 'monthID' and 'temperature' hold numeric values, while 'monthName' holds character values.

### Matrices

A matrix is a data structure that is something between a vector and a data frame. Like a vector, it can hold values of only one data type. But, like a data frame, it can store and display that data in tabular format, columns, and rows, like a spreadsheet.

Here's how to create a matrix, using the matrix() function:

`myMatrix <-matrix(1:60, ncol=3)`

The above code will create a matrix named 'myMatrix' with three columns and 20 rows because 60/3 = 20.

To unlock this lesson you must be a Study.com Member.

### Register to view this lesson

Are you a student or a teacher?

#### See for yourself why 30 million people use Study.com

##### Become a Study.com member and start learning now.
Back
What teachers are saying about Study.com

### Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.