Вот идея с использованием факторов:
С Базой R:
df$Group = as.integer(factor(paste(df$Key.1, df$Key.2),
levels = unique(paste(df$Key.1, df$Key.2))))
Или с mutate
из dplyr
:
library(dplyr)
df = mutate(df, Group = paste(Key.1, Key.2) %>%
factor(., levels = unique(.)) %>%
as.integer())
Результат:
Key.1 Key.2 Value Group
1 5/25/2018 -10 0.53928999 1
2 5/25/2018 -10 0.23083204 1
3 5/25/2018 -10 0.33742676 1
4 5/25/2018 0 0.53479860 2
5 5/25/2018 0 0.27612761 2
6 5/25/2018 0 0.74993199 2
7 5/25/2018 10 0.01397069 3
8 5/25/2018 10 0.10553610 3
9 5/25/2018 10 0.66147883 3
10 1/17/2018 -10 0.14381738 4
11 1/17/2018 -10 0.52708544 4
12 1/17/2018 -10 0.75862925 4
13 1/17/2018 0 0.45954116 5
14 1/17/2018 0 0.68467543 5
15 1/17/2018 0 0.15865298 5
16 1/17/2018 10 0.01039363 6
17 1/17/2018 10 0.49886623 6
18 1/17/2018 10 0.98269967 6
19 5/25/2018 10 0.10553610 3
20 5/25/2018 -10 0.33742676 1
Данные:
df = structure(list(Key.1 = c("5/25/2018", "5/25/2018", "5/25/2018",
"5/25/2018", "5/25/2018", "5/25/2018", "5/25/2018", "5/25/2018",
"5/25/2018", "1/17/2018", "1/17/2018", "1/17/2018", "1/17/2018",
"1/17/2018", "1/17/2018", "1/17/2018", "1/17/2018", "1/17/2018",
"5/25/2018", "5/25/2018"), Key.2 = c(-10L, -10L, -10L, 0L, 0L,
0L, 10L, 10L, 10L, -10L, -10L, -10L, 0L, 0L, 0L, 10L, 10L, 10L,
10L, -10L), Value = c(0.53928999, 0.23083204, 0.33742676, 0.5347986,
0.27612761, 0.74993199, 0.01397069, 0.1055361, 0.66147883, 0.14381738,
0.52708544, 0.75862925, 0.45954116, 0.68467543, 0.15865298, 0.01039363,
0.49886623, 0.98269967, 0.1055361, 0.33742676)), .Names = c("Key.1",
"Key.2", "Value"), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20"))