vendredi 6 mars 2020

If statement that increases variable for each line when conditions not met in R

I would like to ask for some advice. I have a .csv file that I used to calculate the difference in time between two sightings(Elapsed). I would like to label every sighting(Elapsed) that is below 30 as visit number 1 but once the condition is false(above 30), it has to increase the visit number permanently to two....

Data table looks as follows:

Species Elapsed VisitNo aardvark 5 aardvark 10 aardvark 2 aardvark 30 aardvark 4 aardvark 30 aardvark 10

what I want is something like:

Species Elapsed VisitNo aardvark 5 1 aardvark 10 1 aardvark 2 1 aardvark 30 2 aardvark 4 2 aardvark 30 3 aardvark 10 3

TempVisit <- 1 VisitNo = TempVisit <- ifelse(Elapsed < 30 ,1,TempVisit + 1))

Although this only increases the value where the statement is false and not the following line as well.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire