Data frames in R Programming With Examples

Let’s continue in our R programming tutorial series, and understand data frames in R. If you have ever handled data in databases, you will be familiar with the idea of records. Records are nothing but a collection of variables. For example, a student record could contain the student’s roll number, name, age, and gender altogether in one observation termed as the record. Database management systems store collections of such records as a table. R has a structure similar to such tables. These are known as data frames.

Unlike matrices or vectors, data frames have no restriction on the data type of variables. Each data frame can be a collection of numeric, strings, factors and so on. The only rule for writing a data frame in R is that all the records must be of the same length. Data frames in R are equipped with several functions and capabilities to handle large amounts of data for statistical processing purposes. Let us get started with data frames.

Creating a Data frame in R Programming

A data frame can be created using the data.frame() function in R. This function can take any number of equal length vectors as arguments, along with one optional argument stringsAsFactors. We will discuss about this shortly. The following is an example of a simple data frame creation.

Notice how the data frame accommodates both the string and associated integer together in its structure. It is possible to have any number of columns like this for a data frame. R also provides a unique index number to each record in the data frame as shown.

The argument stringsAsFactors is set to FALSE. Otherwise, the R compiler would treat each name as a specific categorical variable as we have seen in the factors tutorial earlier.

Accessing Records from Data Frames in R Language

The components of data frames can be accessed via the index numbers or the column names. The indexing of columns is done using a double square brace symbol [[ ]]. When you access the columns using the names you need to precede the name by a dollar sign $.

When you wish to access data at a specific location, such as 2nd item on the 4th column, it can be done by a matrix indexing like notation. Let us look at an example.

You can verify the size of your data frame using the ncol, nrow and dim functions.

Extending Data Frames in R

Often, real-time data is dynamic in nature. The structure of data keeps changing as new variables get added. The length of the data keeps changing as more observations are made. To accommodate these, R provides means to add and remove both rows and columns to data frames.

Let us try adding a new record to the above created emp data frame. To do this, we first need to create the records to be added as a data frame separately. Say we need to add a single record.

Now we add this row to the already created emp dataframe as follows.

Adding columns to the data frame can be done by using a cbind() function instead.

Subsetting Dataframes

You might be familiar with SQL (Sequential Query Language) used to query tables in databases using some logical conditions. R offers similar capabilities to query the data frames and generate logical subsets of larger data frames.

Suppose that I wish to extract all the data records from the emp frame that belong to male employees. I can do that using the following line of code.

The part emp$sex=='M‘ gives a Boolean vector of whether or not the value of sex is M for a particular row. We use the same logical Boolean vector to index upon the emp frame. The comma that follows is necessary to specify matrix-like indexing. The part before comma represents the rows and the one after is columns. Since we left it blank, we simply select all the columns. We could also choose to display only the names and sex instead.

Instead of supplying the index number of columns, you can also give the names of the columns like below.

You could also add logical conditions in indexing your data frames. Suppose you wish to extract the records of all the female employees with a salary greater than 12000. You can do that as follows.

Useful Data Frame Functions

Apart from the utilities listed above, there are some handy functions you may need while handling data frames. This section lists a few of them.

Sorting a Data Frame

Sorting can be done using an order function in the indexing.

head() and tail() functions

These are used to get the first few or last few rows of a dataframe respectively. These are especially useful when you have a huge dataset. They allow you to examine the characteristics of data without having to clog memory by displaying the entire dataset.

By default, the head and tail functions fetch 6 rows without any number specified.

Merging two Data Frames

Merging data frames is similar to performing database joins on tables. When there is more information available regarding one column of the data frame in a separate data frame, we can easily merge these two using the common column. For example, consider that we have the marital status of some of the employees available as below.

We can now merge this with our emp data frame to get the combined information.

Even when we have no information about some employees, the data frame still gets populated with NA values to ensure a smooth merge.

By admin

Leave a Reply

%d bloggers like this: