I'm trying to get the whole rows per date entry that has the lower BIC value between the group. But I want to get all the rows with this value not just the first one. Using the code below I can get the minimum value
df %>% group_by(sample, date) %>%
slice(which.min(BIC))
# Groups: sample, date [11]
date sample BIC mode nmodes
<dttm> <int> <dbl> <int> <dbl>
1 2016-10-11 00:02:23 1 1246. 1 3
2 2016-10-11 00:07:23 2 1299. 1 2
3 2016-10-11 00:12:23 3 1297. 1 2
4 2016-10-11 00:17:23 4 1276. 1 2
5 2016-10-11 00:22:23 5 1318. 1 3
6 2016-10-11 00:27:23 6 1261. 1 2
7 2016-10-11 00:32:23 7 1283. 1 2
8 2016-10-11 00:37:23 8 1253. 1 3
9 2016-10-11 00:42:23 9 1311. 1 3
10 2016-10-11 00:47:23 10 1257. 1 3
11 2016-10-11 00:52:23 11 1307. 1 2
But I want to keep all the entries with the min value per group, something like this:
# Groups: sample, date [11]
date sample BIC mode nmodes
<dttm> <int> <dbl> <int> <dbl>
1 2016-10-11 00:02:23 1 1246. 1 3
1 2016-10-11 00:02:23 1 1246. 2 3
1 2016-10-11 00:02:23 1 1246. 3 3
How can I do it? This is my sample df:
structure(list(date = structure(c(1476144143, 1476144443, 1476144743,
1476145043, 1476145343, 1476145643, 1476145943, 1476146243, 1476146543,
1476146843, 1476147143, 1476144143, 1476144143, 1476144443, 1476144443,
1476144743, 1476144743, 1476145043, 1476145043, 1476145343, 1476145343,
1476145643, 1476145643, 1476145943, 1476145943, 1476146243, 1476146243,
1476146543, 1476146543, 1476146843, 1476146843, 1476147143, 1476147143,
1476144143, 1476144143, 1476144143, 1476144443, 1476144443, 1476144443,
1476144743, 1476144743, 1476144743, 1476145043, 1476145043, 1476145043,
1476145343, 1476145343, 1476145343, 1476145643, 1476145643, 1476145643,
1476145943, 1476145943, 1476145943, 1476146243, 1476146243, 1476146243,
1476146543, 1476146543, 1476146543, 1476146843, 1476146843, 1476146843,
1476147143, 1476147143, 1476147143), tzone = "UTC", class = c("POSIXct",
"POSIXt")), sample = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L,
8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L,
8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L), BIC = c(1326.56509702336,
1359.13951012986, 1354.9815001139, 1360.57365342725, 1549.92289705593,
1319.65769432099, 1353.56368040222, 1332.20398344578, 1379.63979085508,
1370.67135814892, 1333.7747223603, 1249.98496322515, 1249.98496322515,
1298.60937765424, 1298.60937765424, 1297.28476441819, 1297.28476441819,
1276.10482284557, 1276.10482284557, 1359.6738228193, 1359.6738228193,
1260.72557425431, 1260.72557425431, 1282.76144735464, 1282.76144735464,
1261.48313495276, 1261.48313495276, 1311.32200578627, 1311.32200578627,
1261.70365272645, 1261.70365272645, 1307.17533630926, 1307.17533630926,
1245.69239787804, 1245.69239787804, 1245.69239787804, 1298.74822799891,
1298.74822799891, 1298.74822799891, 1304.94059543911, 1304.94059543911,
1304.94059543911, 1287.37139573675, 1287.37139573675, 1287.37139573675,
1317.87026576071, 1317.87026576071, 1317.87026576071, 1267.49084561851,
1267.49084561851, 1267.49084561851, 1287.11364956865, 1287.11364956865,
1287.11364956865, 1252.77079960898, 1252.77079960898, 1252.77079960898,
1311.12551673505, 1311.12551673505, 1311.12551673505, 1256.94251071717,
1256.94251071717, 1256.94251071717, 1315.49720427767, 1315.49720427767,
1315.49720427767), mode = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), nmodes = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)), row.names = c("c95",
"c16", "c9", "c56", "c30...5", "c27...6", "c91", "c7...8", "c12",
"c23", "c911", "f29", "c29", "c13", "f13", "f24...16", "c24...17",
"c36", "f36", "c27...20", "f27", "f65", "c65", "f15...24", "c15...25",
"f60", "c60", "c2", "f2", "c361", "f361", "c7...32", "f7", "f31",
"c31", "j31", "j3", "c3", "f3", "j24", "f24...41", "c24...42",
"f15...43", "c15...44", "j15", "j8", "c8", "f8", "c32", "f32",
"j32", "c30...52", "f30", "j30", "c18", "j18", "f18", "j301",
"f301", "c301", "c25", "f25", "j25", "j", "f", "c"), class = "data.frame")