我的数据集由每个人的三种处理(C、S 和 E)组成。看起来像这样。
Year Cultivar Site Distance Plant Treat yield1 yield2
1 2011 Blue ABR 0m 1 C 0.879 1.5
2 2011 Blue ABR 0m 1 S 0.384 2.3
3 2011 Blue ABR 0m 1 E 0.03 0.5
4 2011 Blue ABR 0m 2 C 0.923 1.2
5 2011 Blue ABR 0m 2 S 0.344 0.5
6 2011 Blue ABR 0m 2 E 0.07 0.7
7 2011 Blue ABR 50m 1 C 0.255 3.4
8 2011 Blue ABR 50m 1 S 1.00 2.4
9 2011 Blue ABR 50m 1 E 0.1 0.9
.
.
.
我有两年的数据,2 个品种,15 个地点,每个地点 3 个距离,每个距离 10 株植物。基本上我有很多数据(> 1400 行)。我希望能够做的是添加一个新列,为整个研究中的每个人分配一个新数字。我希望我的数据最终看起来像这样。
Individual Year Cultivar Site Distance Plant Treat yield1 yield2
1 1 2011 Blue ABR 0m 1 C 0.879 1.5
2 1 2011 Blue ABR 0m 1 S 0.384 2.3
3 1 2011 Blue ABR 0m 1 E 0.03 0.5
4 2 2011 Blue ABR 0m 2 C 0.923 1.2
5 2 2011 Blue ABR 0m 2 S 0.344 0.5
6 2 2011 Blue ABR 0m 2 E 0.07 0.7
7 3 2011 Blue ABR 50m 1 C 0.255 3.4
8 3 2011 Blue ABR 50m 1 S 1.00 2.4
9 3 2011 Blue ABR 50m 1 E 0.1 0.9
.
.
.
我对 R 比较陌生,所以如果这是相对容易做的事情,我深表歉意。我知道我应该能够“找到”每个个体作为植物*距离*地点*品种*年份的独特组合,但老实说我不知道我将如何编码,而且我还没有找到任何类似的帮助页面。
任何建议将不胜感激!
最佳答案
还有一个 data.table
使用 .GRP
的解决方案
.GRP is an integer, length 1, containing a simple group counter. 1 for the 1st group, 2 for the 2nd, etc.
library(data.table)
DT <- data.table(df)
DT[,grp :=.GRP,by = list(Year,Cultivar, Site, Distance, Plant)]
关于r - 添加标识个人的新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15260654/