Add a new row in specific place in a dataframe

Bruce Brown picture Bruce Brown · Apr 27, 2013 · Viewed 33.1k times · Source

Heres my data:

 > data
  Manufacturers       Models
1   Audi                RS5  
2   BMW                 M3  
3   Cadillac            CTS-V  
4   Lexus               ISF

I would like to add 1 row in the fourth row, like this:

 > data
  Manufacturers       Models
1   Audi                RS5  
2   BMW                 M3  
3   Cadillac            CTS-V  
4   Benz                C63
5   Lexus               ISF

I have tried to use the rbind() like this:

 Benz = data.frame(Manufacturers = "Benz", Models = "C63")
 newdata = rbind(data,Benz)

But I cannot add to the place I want. I would appreciate any help on this question. Thanks a lot.

Answer

vaettchen picture vaettchen · Apr 27, 2013

In case you don't want the index but rather a one-off "quick fix" for some spreadsheet-like appearance, you might resort to

newData <- rbind( data[1:3,], Benz, data[ 4,] )