0

I have a tibble of 401 rows. There are more columns in my full dataset but I have provided below only the ones relevant to my question.

> head(df)
# A tibble: 6 x 5
# Groups:   Week, Date, Observer [1]
   Week Date       Observer StartTime   ord
  <int> <date>     <fct>    <fct>     <dbl>
1     1 2020-07-08 IF       05:10         3
2     1 2020-07-08 IF       05:37        12
3     1 2020-07-08 IF       06:06        27
4     1 2020-07-08 IF       06:35        47
5     1 2020-07-08 IF       07:01        68
6     1 2020-07-08 IF       07:29        93

Week runs from 1 to 13, Date has 2-3 unique values for each Week, Observer has two values "IF" and "KT" for unique Date, and StartTime has up to 10 unique values for unique Observer. My aim is to create a column, ord here, of numbers 1-10 based on the grouping by Week, Date and Observer. In other words, I want to number the (up to) 10 unique values of StartTime for unique Week-Date-Observer combinations.

e.g., the date 2020-07-08 should have 1-10 numbering for "IF" and also 1-10 for "KT". Then, it should repeat for 2020-07-09 and the rest. But additionally, in case there are less than 10 unique values (say 6), the numbering should start at 1 and end at the appropriate number (6).

In the example I have provided, I used summarise(ord = as.numeric(unique(StartTime)) after the grouping and this seems to be the best result so far, as it gets the grouping right and only the individual values are wrong (3, 12, 27, ... instead of 1, 2, 3, ..., 10). On the other hand, when I tried things like summarise(ord = levels(unique(StartTime)) or summarise(ord = 1:n_distinct(StartTime)), the results weren't even close to what I wanted.

I have tried for-loops and the *apply functions, but I can't seem to wrap my head around how exactly to use them for my particular requirement. Any help and nudge towards the right direction would be greatly appreciated!

dput(df) below:

structure(list(Week = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,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 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L), Date = structure(c(18451, 18451, 18451, 18451, 
18451, 18451, 18451, 18451, 18451, 18451, 18451, 18451, 18451, 
18451, 18451, 18451, 18451, 18451, 18451, 18452, 18452, 18452, 
18452, 18452, 18452, 18452, 18452, 18452, 18452, 18452, 18452, 
18452, 18457, 18457, 18457, 18457, 18457, 18457, 18457, 18457, 
18457, 18457, 18457, 18457, 18457, 18457, 18457, 18457, 18457, 
18457, 18458, 18458, 18458, 18458, 18458, 18458, 18458, 18458, 
18458, 18458, 18458, 18458, 18465, 18465, 18465, 18465, 18465, 
18465, 18465, 18465, 18465, 18465, 18465, 18465, 18465, 18465, 
18465, 18465, 18465, 18465, 18465, 18465, 18466, 18466, 18466, 
18466, 18466, 18466, 18466, 18466, 18466, 18466, 18471, 18471, 
18471, 18471, 18471, 18471, 18471, 18471, 18471, 18471, 18471, 
18471, 18471, 18471, 18471, 18471, 18471, 18471, 18471, 18473, 
18473, 18473, 18473, 18473, 18473, 18473, 18473, 18473, 18473, 
18473, 18480, 18480, 18480, 18480, 18480, 18480, 18480, 18480, 
18480, 18480, 18480, 18480, 18480, 18480, 18480, 18480, 18480, 
18480, 18481, 18481, 18481, 18481, 18481, 18481, 18481, 18481, 
18481, 18481, 18481, 18486, 18486, 18486, 18486, 18486, 18486, 
18486, 18486, 18486, 18486, 18486, 18486, 18486, 18486, 18486, 
18486, 18486, 18486, 18487, 18487, 18487, 18487, 18487, 18487, 
18487, 18487, 18487, 18487, 18487, 18494, 18494, 18494, 18494, 
18494, 18494, 18494, 18494, 18494, 18494, 18494, 18494, 18494, 
18494, 18494, 18494, 18494, 18494, 18494, 18494, 18495, 18495, 
18495, 18495, 18495, 18495, 18495, 18495, 18495, 18495, 18495, 
18495, 18500, 18500, 18500, 18500, 18500, 18500, 18500, 18500, 
18500, 18500, 18500, 18500, 18500, 18500, 18500, 18500, 18500, 
18500, 18500, 18500, 18501, 18501, 18501, 18501, 18501, 18501, 
18501, 18501, 18501, 18501, 18501, 18501, 18507, 18507, 18507, 
18507, 18507, 18507, 18507, 18507, 18507, 18507, 18507, 18507, 
18507, 18507, 18507, 18507, 18507, 18507, 18507, 18507, 18508, 
18508, 18508, 18508, 18508, 18508, 18508, 18508, 18508, 18508, 
18508, 18508, 18515, 18515, 18515, 18515, 18515, 18515, 18515, 
18515, 18515, 18515, 18515, 18515, 18515, 18515, 18515, 18515, 
18515, 18515, 18515, 18516, 18516, 18516, 18516, 18516, 18516, 
18516, 18516, 18516, 18516, 18516, 18516, 18521, 18521, 18521, 
18521, 18521, 18521, 18521, 18521, 18521, 18521, 18521, 18521, 
18521, 18521, 18521, 18521, 18521, 18521, 18521, 18522, 18522, 
18522, 18522, 18522, 18522, 18522, 18522, 18522, 18522, 18522, 
18522, 18527, 18527, 18527, 18527, 18527, 18527, 18527, 18527, 
18527, 18527, 18527, 18527, 18527, 18527, 18527, 18527, 18527, 
18527, 18527, 18527, 18529, 18529, 18529, 18529, 18529, 18529, 
18529, 18529, 18529, 18529, 18529, 18535, 18535, 18535, 18535, 
18535, 18535, 18535, 18535, 18535, 18535, 18535, 18535, 18535, 
18535, 18535, 18535, 18535, 18535, 18535, 18535, 18536, 18536, 
18536, 18536, 18536, 18536, 18536, 18536, 18536, 18536, 18536, 
18536), class = "Date"), Observer = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 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2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("IF", 
"KT"), class = "factor"), StartTime = structure(c(3L, 12L, 27L, 
47L, 68L, 93L, 109L, 130L, 152L, 173L, 1L, 11L, 30L, 50L, 76L, 
102L, 124L, 152L, 176L, 9L, 25L, 43L, 61L, 83L, 105L, 1L, 17L, 
45L, 84L, 114L, 136L, 167L, 4L, 16L, 35L, 79L, 100L, 118L, 140L, 
158L, 1L, 7L, 26L, 48L, 73L, 101L, 121L, 145L, 167L, 189L, 5L, 
16L, 35L, 53L, 79L, 100L, 2L, 13L, 31L, 56L, 82L, 107L, 6L, 19L, 
39L, 57L, 80L, 101L, 120L, 143L, 160L, 180L, 1L, 7L, 24L, 43L, 
67L, 94L, 118L, 147L, 169L, 189L, 8L, 21L, 78L, 100L, 1L, 10L, 
28L, 52L, 82L, 107L, 8L, 23L, 41L, 62L, 86L, 105L, 121L, 159L, 
178L, 7L, 22L, 41L, 65L, 92L, 114L, 141L, 164L, 184L, 198L, 7L, 
20L, 59L, 81L, 102L, 7L, 24L, 44L, 66L, 92L, 111L, 12L, 26L, 
42L, 61L, 82L, 103L, 158L, 178L, 14L, 28L, 46L, 67L, 92L, 110L, 
135L, 159L, 182L, 195L, 16L, 34L, 71L, 94L, 109L, 14L, 29L, 54L, 
80L, 103L, 128L, 15L, 33L, 54L, 74L, 113L, 133L, 171L, 188L, 
16L, 35L, 58L, 83L, 104L, 125L, 149L, 174L, 192L, 207L, 18L, 
34L, 56L, 75L, 114L, 16L, 36L, 56L, 76L, 102L, 122L, 25L, 43L, 
64L, 85L, 105L, 124L, 145L, 163L, 181L, 193L, 24L, 41L, 63L, 
88L, 109L, 132L, 153L, 175L, 191L, 207L, 30L, 47L, 67L, 88L, 
106L, 130L, 24L, 47L, 71L, 96L, 123L, 146L, 33L, 52L, 72L, 94L, 
111L, 131L, 152L, 172L, 190L, 202L, 32L, 49L, 72L, 97L, 117L, 
145L, 166L, 188L, 202L, 214L, 60L, 80L, 101L, 116L, 139L, 155L, 
32L, 52L, 77L, 100L, 120L, 146L, 37L, 55L, 74L, 97L, 113L, 134L, 
151L, 171L, 190L, 203L, 38L, 58L, 83L, 104L, 124L, 147L, 170L, 
191L, 206L, 216L, 42L, 63L, 100L, 115L, 136L, 154L, 38L, 58L, 
92L, 111L, 136L, 161L, 44L, 66L, 89L, 107L, 128L, 147L, 167L, 
202L, 213L, 46L, 70L, 97L, 115L, 137L, 160L, 184L, 199L, 212L, 
220L, 48L, 69L, 96L, 115L, 139L, 157L, 46L, 70L, 94L, 112L, 135L, 
160L, 53L, 73L, 95L, 112L, 132L, 159L, 178L, 191L, 204L, 51L, 
74L, 99L, 117L, 144L, 165L, 185L, 199L, 211L, 218L, 53L, 102L, 
123L, 143L, 159L, 180L, 51L, 80L, 111L, 138L, 162L, 185L, 59L, 
81L, 101L, 117L, 138L, 155L, 177L, 191L, 205L, 215L, 59L, 82L, 
105L, 127L, 150L, 177L, 194L, 208L, 217L, 221L, 60L, 81L, 103L, 
141L, 197L, 62L, 87L, 112L, 135L, 158L, 182L, 69L, 91L, 108L, 
126L, 147L, 168L, 186L, 199L, 209L, 217L, 67L, 92L, 113L, 135L, 
156L, 180L, 196L, 210L, 219L, 222L, 68L, 90L, 110L, 129L, 148L, 
167L, 67L, 95L, 131L, 156L, 183L, 201L), .Label = c("05:00", 
"05:03", "05:10", "05:22", "05:23", "05:25", "05:30", "05:31", 
"05:32", "05:35", "05:36", "05:37", "05:38", "05:40", "05:48", 
"05:50", "05:51", "05:52", "05:53", "05:56", "05:57", "05:58", 
"05:59", "06:00", "06:02", "06:03", "06:06", "06:07", "06:08", 
"06:10", "06:11", "06:12", "06:15", "06:16", "06:17", "06:18", 
"06:19", "06:22", "06:23", "06:25", "06:26", "06:27", "06:28", 
"06:30", "06:32", "06:34", "06:35", "06:37", "06:38", "06:39", 
"06:40", "06:41", "06:42", "06:43", "06:44", "06:45", "06:48", 
"06:49", "06:50", "06:51", "06:52", "06:53", "06:54", "06:55", 
"06:56", "06:58", "07:00", "07:01", "07:02", "07:03", "07:05", 
"07:06", "07:07", "07:08", "07:09", "07:11", "07:12", "07:13", 
"07:14", "07:15", "07:16", "07:17", "07:18", "07:19", "07:20", 
"07:21", "07:22", "07:23", "07:25", "07:26", "07:27", "07:28", 
"07:29", "07:31", "07:33", "07:34", "07:35", "07:36", "07:37", 
"07:40", "07:41", "07:42", "07:43", "07:45", "07:46", "07:49", 
"07:51", "07:52", "07:55", "07:56", "07:57", "07:58", "08:00", 
"08:02", "08:03", "08:05", "08:06", "08:07", "08:08", "08:09", 
"08:11", "08:12", "08:13", "08:14", "08:15", "08:16", "08:18", 
"08:19", "08:20", "08:22", "08:23", "08:24", "08:25", "08:26", 
"08:28", "08:29", "08:30", "08:31", "08:32", "08:33", "08:35", 
"08:36", "08:37", "08:38", "08:39", "08:41", "08:43", "08:45", 
"08:47", "08:49", "08:50", "08:51", "08:52", "08:54", "08:56", 
"08:57", "08:58", "08:59", "09:00", "09:01", "09:02", "09:03", 
"09:04", "09:06", "09:07", "09:09", "09:10", "09:12", "09:13", 
"09:15", "09:16", "09:17", "09:18", "09:19", "09:20", "09:22", 
"09:23", "09:24", "09:25", "09:26", "09:28", "09:29", "09:31", 
"09:34", "09:35", "09:36", "09:37", "09:41", "09:42", "09:44", 
"09:48", "09:49", "09:53", "09:55", "09:57", "09:58", "09:59", 
"10:01", "10:03", "10:06", "10:08", "10:09", "10:11", "10:12", 
"10:14", "10:17", "10:18", "10:26", "10:29", "10:30", "10:31", 
"10:33", "10:34", "10:37", "10:39", "10:45", "10:55", "11:00", 
"11:02", "11:03", "11:24", "11:32"), class = "factor"), ord = c(3, 
12, 27, 47, 68, 93, 109, 130, 152, 173, 1, 11, 30, 50, 76, 102, 
124, 152, 176, 9, 25, 43, 61, 83, 105, 1, 17, 45, 84, 114, 136, 
167, 4, 16, 35, 79, 100, 118, 140, 158, 1, 7, 26, 48, 73, 101, 
121, 145, 167, 189, 5, 16, 35, 53, 79, 100, 2, 13, 31, 56, 82, 
107, 6, 19, 39, 57, 80, 101, 120, 143, 160, 180, 1, 7, 24, 43, 
67, 94, 118, 147, 169, 189, 8, 21, 78, 100, 1, 10, 28, 52, 82, 
107, 8, 23, 41, 62, 86, 105, 121, 159, 178, 7, 22, 41, 65, 92, 
114, 141, 164, 184, 198, 7, 20, 59, 81, 102, 7, 24, 44, 66, 92, 
111, 12, 26, 42, 61, 82, 103, 158, 178, 14, 28, 46, 67, 92, 110, 
135, 159, 182, 195, 16, 34, 71, 94, 109, 14, 29, 54, 80, 103, 
128, 15, 33, 54, 74, 113, 133, 171, 188, 16, 35, 58, 83, 104, 
125, 149, 174, 192, 207, 18, 34, 56, 75, 114, 16, 36, 56, 76, 
102, 122, 25, 43, 64, 85, 105, 124, 145, 163, 181, 193, 24, 41, 
63, 88, 109, 132, 153, 175, 191, 207, 30, 47, 67, 88, 106, 130, 
24, 47, 71, 96, 123, 146, 33, 52, 72, 94, 111, 131, 152, 172, 
190, 202, 32, 49, 72, 97, 117, 145, 166, 188, 202, 214, 60, 80, 
101, 116, 139, 155, 32, 52, 77, 100, 120, 146, 37, 55, 74, 97, 
113, 134, 151, 171, 190, 203, 38, 58, 83, 104, 124, 147, 170, 
191, 206, 216, 42, 63, 100, 115, 136, 154, 38, 58, 92, 111, 136, 
161, 44, 66, 89, 107, 128, 147, 167, 202, 213, 46, 70, 97, 115, 
137, 160, 184, 199, 212, 220, 48, 69, 96, 115, 139, 157, 46, 
70, 94, 112, 135, 160, 53, 73, 95, 112, 132, 159, 178, 191, 204, 
51, 74, 99, 117, 144, 165, 185, 199, 211, 218, 53, 102, 123, 
143, 159, 180, 51, 80, 111, 138, 162, 185, 59, 81, 101, 117, 
138, 155, 177, 191, 205, 215, 59, 82, 105, 127, 150, 177, 194, 
208, 217, 221, 60, 81, 103, 141, 197, 62, 87, 112, 135, 158, 
182, 69, 91, 108, 126, 147, 168, 186, 199, 209, 217, 67, 92, 
113, 135, 156, 180, 196, 210, 219, 222, 68, 90, 110, 129, 148, 
167, 67, 95, 131, 156, 183, 201)), row.names = c(NA, -401L), groups = structure(list(
    Week = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
    7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 
    11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L), Date = structure(c(18451, 
    18451, 18452, 18452, 18457, 18457, 18458, 18458, 18465, 18465, 
    18466, 18466, 18471, 18471, 18473, 18473, 18480, 18480, 18481, 
    18481, 18486, 18486, 18487, 18487, 18494, 18494, 18495, 18495, 
    18500, 18500, 18501, 18501, 18507, 18507, 18508, 18508, 18515, 
    18515, 18516, 18516, 18521, 18521, 18522, 18522, 18527, 18527, 
    18529, 18529, 18535, 18535, 18536, 18536), class = "Date"), 
    Observer = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("IF", 
    "KT"), class = "factor"), .rows = structure(list(1:10, 11:19, 
        20:25, 26:32, 33:40, 41:50, 51:56, 57:62, 63:72, 73:82, 
        83:86, 87:92, 93:101, 102:111, 112:116, 117:122, 123:130, 
        131:140, 141:145, 146:151, 152:159, 160:169, 170:174, 
        175:180, 181:190, 191:200, 201:206, 207:212, 213:222, 
        223:232, 233:238, 239:244, 245:254, 255:264, 265:270, 
        271:276, 277:285, 286:295, 296:301, 302:307, 308:316, 
        317:326, 327:332, 333:338, 339:348, 349:358, 359:363, 
        364:369, 370:379, 380:389, 390:395, 396:401), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 52L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
  • Your data seems to have no duplicate, then `mutate(ord=row_number())` just works well. – yh6 Mar 12 '21 at 12:46
  • @yh6 Sorry for that. My original data has many duplicates of `Week`, `Date`, `Observer` and `StartTime`--as many rows as the number of observations made. The example I provided does not have duplicates because of the `summarise()` function I used but I would like for the solution to work such that the duplicate rows have the same value of `ord` as well. – Karthik Thrikkadeeri Mar 12 '21 at 14:55

1 Answers1

1

Maybe using factor for hour but getting only the index..

df %>%
  group_by(
    Week, 
    Date, 
    Observer
  ) %>%
  summarise(
    StartTime = StartTime
  ) %>%
  arrange(
    StartTime
  ) %>%
  mutate(
    ord = as.integer(factor(StartTime))
  ) %>%
  arrange(
    Week, Date, Observer
  ) %>% View()
abreums
  • 166
  • 8
  • I tried your code, and it seems to be working. Could you please clarify the following: (1) `mutate(ord = as.integer(factor(StartTime)))` is the main player here? You mentioned "getting only the index" which I failed to understand. Also, `summarise()` instead of `mutate()` wouldn't be wrong, would it?. (2) `summarise(StartTime = StartTime)` in your code is simply to remove the `ord` column and has no other function? (3) The documentation of `arrange()` states that it doesn't respect grouping by default, so is it necessary to specify it here, or does the upstream `group_by()` get passed on? – Karthik Thrikkadeeri Mar 12 '21 at 18:20
  • when mutating an attribute to a factor `ord = factor(StartTime)` , R creates an index for each different StartTime value (see R factors for more details), although it shows the referenced value instead of the index. Casting to integer, `ord = as.integer(factor(StartTime))` explicit retrieves the index not the indexed value. It works because of the grouping criteria used before. – abreums Mar 12 '21 at 18:48
  • Understood, but what difference would using `as.factor()` instead of `factor()` make? This was one of the options I tried earlier. – Karthik Thrikkadeeri Mar 12 '21 at 19:07
  • 1
    "as.factor" seems not to be just a wrapper to "factor". There are differences. There's a good explanation here: https://stackoverflow.com/questions/39279238/why-use-as-factor-instead-of-just-factor – abreums Mar 12 '21 at 21:09