How to do Modification in Dataframe?

Modification in Data Frame

R allows us to do modification in our data frame. Like matrices modification, we can modify our data frame through re-assignment. We cannot only add rows and columns, but also we can delete them. The data frame is expanded by adding rows and columns.

We can

  1. Add a column by adding a column vector with the help of a new column name using cbind() function.
  2. Add rows by adding new rows in the same structure as the existing data frame and using rbind() function
  3. Delete the columns by assigning a NULL value to them.
  4. Delete the rows by re-assignment to them.

Let’s see an example to understand how rbind() function works and how the modification is done in our data frame.

Example: Adding rows and columns

# 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  
)  
print(emp.data)  
  
#Adding row in the data frame  
x <- list(6,"Vaishali",547,"2015-09-01")  
rbind(emp.data,x)  
  
#Adding column in the data frame  
y <- c("Moradabad","Lucknow","Etah","Sambhal","Khurja")  
cbind(emp.data,Address=y)  

Output

 employee_id  employee_name    sal          starting_date
1       1              Shubham    623.30          2012-01-01
2       2              Arpita     515.20          2013-09-23
3       3              Nishka     611.00          2014-11-15
4       4              Gunjan     729.00          2014-05-11
5       5              Sumit      843.25          2015-03-27
 employee_id  employee_name     sal        starting_date
1       1              Shubham     623.30          2012-01-01
2       2              Arpita      515.20          2013-09-23
3       3              Nishka      611.00          2014-11-15
4       4              Gunjan      729.00          2014-05-11
5       5              Sumit       843.25          2015-03-27
6       6              Vaishali    547.00          2015-09-01
 employee_id     employee_name    sal        starting_date        Address
1       1              Shubham       623.30        2012-01-01        Moradabad
2       2              Arpita        515.20        2013-09-23        Lucknow
3       3              Nishka        611.00        2014-11-15        Etah
4       4              Gunjan        729.00        2014-05-11        Sambhal
5       5              Sumit         843.25        2015-03-27        Khurja

Example: Delete rows and columns

# 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  
)  
print(emp.data)  
  
#Delete rows from data frame  
emp.data<-emp.data[-1,]  
print(emp.data)  
  
#Delete column from the data frame  
emp.data$starting_date<-NULL  
print(emp.data)  

Output

employee_idemployee_namesalstarting_date
1           1       Shubham623.30    2012-01-01
2           2        Arpita515.20    2013-09-23
3           3        Nishka611.00    2014-11-15
4           4        Gunjan729.00    2014-05-11
5           5         Sumit843.25    2015-03-27
employee_idemployee_namesalstarting_date
2           2        Arpita515.20    2013-09-23
3           3        Nishka611.00    2014-11-15
4           4        Gunjan729.00    2014-05-11
5           5         Sumit843.25    2015-03-27
employee_idemployee_namesal
1           1       Shubham623.30    
2           2        Arpita515.20    
3         3        Nishka611.00    
4           4        Gunjan729.00    
5           5         Sumit843.25