Skip to content

Instantly share code, notes, and snippets.

@nassimhaddad
Created February 16, 2013 19:10
Show Gist options
  • Save nassimhaddad/4968264 to your computer and use it in GitHub Desktop.
Save nassimhaddad/4968264 to your computer and use it in GitHub Desktop.
insert a row to a data frame at a wanted position
insert_row <- function(existingDF, newrow, r){
r <- as.numeric(r)
if (length(newrow) != ncol(existingDF)){
stop("Length of row doesn't match.")
}
if (r == 1){
existingDF <- rbind(newrow,existingDF)
} else{
existingDF <- rbind(existingDF[1:(r-1),],newrow,existingDF[-(1:(r-1)),])
}
return(existingDF)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment