R Data Frames

A Data Frame is a list of vectors of equal length. It has two dimensional array like structure, in which the number of elements should be same in each column and the row names should be unique.

Creating a Data Frames

A data frame is created by data.frame() function. Here is a simple example of a data frame.

df <- data.frame(id = c(1:4), name = c("Mary", "Soy", "Alexa", "Roxy"), class = 5)
print(df)
  id  name class
1  1  Mary     5
2  2   Soy     5
3  3 Alexa     5
4  4  Roxy     5

In a data frame, each column can have different types than the other columns, but all the elements of a column should have same data type.


Get Data Frame Structure

R provides str() function to get the structure of the data frame.

data <- data.frame(id = c(1:4), name = c("Mary", "Soy", "Alexa", "Roxy"), class = 5)
str(data)
'data.frame':   4 obs. of  3 variables:
 $ id   : int  1 2 3 4
 $ name : Factor w/ 4 levels "Alexa","Mary",..: 2 4 1 3
 $ class: num  5 5 5 5




Access Data Frame Elements

The following code extracts the name and age columns of the data frame.

data <- data.frame(id=c(1:4),
+ name=c('Mary','Soy','Alexa','Roxy'),
+ age=c(7,8,7,9),
+ class=5)
> data
  id  name age class
1  1  Mary   7     5
2  2   Soy   8     5
3  3 Alexa   7     5
4  4  Roxy   9     5

result <- data.frame(data$name,data$age)
result
  data.name data.age
1      Mary        7
2       Soy        8
3     Alexa        7
4      Roxy        9

Get Length of a Data Frame

The length() function is used to get the length of a data frame.

data <- data.frame(id = c(1:4), name = c("Mary", "Soy", "Alexa", "Roxy"), age = c(7, 8, 7, 7), class = 5)
length(data)
[1] 4




Add Element to Data Frame

Add New Column

We can add a new column to the data frame either by assigning the new value to a data frame index (increase one to the last index) or by giving a name to the new index, like in the below code, we have added a new element name 'section' in the existing data frame.

data <- data.frame(id = c(1:4), name = c("Mary", "Soy", "Alexa", "Roxy"), age = c(7, 8, 7, 7), class = 5)

#Add new element 'section'
data$section <- c('A','B','A','C')
print(data)

Add New Row

R provides rbind() function to add new rows in the existing data frame. For this, we have to create a second data frame and merge to the existing one. In the below example, we have an existing data frame 'data1' and a new data frame 'data2'. We merge the second data frame to the first one with the help of rbind() function.

data1 <- data.frame(id = c(1:4),name = c("Mary", "Soy", "Alexa", "Roxy"),age = c(7, 8, 7, 7),class = 5, c('A','B','A','C'))
# New data frame
data2 <- data.frame(id=5,name="Smith",age=9,class=6,section='B')

# Bind the data frames
data <- rbind(data1,data2)
print(data)