我有以下数据框(如下),它是我的完整数据集的一个子集。我需要为具有相同 LAT/LONG 和登陆日期的每个条目定义相同的 ID。我之前从最早到最晚的日期订购了 LANDEDDATE。
> dput(df2)
structure(list(LATITUDE = c(43.35, 43.35, 43.35, 43.35, 43.35,
43.35, 43.35, 43.35, 43.5166, 43.5166, 43.5166, 43.5166, 43.5166,
43.5166, 43.5166, 42.9833, 42.9833, 42.9833, 42.9666, 42.9666
), LONGITUDE = c(-60.6163, -60.6165, -60.7167, -60.7166, -60.7163,
-60.716, -60.7169, -60.7166, -59.9169, -59.9168, -59.9169, -59.9166,
-59.9166, -59.916, -59.916, -61.8333, -61.8333, -61.8333, -61.9161,
-61.9161), LANDEDDATE = structure(c(11171, 11171, 11183, 11183,
11183, 11183, 11183, 11183, 11192, 11192, 11192, 11192, 11192,
11192, 11192, 11210, 11210, 11210, 11210, 11210), class = "Date"),
sppCODE = c(251L, 251L, 251L, 251L, 251L, 251L, 251L, 251L,
251L, 251L, 251L, 251L, 251L, 256L, 251L, 256L, 252L, 251L,
251L, 252L), LIVEW = c(0.337, 0.471, 0.238, 0.772, 0.178,
0.416, 0.535, 0.356, 0.442, 0.663, 0.442, 0.497, 0.276, 0.032,
0.828, 0.035, 0.011, 1.224, 1.025, 0.072), SPECIES = structure(c(7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L), .Label = c("Albacore Tuna", "Bigeye Tuna",
"Bluefin Tuna", "Mako", "Porbeagle", "Shark, UNSP", "Swordfish",
"Tuna, UNSP", "White Marlin", "Yellowfin Tuna"), class = "factor")), .Names = c("LATITUDE",
"LONGITUDE", "LANDEDDATE", "sppCODE", "LIVEW", "SPECIES"), row.names = c(19L,
20L, 13L, 14L, 15L, 16L, 17L, 18L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L), class = "data.frame")
这是我到目前为止所做的,但我无法弄清楚其余的。
df2$setID<-"NA" #I created an empty setID.
> head(df2)
LATITUDE LONGITUDE LANDEDDATE sppCODE LIVEW SPECIES setID
19 43.35 -60.6163 2000-08-02 251 0.337 Swordfish NA
20 43.35 -60.6165 2000-08-02 251 0.471 Swordfish NA
13 43.35 -60.7167 2000-08-14 251 0.238 Swordfish NA
14 43.35 -60.7166 2000-08-14 251 0.772 Swordfish NA
15 43.35 -60.7163 2000-08-14 251 0.178 Swordfish NA
16 43.35 -60.7160 2000-08-14 251 0.416 Swordfish NA
unique<-df2[which(!duplicated(df2[,1:3])),] #This is each entry that are NOT duplicate
unique2$setID<-1:13 # Ranked from 1:13
> head(unique) #looks like that
LATITUDE LONGITUDE LANDEDDATE sppCODE LIVEW SPECIES setID
19 43.35 -60.6163 2000-08-02 251 0.337 Swordfish 1
20 43.35 -60.6165 2000-08-02 251 0.471 Swordfish 2
13 43.35 -60.7167 2000-08-14 251 0.238 Swordfish 3
14 43.35 -60.7166 2000-08-14 251 0.772 Swordfish 4
15 43.35 -60.7163 2000-08-14 251 0.178 Swordfish 5
16 43.35 -60.7160 2000-08-14 251 0.416 Swordfish 6
rep<-df2[which(duplicated(df2[,1:3])),] #This is all my replicates
我需要将我唯一数据帧的 setID 分配给 rep 数据帧中的相应集合(使用相同的 LAT/LONG 和 LANDEDDATE 复制)。任何建议将不胜感激!