I have two data frames (**df1** and **df2**) and I want to insert all consecutive rows in the correct order from df2 to df1, if they do not already exist in the current position. Column **Index** decides whether to insert or not, but this column is not unique, so it may appear multiple times in df1 or df2. I have some code, but it's extremely slow for much bigger dataframes, so I'd like to ask if there's a way to do it more efficiently. If you run my code, you'll see the expected result.
Thanks in advance for your help.

**df1 looks like:**

```
df1 <- data.frame(datetime=c("2016-03-02 16:44:32 UTC","2016-03-02 16:51:32 UTC","2016-03-02 16:53:45 UTC","2016-03-02 19:12:15 UTC",
                           "2016-03-02 19:12:32 UTC","2016-03-02 19:12:36 UTC","2016-03-02 19:13:50 UTC","2016-03-03 05:44:54 UTC",
                           "2016-03-03 05:45:06 UTC","2016-03-03 05:45:11 UTC","2016-03-03 05:45:27 UTC","2016-03-03 05:45:42 UTC",
                           "2016-03-03 05:45:52 UTC","2016-03-03 05:45:57 UTC","2016-03-03 05:46:12 UTC","2016-03-03 05:46:23 UTC",
                           "2016-03-03 05:46:29 UTC","2016-03-03 05:46:45 UTC","2016-03-03 05:47:03 UTC","2016-03-03 05:47:19 UTC",
                           "2016-03-03 05:47:37 UTC","2016-03-03 05:47:51 UTC","2016-03-03 05:47:56 UTC","2016-03-03 05:48:03 UTC",
                           "2016-03-03 05:48:04 UTC","2016-03-03 05:48:10 UTC","2016-03-03 05:48:18 UTC","2016-03-03 05:48:27 UTC",
                           "2016-03-03 05:48:45 UTC"),
                Index=c(rep(NA,7),"68362","68364","68364","68364","68352","427292","427292",
                        "427292","427292","255720","255720","255720","255720","255720","255721","255721","255721","255721","255721","255721","255721",
                        "255713"))

```

**df2 looks like:**

```
 df2 <- data.frame(Index=c("68362","68363","68364","68365","68352","68351","68373","68372","68371","427292","255720","255721","255713"))

```

**My code looks like:**

```
library(lubridate)

insertFromDF2 <- function(df_1, df_2) {
  #datetime is in wrong format
  df_1$datetime <- parse_date_time(df_1$datetime, orders="ymd HMS")
  #iteration index will be used to access df_1
  iteration <- 1
  #help var for storing seconds
  last_date <- 1
  temp <- data.frame()
  #list used to store all inserted rows
  l <- list()
  #list index will be used to access l
  l_index <- 1
  newR <- data.frame(datetime=NA,Index=NA)
  #loop through the df_2 and check if row with current Index exists in df_1
  for (i in 1:nrow(df_2)) { 
    #skip NA rows in df1
    while(is.na(df_1$Index[iteration])) {
      iteration <- iteration + 1
    }
    #skip if rows with current Index exist in df_1
    if(df_1$Index[iteration] == df_2$Index[i]) {
      while(df_1$Index[iteration] == df_2$Index[i] & iteration < nrow(df_1)) {
        iteration <- iteration + 1
      }
      last_date <- 1
      next
    }

    #I used datetime as a help column for correct order, I used time from the previous row and add 1 second
    newR$datetime <- df_1$datetime[iteration - 1] + lubridate::seconds(last_date)
    #copy columns from df_2 to new row, which will be inserted do df_1
    newR[2] <- df_2$Index[i]
    #increment help var for storing seconds, which will be used in next iteration
    last_date <- last_date + 1
    #store new row in list
    l[[l_index]] <- newR
    l_index <- l_index + 1
  }

  #at the end call rbind to combine list with df_1
  temp <- do.call("rbind", l)
  #highlight inserted rows
  temp$inserted <- 1
  df_1$inserted <- 0
  df_1 <- rbind(temp,df_1) 
  #order by datetime
  df_1 <- df_1[order(df_1$datetime),]

  return (df_1)   
}

#make some magic
df <- insertFromDF2(df1,df2)

```