Identifying or coding unique factors
Asked Answered
T

1

1

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
Telegram answered 27/4, 2011 at 1:4 Comment(0)
C
3

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))
Clichy answered 27/4, 2011 at 1:14 Comment(2)
i am trying to base it on the sow and fdate.Telegram
Ah, missed that since your sample data doesn't have two sows on the same day. You could do as.numeric(factor(paste(piglets$fdate,piglets$sow,sep=""))) to look at the combination of the two.Clichy

© 2022 - 2024 — McMap. All rights reserved.