You cas use isTRUE:

isTRUE(grep("a", "bbbb") > 0)


On Sat, Jul 4, 2009 at 2:56 PM, Mark Kimpel <mwkimpel@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
>
> ####################################3
> vec.1 <- c("a", "b", "c")
> vec.2 <- c("a", "c", "d")
> for (i in 1:length(vec.1)){
>  for (j in 1:length(vec.2)){
>    print(paste("i:", i, " j:", j, sep = ""))
>    pos <- grep(vec.1[i], vec.2[j])
>    if (pos > 0){
>      print("pos identified")
>    }
>    else{
>      print("pos not found")
>    }
>  }
> }
> ################################
>
> > #################################3
> > vec.1 <- c("a", "b", "c")
> > vec.2 <- c("a", "c", "d")
> > for (i in 1:length(vec.1)){
> +   for (j in 1:length(vec.2)){
> +     print(paste("i:", i, " j:", j, sep = ""))
> +     pos <- grep(vec.1[i], vec.2[j])
> +     if (pos > 0){
> +       print("pos identified")
> +     }
> +     else{
> +       print("pos not found")
> +     }
> +   }
> + }
> [1] "i:1 j:1"
> [1] "pos identified"
> [1] "i:1 j:2"
> Error in if (pos > 0) { : argument is of length zero
> No suitable frames for recover()
> > ###############
> > sessionInfo()
> R version 2.9.1 (2009-06-26)
> x86_64-unknown-linux-gnu
>
> locale:
>
> LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C
>
> attached base packages:
> [1] tcltk     stats     graphics  grDevices datasets  utils     methods
> [8] base
>
> other attached packages:
>  [1] sfsmisc_1.0-7       KEGG.db_2.2.11      GO.db_2.2.11
>  [4] rat2302.db_2.2.11   GOstats_2.10.0      RSQLite_0.7-1
>  [7] DBI_0.2-4           graph_1.22.2        Category_2.10.1
> [10] AnnotationDbi_1.6.1 qvalue_1.18.0       limma_2.18.2
> [13] affy_1.22.0         Biobase_2.4.1
>
> loaded via a namespace (and not attached):
>  [1] affyio_1.12.0        annotate_1.22.0      genefilter_1.24.2
>  [4] GSEABase_1.6.1       preprocessCore_1.6.0 RBGL_1.20.0
>  [7] splines_2.9.1        survival_2.35-4      tools_2.9.1
> [10] XML_2.5-3            xtable_1.5-5
> ------------------------------------------------------------
> Mark W. Kimpel MD  ** Neuroinformatics ** Dept. of Psychiatry
> Indiana University School of Medicine
>
> 15032 Hunter Court, Westfield, IN  46074
>
> (317) 490-5129 Work, & Mobile & VoiceMail
>
> "The real problem is not whether machines think but whether men do."
> -- B. F. Skinner
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]


