11

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Bruce Brown
  • 217
  • 2
  • 4
  • 8
  • possible duplicate of [R: Insert a vector as a row in data.frame](http://stackoverflow.com/questions/3651198/r-insert-a-vector-as-a-row-in-data-frame) – Roland Apr 27 '13 at 08:43

4 Answers4

7

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,] )
vaettchen
  • 7,299
  • 22
  • 41
4

If order is an important feature of your dataset then you should codify it in a safe way, e.g., by using an index variable. I wouldn't rely on rownames or the order of the data.frame rows since there are operations where they are not preserved.

data <- read.table(text="Manufacturers       Models
1   Audi                RS5  
2   BMW                 M3  
3   Cadillac            CTS-V  
4   Lexus               ISF",header=TRUE)

data$ind <- seq_len(nrow(data))
data <- rbind(data,data.frame(Manufacturers = "Benz", Models = "C63",ind=3.1))
data <- data[order(data$ind),]

#   Manufacturers Models ind
# 1          Audi    RS5 1.0
# 2           BMW     M3 2.0
# 3      Cadillac  CTS-V 3.0
# 5          Benz    C63 3.1
# 4         Lexus    ISF 4.0
Roland
  • 127,288
  • 10
  • 191
  • 288
  • 3
    Point taken about row names, but a row names approach that is very similar to yours would be `rbind(data, "3.1" = data.frame(Manufacturers = "Benz", Models = "C63"))` (without creating your "ind" column). Of course, reordering is still required, but one step is saved :) – A5C1D2H2I1M1N2O1R2T1 Apr 27 '13 at 16:41
2

this function would improve and solve your problem:

INSERT_NA_ROW <- function(indice, tabla) {
  new_Row <- NA
  long <- NROW(tabla)
  new_Data<- rbind(tabla[1:indice,], new_Row ,tabla[(indice + 1):(long),])
  return(new_Data)
} # Insert Row in index of dataframe

Thanks for read me!

1

I used the @Sergio Mora answer to adapt and solve the question. I saw today the question and I needed a solution.

insert_row <- function(index, new_row, dat){
  if(is.matrix(dat) && ncol(dat) > 1){
    long <- NROW(dat)
    if(index == 1){
      new_data <- rbind(new_row, dat)  
    }
    else if(index == long){
      new_data <- rbind(dat, new_row)
    }
    else{
      new_data <- rbind(dat[1 : (index - 1), ], new_row, dat[index : long, ])
    }
    row.names(new_data) <- NULL
  }
  else if(ncol(dat) == 1 || is.vector(dat)){
    long <- length(dat)
    if(index == 1){
      new_data <- c(new_row, dat)  
    }
    else if(index == long){
      new_data <- c(dat, new_row)
    }
    else{
      new_data <- c(dat[1 : (index - 1)], new_row, dat[index : long])
    }
    row.names(new_data) <- NULL
  }
  return(new_data)
}
Wagner Jorge
  • 430
  • 3
  • 15