[R] problem with grep under loop

MacQueen, Don macqueen1 at llnl.gov
Mon Sep 16 20:20:54 CEST 2013


to understand the "argument is of length zero" message, study these
example:

> if (grep('a',c('a','b'))==1) 'a' else 'b'
[1] "a"

> if (grep('a',c('c','b'))==1) 'a' else 'b'
Error in if (grep("a", c("c", "b")) == 1) "a" else "b" :
  argument is of length zero
 
> grep('a',c('c','b'))
integer(0)

> grep('a',c('c','b'))==1
logical(0)

> length(grep('a',c('c','b')))
[1] 0

> length(grep('a',c('c','b'))==1)
[1] 0


The value inside the parentheses of if() has to have length=1.


And consider possibly using grepl() instead of grep(), for example:

> grepl('a',c('c','b'))
[1] FALSE FALSE

> any(grepl('a',c('c','b')))
[1] FALSE



-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 9/13/13 8:17 PM, "capricy gao" <capricyg at yahoo.com> wrote:

>Thanks a lot for all the responses!!
>
>I then first test my data:
>
>> dim(data)
>[1] 52086    13
>>  if(grep(data[3,4],data[3,12])==1) print("Y")
>[1] "Y"
>> for(i in 1:52086){if(grep(data[i,4],data[i,12])==1) print ("Y")}
>Error in if (grep(data[i, 4], data[i, 12]) == 1) print("Y") :
>  argument is of length zero
>
>
>What is this new error message? "argument is of length zero"
>
>Here is my data format:
>>head(data)
>
>           V1     V2         V3           V4       V5  V6       V7
>   V8
>1 ref_gene_id ref_id class_code cuff_gene_id  cuff_id FMI     FPKM
>FPKM_conf_lo
>2           -      -          u       C.3 C.3.1 100 1.000000     0.000000
>3           -      -          u       C.2 C.2.1 100 1.000000     0.000000
>4           -      -          u       C.4 C.4.1 100 1.000000     0.000000
>5           -      -          u       C.1 C.1.1 100 1.000000     0.000000
>6           -      -          u       C.5 C.5.1 100 1.000000     0.000000
>            V9      V10 V11          V12           V13
>1 FPKM_conf_hi      cov len major_iso_id ref_match_len
>2     0.000000 0.056682  96     C.3.1             -
>3     0.000000 0.058453  99     C.2.1             -
>4     0.000000 0.059634 101     C.4.1             -
>5     0.000000 0.059634 101     C.2.1             -
>6     0.000000 0.059634 101     C.5.1             -
>
>
>________________________________
> From: Jeff Newmiller <jdnewmil at dcn.davis.CA.us>
>
>help at r-project.org" <r-help at r-project.org>
>Sent: Friday, September 13, 2013 9:21 PM
>Subject: Re: [R] problem with grep under loop
>
>
>This is because you are not printing it (with the print or cat
>functions). Keep in mind that the visible result you get from calling a
>function or evaluating a variable interactively comes from the
>interactive R command line, not from R itself. Once you put such an
>expression inside a function (such as the "for" function) it is no longer
>directly being invoked by the command interpreter.
>
>You might want to read [1] and [2] (which says don't post using HTML).
>
>[1] 
>http://stackoverflow.com/questions/4716152/why-do-r-objects-not-print-in-a
>-function-or-a-for-loop
>[2] http://www.R-project.org/posting-guide.html
>--------------------------------------------------------------------------
>-
>Jeff Newmiller                        The     .....       .....  Go
>Live...
>DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
>Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>/Software/Embedded Controllers)               .OO#.       .OO#.
>rocks...1k
>--------------------------------------------------------------------------
>-
>Sent from my phone. Please excuse my brevity.
>
>
>>
>>
>>I am just testing the possibility of using grep under for loop:
>>
>>>for(i in 1:10){grep("a",letters)}
>>
>>
>>nothing came out;
>>
>>when I ran: 
>>
>>
>>>grep("a",letters),
>>
>>
>>I got "1"
>>
>>so in my for loop, I expected to see ten "1"s, but I did not.
>>
>>Could anybody help me to figure out why? Thanks a lot for your help.
>>
>>Capricy
>>    [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at 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.
>	[[alternative HTML version deleted]]
>



More information about the R-help mailing list