[R] help with dealing with integer(0) returns from grep used within a conditional loop

hadley wickham h.wickham at gmail.com
Sat Jul 4 23:08:06 CEST 2009


On Sat, Jul 4, 2009 at 7:56 PM, Mark Kimpel<mwkimpel at gmail.com> wrote:
> I am using grep to locate colnames to automate a report build and have
> run into a problem when a colname is not found. The use of integer(0)
> in a conditional statement seems to be a no no as it has length 0.
> Below is a self-contained trivial example. I would like to get
> something like "NA" or -1 for the position when it is not found OR
> learn a way to use integer(0) or some "cast" of it in a logical
> statement. Example, output, and sessionInfo follow. Thanks, Mark

You might also consider using grepl instead of grep.  grepl works just
like grep, but returns a logical vector.

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list