I have a data set which measures a parameter every 40 seconds. There are, however, gaps in the data. A small example of how time data look is below.

I would like to insert an element in between two consecutive points if the difference between those two points is > 40. This will help me later in plotting my data as I want to introduce NAs to break lines in geom_path for data points that should not connect. Other solutions posted have not been fruitful. Below is what I would like the final vector to look like.

I created a function which returns the number I am interested in, but cannot figure out how to insert it into the original vector.

I understand my for loop is where my issue is, but I am stuck after multiple attempts. I am looking to stick to baseR/tidyverse to do this in an efficient way (so a for loop / function is not necessary if there is a better way to accomplish this).

# Consider this vector
    testV1 <- c(30, 70, 110, 150, 250, 290, 330, 370, 560, 600, 640, 680, 900, 940)

# So my desired output vector is
    desired <- c(30, 70, 110, 150, 200, 250, 290, 330, 370, 465, 560, 600, 640, 680, 790, 900, 940)

# Returns a number that is the either the number itself or the midpoint between two numbers if the difference is > 40
    testF1 <- function(i){
              ifelse(testV1[i + 1] - testV1[i] < 40.1, 
                testV1[i], 
                (testV1[i+1] + testV1[i])/2)
    }

# Desired output when i = 4 is correct, but how to append this to the original vector?
    testR1 <- testF1(4)

# Seeing if I will get correct True or False values for an ifelse statement
# works
    testF1(3) == testV1[3] # True, works
    testF1(4) == testV1[4] # False, works

# Here is my attempted and for loop.
    testv2 <- c()

    for(i in 1:length(testV1)){
      ifelse(testF1(i) == testV1[i], # if the function returns the input value,
            testv2[i] <- testF1(i),  # put the input value in the i'th spot
            testv2[i + 1] <- testF1(i)) # else put it in the i'th + 1 spot
    }
0

There are 0 best solutions below