The following code combines a vector with a dataframe:
newrow = c(1:4)
existingDF = rbind(existingDF,newrow)
However this code always inserts the new row at the end of the dataframe.
How can I insert the row at a specified point within the dataframe? For example, lets say the dataframe has 20 rows, how can I insert the new row between rows 10 and 11?
Here’s a solution that avoids the (often slow)
rbindcall:If speed is less important than clarity, then @Simon’s solution works well:
(Note we index
rdifferently).And finally, benchmarks:
Benchmarks
As @MatthewDowle always points out to me, benchmarks need to be examined for the scaling as the size of the problem increases. Here we go then:
@Roland’s solution scales quite well, even with the call to
rbind:Plotted on a linear scale:
And a log-log scale: