答案 0 :(得分:2)
使用grep在两行之间创建索引向量。
x[-c(grep("5. Demand Disputed", x$V1) : grep("Total Demand Disputed", x$V1), ]
解释
grep
"返回x的元素索引的向量,产生匹配" (?grep
)
因此,您可以简单地在两行之间创建一个整数向量,使:
匹配两个字符串。
答案 1 :(得分:2)
使用玩具示例......
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"