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

Mark Kimpel mwkimpel at gmail.com
Sun Jul 5 01:52:06 CEST 2009


Thanks, embarrased that I didn't think of that myself :)
------------------------------------------------------------
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
******************************************************************



On Sat, Jul 4, 2009 at 2:47 PM, Allan Engelhardt<allane at cybaea.com> wrote:
>
>
> On 04/07/09 18:56, Mark Kimpel 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){
>>
>
> Try: if ( length(pos) > 0 ) {
>
> Allan.
>




More information about the R-help mailing list