In this tutorial you will learn about the R Data Frames and its application with practical example.
R Data Frames
Data Frame is a data structure that contain list of vectors that is of equal length. Vectors in a data frame can be different data types i.e. numeric, character, factor, etc. It is a two dimensional data structure that us mainly used to store data tables.
Example:-
1 2 3 4 5 6 |
print("W3Adda - R Data Frame") a <- c(2, 4, 6) b <- c("aaa", "bbb", "ccc") c <- c(TRUE, FALSE, TRUE) df <- data.frame(a, b, c) # df is a data frame print(df) |
Here, df is a data frame containing three vectors a, b, c. When we run the above R script, we see the following output –
Output:-
In Data Frame, each of the component form column and its contents form the rows.
Creating a Data Frame
In R, a data frame is created using data.frame() function. The data.frame() function takes a vector input in order to create a factor.
Syntax:-
1 |
data.frame(df, stringsAsFactors = TRUE) |
Above is the general syntax of data.frame() function, here
df:- It is collection of vector input that us being join as data frame, or it could be a matrix that can be converted to data frame.
stringsAsFactors:- It convert string to factor by default. To suppress this behavior, we can pass assign FALSE to it.
Let’s create a data frame using data.frame() function –
Example:-
1 2 3 4 5 6 |
print("W3Adda - R Data Frame") a <- c(2, 4, 6) b <- c("aaa", "bbb", "ccc") c <- c(TRUE, FALSE, TRUE) df <- data.frame(a, b, c) # df is a data frame print(df) |
Here, df is a data frame containing three vectors a, b, c. When we run the above R script, we see the following output –
Output:-
Accessing factor Elements
Elements in a data frame can be accessed same way as of list or matrix by passing row and column index value(s) in brackets [ ] separated with a comma you can access individual data frame element. However, individual column can be accessed using [[
or $
operator.
Syntax:-
1 |
df[row,col] |
df:- Data frame name
row:- row index of the element, if not provided specified row elements will be fetched for all columns.
col:- column index of the element, if not provided specified column elements will be fetched for all rows.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
print("W3Adda - R Accessing Data Frame") # Create the data frame. emp <- data.frame( empID = c (1:5), empName = c("John","Alex","Steve","Keith","Murphy"), empSalary = c(5000,2000,3000,5000,5000), stringsAsFactors = FALSE ) print(emp) # Access Specific columns. A <- data.frame(emp$empName) print(A) # Access the element at 1st row and 3rd column. print(emp[1,3]) # Access the element at 2nd row and 5th column. print(emp[2,2]) # Access only the 1st row. print(emp[1,]) # Access only the 5th column. print(emp[,2]) |
When we run the above R script, we see the following output –
Output:-
Modify Data frame Element
A data frame element can be modified by accessing element using above method and assigning it new value.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
print("W3Adda - R Modify Data Frame") # Create the data frame. emp <- data.frame( empID = c (1:5), empName = c("John","Alex","Steve","Keith","Murphy"), empSalary = c(5000,2000,3000,5000,5000), stringsAsFactors = FALSE ) print("Before Update") print(emp) print("After Update") emp[4,3] <- 4000 print(emp) |
Output:-
Data Frame rbind() and cbind() Function
The rbind()
and cbind()
function is used to add a row and column respectively to a data frame.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
print("W3Adda - R Data Frame rbind and cbind") # Create the data frame. emp <- data.frame( empID = c (1:5), empName = c("John","Alex","Steve","Keith","Murphy"), empSalary = c(5000,2000,3000,5000,5000), stringsAsFactors = FALSE ) print(emp) print("Data Frame Add column using cbind") cbind(emp, "empCode" = c("emp001", "emp002", "emp003", "emp004", "emp005")) # add column print("Data Frame Add Row using rbind") rbind(emp, c(6, "Chris", 3500)) # add row |
Output:-
Delete Data frame Element
A data frame element can be deleted by simply accessing and assigning NULL value to it.