To detail a bit my comment and @Adii_ 's :
when you use grep
, the result is the "position" of elements that fulfill the condition... so "nothing" if there is no match (hence the error message).
Using grepl
, you'll get TRUE or FALSE, which you can use in your if
statement.
As for length(grep(...))
, the result will be 0
if there is no match, corresponding to FALSE
for the if
statement, or a positive integer (1
in your case because you're testing only one element), if there is a match, corresponding to TRUE
for the if
statement.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…