It is easier to work with vectors of ranges to keep instead of ranges to cut. This is pretty easy to do by flipping the starts and ends and adjusting slightly:
hw <- "Hadley WickhamPLUSENDING"
cuts <- data.frame(start=c(1,8), end=c(6,14))
keeps <- data.frame(start=c(1, cuts$end+1), end=c(cuts$start-1, nchar(hw)))
keeps
# start end
# 1 1 0
# 2 7 7
# 3 15 24
Substrings that start after they end will simply return no characters, so they are not an issue with our method.
You can use apply
to vectorize the operation of keeping everything between each start/end pair:
pieces <- apply(keeps, 1, function(x) substr(hw, x[1], x[2]))
pieces
# [1] "" " " "PLUSENDING"
paste(pieces, collapse="")
# [1] " PLUSENDING"