Your code is incomplete. Also, your first comparison is NA vs a value. See what happens when you do that:
> if(NA != 1) print('Hello')
Error in if (NA != 1) print("Hello") :
missing value where TRUE/FALSE needed
On a side note, this sort of programming is like using python or java naively, where language specific features are discarded to "make something work". I'm sure there are better ways in R to partition a dataset and order within partitions in a custom manner, using a loop seems to be really inefficient in a language that vectorizes operations and uses the apply family to great effect.
On second thought, I guess OP's problem cannot really be solved without forcing R to process a data.frame row-by-row, which is why I suspect this is an XY problem.
EDIT: Here's a possible sequence of operations to get to the result:
Step-1: group by chromosome and start, and number groups
INPUT:
Chromosome Start End
1 990595 990685
1 990595 990882
1 1459777 1460585
1 1563779 1563827
2 1564102 1564215
2 1564108 1564219
OUTPUT:
Chromosome Start End gp.1
1 990595 990685 1
1 990595 990882 1
1 1459777 1460585 2
1 1563779 1563827 3
2 1564102 1564215 1
2 1564108 1564219 2
Step-2: group by chromosome gp.1; and number groups, while counting group membership
INPUT:
Chromosome Start End gp.1
1 990595 990685 1
1 990595 990882 1
1 1459777 1460585 2
1 1563779 1563827 3
2 1564102 1564215 1
2 1564108 1564219 2
OUTPUT:
Chromosome Start End gp.1 gp.2 gp.count
1 990595 990685 1 1 2
1 990595 990882 1 2 2
1 1459777 1460585 2 1 1
1 1563779 1563827 3 1 1
2 1564102 1564215 1 1 1
2 1564108 1564219 2 1 1
Step-3: Determine final group
If count is 1 for a group, gp is gp.1. If count > 1, gp = paste(gp.1,gp.2,sep=".")
INPUT:
Chromosome Start End gp.1 gp.2 gp.count
1 990595 990685 1 1 2
1 990595 990882 1 2 2
1 1459777 1460585 2 1 1
1 1563779 1563827 3 1 1
2 1564102 1564215 1 1 1
2 1564108 1564219 2 1 1
OUTPUT:
Chromosome Start End gp.1 gp.2 gp.count final.gp
1 990595 990685 1 1 2 1.1
1 990595 990882 1 2 2 1.2
1 1459777 1460585 2 1 1 2
1 1563779 1563827 3 1 1 3
2 1564102 1564215 1 1 1 1
2 1564108 1564219 2 1 1 2
Each of the three steps above can be vectorized, avoiding loops entirely.
What have you tried? Please add some context (on the biological problem you're trying to solve) so this pure R question can qualify as a bioinformatics question.
Ok I will modify the question for better understanding.
This looks like an XY problem. What are you doing this exercise for?
I have to find the overlap region if it has been found then add new column like serial number 1.1, 1.2 etc
That is a verbatim description of the problem, not the reason why this problem needs solving (See what an XY problem means). There is also another factor you have not mentioned: the numbering resets with the start of a new chromosome. This is quite the convoluted logic and I don't know why you're doing it, so I want to make sure this problem needs solving before starting to solve it.
You probably need:
Or from data.table package: