How to deal with grep failure returning integer(0) in R?
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"
Instead of grep
, youcould use grepl
which returns a logical:
> if (grepl("w",data)== FALSE) print ("ok") else print("donkeykong")
[1] "ok"
> if (grepl("h",data)== FALSE) print ("ok") else print("donkeykong")
[1] "donkeykong"