4

I am trying to cut down a list of gene names that I have been given. I'm trying to eliminate any repetitive names that may be present but I keep getting an error when running my code:

counter=0 i=0 j=0 geneNamesRevised=array(dim=length(geneNames)) for (i in 0:length(geneNamesRevised)) geneNamesRevised[i]="" geneNamesRevised for (i in 1:length(geneNames)) for (j in 1:length(geneNamesRevised)) if (geneNames[i]==geneNamesRevised[j]) { break } else if ((j==length(geneNamesRevised)-1) && (geneNames[i]!=geneNamesRevised[j])) { geneNamesRevised[counter]=geneNames[i] counter++ } 

The error message is a repetitive string of :

the condition has length > 1 and only the first element will be usedthe condition has length > 1 and only the first element will be usedthe condition has length > 1 and only the first element will be used

and this error message is for the last "else if" statement that has the '&&'.

Thank you!

1
  • just try only one &. does it work? Commented Feb 8, 2017 at 0:01

3 Answers 3

2

Why not just

geneNamesRevised <- unique( geneNames ) 

... which returns a shortened list. There is also a duplicated function that can be used to remove duplicates when negated.

Sign up to request clarification or add additional context in comments.

Comments

1

There are a few problems in your code.

1) The else is incorrectly specified - or not :) thanks @Mohsen_Fatemi

2) & is usually what you need rather than &&

3) counter++ isn't R

Copy the code below and see if it runs

 for (i in 1:length(geneNames)){ for (j in 1:length(geneNamesRevised)){ if (geneNames[i]==geneNamesRevised[j]) { break } else { if ((j==length(geneNamesRevised)-1) & (geneNames[i]!=geneNamesRevised[j])) { geneNamesRevised[counter]=geneNames[i] counter <- counter + 1 } } } } 

Edit

4) also you were missing braces for your fors

1 Comment

you are wrong about else if , look here
0

use & instead of && ,

else if ((j==length(geneNamesRevised)-1) & (geneNames[i]!=geneNamesRevised[j])) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.