Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

r - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...