I want to remove all rows between "5. Demand Disputed" and "Total Demand Disputed" from their respective columns. I have tried
grepl
gsub
but not able to achieve the desire output.Kindly guide.
I want to remove all rows between "5. Demand Disputed" and "Total Demand Disputed" from their respective columns. I have tried
grepl
gsub
but not able to achieve the desire output.Kindly guide.
Use grep to create an index vector between the two lines.
x[-c(grep("5. Demand Disputed", x$V1) : grep("Total Demand Disputed", x$V1), ]
Explanation
grep
" returns a vector of the indices of the elements of x that yielded a match" (?grep
)
So, you can simply create an integer vector between the two lines that match the two strings by :
.
Using a toy example...
df <- data.frame(a=LETTERS[1:10],b=LETTERS[3:12],stringsAsFactors = FALSE)
limits <- c("E","H")
sapply(df,function(x){
del.min <- grep(limits[1],x)
del.max <- grep(limits[2],x)
x[del.min:del.max] <- ""
return(x)})
a b
[1,] "A" "C"
[2,] "B" "D"
[3,] "C" ""
[4,] "D" ""
[5,] "" ""
[6,] "" ""
[7,] "" "I"
[8,] "" "J"
[9,] "I" "K"
[10,] "J" "L"