Data Frames

Data frames are used to store tabular data in R. They are an important type of object in R and are used in a variety of statistical modeling applications.Data frames are represented as a special type of list where every element of the list has to have the same length. Each element of the list can be thought of as a column and the length of each element of the list is the number of rows. Unlike matrices, data frames can store different classes of objects in each column. Matrices must have every element be the same class (e.g. all integers or all numeric).

Data frames can be created explicitly with the data.frame() function.

employee <- c('Ram','Sham','Jadu')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2016-11-1','2015-3-25','2017-3-14'))
employ_data <- data.frame(employee, salary, startdate)


> employ_data
employee salary startdate
1 Ram 21000 2016-11-01
2 Sham 23400 2015-03-25
3 Jadu 26800 2017-03-14
> View(employ_data)

If you look at the structure of the data frame now, you see that the variable employee is a character vector, as shown in the following output:



> str(employ_data)
'data.frame': 3 obs. of 3 variables:
$ employee : Factor w/ 3 levels "Jadu","Ram","Sham": 2 3 1
$ salary : num 21000 23400 26800
$ startdate: Date, format: "2016-11-01" "2015-03-25" "2017-03-14"

You can try some other functions like dim() to see the dimension. nrow() and ncol() function will help you  to know number of row and column in the data frame.



> dim(employ_data)
[1] 3 3
> nrow(employ_data)
[1] 3
> ncol(employ_data)
[1] 3


Matrices, Lists, Factors

Control Structures