Extracting data from Data Frame
The data of the data frame is very crucial for us. To manipulate the data of the data frame, it is essential to extract it from the data frame. We can extract the data in three ways which are as follows:
- We can extract the specific columns from a data frame using the column name.
- We can extract the specific rows also from a data frame.
- We can extract the specific rows corresponding to specific columns.
Let’s see an example of each one to understand how data is extracted from the data frame with the help these ways.
Extracting the specific columns from a data frame
Example
# Creating the data frame. emp.data<- data.frame( employee_id = c (1:5), employee_name= c("Shubham","Arpita","Nishka","Gunjan","Sumit"), sal = c(623.3,515.2,611.0,729.0,843.25), starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11", "2015-03-27")), stringsAsFactors = FALSE ) # Extracting specific columns from a data frame final <- data.frame(emp.data$employee_id,emp.data$sal) print(final)
Output
emp.data.employee_idemp.data.sal 1 1 623.30 2 2 515.20 3 3 611.00 4 4 729.00 5 5 843.25
Extracting the specific rows from a data frame
Example
# Creating the data frame. emp.data<- data.frame( employee_id = c (1:5), employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"), sal = c(623.3,515.2,611.0,729.0,843.25), starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11", "2015-03-27")), stringsAsFactors = FALSE ) # Extracting first row from a data frame final <- emp.data[1,] print(final) # Extracting last two row from a data frame final <- emp.data[4:5,] print(final)
Output
employee_id employee_name sal starting_date
1 1 Shubham 623.3 2012-01-01
employee_id employee_name sal starting_date
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27
Extracting specific rows corresponding to specific columns
Example
# Creating the data frame. emp.data<- data.frame( employee_id = c (1:5), employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"), sal = c(623.3,515.2,611.0,729.0,843.25), starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11", "2015-03-27")), stringsAsFactors = FALSE ) # Extracting 2nd and 3rd row corresponding to the 1st and 4th column final <- emp.data[c(2,3),c(1,4)] print(final)
Output
employee_id starting_date 2 2 2013-09-23 3 3 2014-11-15