Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...