Identifying or coding unique factors

佐手、 提交于 2019-12-17 21:14:41

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/5798206/identifying-or-coding-unique-factors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!