How can I delete a line that has a line that starts with a space?
2 answers
One way with regex
and grepl
:
vec <- c('John',
'Tally',
' mac',
'hero')
#grepl returns TRUE if there is a match.
#'^ ' is regex for 'starting with space'
> vec[!grepl('^ ', vec)]
[1] "John" "Tally" "hero"
Or as per @NealFultz comment:
> vec[grep('^ ', vec, invert=TRUE)]
[1] "John" "Tally" "hero"
> grep('^ ', vec, invert=TRUE, value=TRUE)
[1] "John" "Tally" "hero"
Or, if you want to use startsWith
:
library(gdata)
#notice the minus sign below just before which
> vec[-which(startsWith(vec," "))]
[1] "John" "Tally" "hero"
or simply (as per @ Gregor's comment):
> vec[!startsWith(vec, " ")]
[1] "John" "Tally" "hero"
+8
source to share