In the grep expression,when the value of grep is integer(0)
,print "ok",how can i do?
> data="haha"
> grep("w",data)
integer(0)
> if (grep("w",data)==0) print ("ok")
Error in if (grep("w", data) == 0) print("ok") :
argument is of length zero
The grepl R function searches for matches of certain character pattern in a vector of character strings and returns a logical vector indicating which elements of the vector contained a match. Example how to use grepl: As we can see, grepl () returns a logical vector for each element.
(though beware it will also run the second grep and/or echo if the first echo fails) Edit 1 > explain grep -q ... Sure. In normal situations, grep return status is 0 (and just returns "not 0" if an error occurs (eg. file not found))
In normal situations, grep return status is 0 (and just returns "not 0" if an error occurs (eg. file not found)) grep -qF exp file "returns" 0 if it finds exp in file, error otherwise ( grep -q exp file would do that if the exp regexp was matched in file ). Show activity on this post.
Sometimes when you use the which () function in R, you may end up with integer (0) as a result, which indicates that none of the elements in a vector evaluated to TRUE. For example, suppose we use the following code to check which elements in a vector are equal to the value 10:
You can use either length
or identical
R> if (length(grep("w", data)) == 0) print ("ok")
[1] "ok"
R> if (identical(grep("w", data), integer(0))) print ("ok")
[1] "ok"
You could also use grepl
instead of grep
R> if (!any(grepl("w", data))) print('ok')
[1] "ok"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With