1

TO the following code I get the message "the condition has length > 1 and only the first element will be used":

if (is.na(PSIBl_Tax_Gene$CDS_Acc1)==TRUE & is.na(PSIBl_Tax_Gene$GeneID)==FALSE)

I guess that here IF looks only at the "==" operator, but I'm actually interested in IF looking at the whole expression, given by the "&" (AND) logical operator. I guess I'm making a syntax mistake, but so far I haven't figure out which.

CLM
  • 119
  • 2
  • 12
  • 1
    You need the `ifelse` function if your logical expression results in a vector of length > 1. – Roland Mar 03 '14 at 15:20
  • 2
    Perhaps you are looking to add `any` or `all` to your queries. – A5C1D2H2I1M1N2O1R2T1 Mar 03 '14 at 15:20
  • I've run into this problem before, try using `ifelse` to work on a vector(s) rather than `if` – maloneypatr Mar 03 '14 at 15:21
  • I've thought previously about ifelse, and, yby reading the description of ifelse, it I don't think it is what I need. – CLM Mar 03 '14 at 15:30
  • first see this answer: http://stackoverflow.com/questions/4367177/warning-the-condition-has-length-1-and-only-the-first-element-will-be-used?rq=1 If it doesnt answer your question, consider letting us know the size shape and levels of `PSIBl_Tax_Gene$CDS_Acc1` and `PSIBl_Tax_Gene$GeneID` – Seth Mar 03 '14 at 15:40

1 Answers1

3

Try:

if (all(is.na(PSIBl_Tax_Gene$CDS_Acc1)==TRUE & is.na(PSIBl_Tax_Gene$GeneID)==FALSE))

(see also vector logical operator "any").

The problem is that you are trying to condition a vector of TRUE/FALSE values:

if(c(TRUE,FALSE,TRUE,TRUE,...)) show("yes")

hence the message. Look what happens when you run

is.na(PSIBl_Tax_Gene$CDS_Acc1)

you should get a vector of TRUE/FALSE values. You either need to look at one of them:

is.na(PSIBl_Tax_Gene$CDS_Acc1)[x]==TRUE

where x is the index of a vector element you are interested in, or you can compare the entire vector with a vector of TRUE/FALSE values

all(is.na(PSIBl_Tax_Gene$CDS_Acc1) == rep(TRUE,length(PSIBl_Tax_Gene$CDS_Acc1)))

in short

all(is.na(PSIBl_Tax_Gene$CDS_Acc1) == TRUE))

polTo
  • 290
  • 1
  • 4
  • 12
  • 1
    yes, you're right, I actually needed to look only at one of them, without realizing I did not specify the X. Thanks! – CLM Mar 03 '14 at 16:12