0

I have observations of animals in different stages (i.e. stage 0 - stage 4). In some of the stages, I have more than one observation in a stage (e.g. Stage 0- 47 days and stage 0 - 50 days). This means that the animal was seen in stage 0 at day 47 and in stage 0 at day 50. But other stages may only be observed once (e.g. Stage 1 - 57 days). My code states that if there is more than one sighting of a stage use a specific code and if there is just one sighting, use another specific code. But for some reason, Stage 0 is also included in the code with only one observation although we clearly have more than one observation of that stage.

I think that my if condition statement in my code may be wrong, but I can't seem to find any alternative code to use.

> head(dat)
    SPENO       Date MoltStatus DaysfromOct01 SEASON
1 PG(1)67 2012/11/16          0            47   2012
2 PG(1)67 2012/11/19          0            50   2012
3 PG(1)67 2012/11/26          1            57   2012
4 PG(1)67 2012/12/05          2            66   2012
5 PG(1)67 2012/12/16          3            77   2012
6 PG(1)67 2012/12/22          4            83   2012

If we only have one observation of a specific stage:

if(length(Molt[Molt==0])=1){ #If there is just one sighting of molt code 0   
  Diff0=(Date[Molt==1])-(Date[Molt==0]) #just get the difference between stage 0 date and stage 1 date ---to get duration of stage 0
  Results=rbind(Results,c(SPENO[i],0,Diff0,(Date[Molt==0]),Season))

If we have more than one observation of a specific code:

if(length(Molt[Molt==0])>0&length(Molt[Molt==1])>0){ #If there is more than one sighting of molt code 0 
        First_0=min(Date[Molt==0]) #Calculate first 0 instance
        First_1=min(Date[Molt==1]) #Calculate first 1 instance
        Diff_0=First_1-First_0 #Calculate difference
        Results=rbind(Results,c(SPENO[i],0,Diff_0,First_0,Season)) 
      }

> Diff0
[1] 10  7

> Diff_0
[1] 10

The output I get from the code for stage 0 when stage 1 - 57 days: The code gives me results for the first code where I actually state that it should not use the code because I have more than one observation. The output is then 10 and 7 (next to each other, which messes up my results table at the end), but I should not have an output for the code. While for the second part where I state the code should be used, the output is given as 10, which is correct.

The expected output should just be:

> Diff0
[1] 0

> Diff_0
[1] 10

Leandri
  • 11
  • 2
  • 1
    Please make your question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In short, add sample data, current and expected output.( not just code). Please read about the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/250659). – NelsonGon Jun 19 '19 at 08:22
  • I made the question clearer now with added sample data and current and expected outputs. – Leandri Jun 19 '19 at 08:34
  • `if(length(Molt[Molt==0])=1)` should be `if(length(Molt[Molt==0])==1)`. Looks like a simple typo. – Aaron Hayman Jun 19 '19 at 08:35

0 Answers0