I have a data frame containing species populations and their abundance in each population. I would like to add another column which contains an ID for each species. The population column increases upwards from 1 and re-sets each time a new species occurs. The species ID column therefore will count along the species population column, and increase in value by +1 each time the population column resets to '1'.
I understand the principle of what I want to achieve, but have so far failed to obtain the correct result. I include a dummy example below containing populations and counts for 2 species:
df <- data.frame(Count=c(10, 5, 4, 10, 6),
Population=c(1, 2, 3, 1, 2))
Count Population
10 1
5 2
4 3
10 1
6 2
The desired outcome would be:
Count Population species
10 1 1
5 2 1
4 3 1
10 1 2
6 2 2
Where a new column of species IDs has been generated, based upon each time the value in df$Population resets to 1. In reality the data set contains many hundreds of species each with varying numbers of populations.
I have been able to generate the following output using the following code:
df$species[c(df$b[-1], 0) == 1] <- df$species[c(df$b[-1], 0) == 1] + 1
Count Population species
10 1 1
5 2 0
4 3 0
10 1 1
6 2 0
However, I cannot get the species column to accumulate in value along the sequence.
Thanks in advance for any suggestions. Apologies in advance if the question or example are not clear.
Mike
Aucun commentaire:
Enregistrer un commentaire