i

R Programming Complete Tutorial

R Data Frames

Data frame

A data frame is used for storing data tables. Unlike an array, the data that we store in the data frame columns can be of different types. It means one column might be a numeric variable, another might be a factor, and a third might be a character variable. All columns have to be of the same length.

Data Frame construction

We can construct a data frame using data.frame() function, which creates data frames, tightly coupled collections of variables that share many of the properties of matrices and of lists used as the fundamental data structure by most of its modeling software.

In this example, we are taking four vectors of six elements as the data argument. The first and third are numeric vectors, the second one, cast name contains character vector, and the final one contains a character vector, which we convert as data. When we run this code, it produces a table-like structure with four columns and six rows.

When we create a data frame, we have to keep in mind that column names should be non-empty; each column should contain the same number of data items and can store different types of data.

Data frame Inspection:

We have already constructed a data frame with four columns and six rows. This str() function will display the structure of the data frame.This command will display the entire structure of bank data, data frame. In our data frame, we have six observations of four variables. Cust_id and balance contain numeric data, Cust_name contains character data, and last_trn contains date data.

Now, the summary function. It will help us to know the detail of each column. Our Cust_id contains numeric data, a minimum of them is 543, and the maximum is 1020. It also gives us the result of the median, mean, first, and the third quintile.The second column contains character data of length six. In case of balance and last_trn, it will provide the same statistical information like Cust_id.