Search code examples
rfactors

Identifying or coding unique factors


I would like to create a new variable,litter, to indicate each sow or litter in different farrowing dates (fdate). Each litter is to be numbered from 1 to N with an increament of 1 as shown in the last column.

sow   season piglet fdate        litter
1M521    1    5702   14/09/2009    1
1M521    1    5703   14/09/2009    1
1M521    2    22920  17/02/2010    2
1M521    2    22920  17/02/2010    2
1M521    2    22920  17/02/2010    2
1M584    1    8516   28/09/2009    3
1M584    1    8516   28/09/2009    3
1M584    1    8516   28/09/2009    3
1N312    1    6192   16/09/2009    4
1N312    1    6193   16/09/2009    4
1N312    1    6194   16/09/2009    4
1N312    2    21818  11/02/2010    5
1N312    2    21819  11/02/2010    5
1N312    2    21820  11/02/2010    5
2A1526   1    9293   3/10/2009     6
2A1526   1    9294   3/10/2009     6
2A1526   1    9295   3/10/2009     6
2A1526   1    9296   3/10/2009     6

Solution

  • If I'm understanding correctly, you're just basing this on fdate? If your dataframe is piglets, try this:

    piglets$litter <- as.numeric(factor(piglets$fdate))