-3

我希望有人可以帮助我解决这个问题,

我有一个 df,其中一列是时间序列。

我需要根据时间将这些时间序列分组为 4 个不同的值。我想将时间按 07:00 - 11:30、11:31 - 16:59、17:00 - 19:59 和最后 20:00 - 24:00 分组。每个组可以命名为 1、2、3 和 4。

最后将该列添加到我的 df.

最好的问候和感谢!

编辑添加一个可重现的例子:

c( "07:02:03", "07:32:03", "08:02:03", "08:32:03", 
"09:02:03", "09:32:03", "10:02:03", "10:32:03", "11:02:03", "11:32:03", 
"12:02:03", "12:32:03", "13:02:03", "13:32:03", "14:02:03", "14:32:03", 
"15:02:03", "15:32:03", "16:02:03", "16:32:03", "17:02:03", "17:32:03", 
"18:02:03", "18:32:03", "19:02:03", "19:32:03", "20:02:03", "20:32:03", 
"21:02:03", "21:32:03", "22:02:03", "22:32:03", "23:02:03", "23:32:03", 
"00:02:03", "00:32:03", "01:02:03", "01:32:03", "02:02:03", "02:32:03", 
"03:02:03", "03:32:03", "04:02:03", "04:32:03", "05:02:03", "05:32:03", 
"06:02:03", "06:32:03", "07:02:03", "07:32:03", "08:02:03", "08:32:03", 
"09:02:03", "09:32:03", "10:02:03", "10:32:03", "11:02:03", "11:32:03", 
"12:02:03", "12:32:03", "13:02:03", "13:32:03", "14:02:03", "14:32:03", 
"15:02:03", "15:32:03", "16:02:03", "16:32:03", "17:02:03", "17:32:03", 
"18:02:03", "18:32:03", "19:02:03", "19:32:03", "20:02:03", "20:32:03", 
"21:02:03", "21:32:03", "22:02:03", "22:32:03", "23:02:03", "23:32:03", 
"00:02:03", "00:32:03", "01:02:03", "01:32:03", "02:02:03", "02:32:03", 
"03:02:03", "03:32:03", "04:02:03", "04:32:03", "05:02:03", "05:32:03", 
"06:02:03", "06:32:03", "07:02:03", "07:32:03", "08:02:03", "08:32:03", 
"09:02:03", "09:32:03", "10:02:03", "10:32:03", "11:02:03", "11:32:03", 
"12:02:03", "12:32:03", "13:02:03", "13:32:03", "14:02:03", "14:32:03", 
"15:02:03", "15:32:03", "16:02:03", "16:32:03", "17:02:03", "17:32:03", 
"18:02:03", "18:32:03", "19:02:03", "19:32:03", "20:02:03", "20:32:03", 
"21:02:03", "21:32:03", "22:02:03", "22:32:03", "23:02:03", "23:32:03", 
"00:02:03", "00:32:03", "01:02:03", "01:32:03", "02:02:03", "02:32:03", 
"03:02:03", "03:32:03", "04:02:03", "04:32:03", "05:02:03", "05:32:03", 
"06:02:03", "06:32:03", "07:02:03", "07:32:03", "08:02:03", "08:32:03", 
"09:02:03", "09:32:03", "10:02:03", "10:32:03", "11:02:03", "11:32:03", 
"12:02:03", "12:32:03", "13:02:03", "13:32:03", "14:02:03", "14:32:03", 
"15:02:03", "15:32:03", "16:02:03", "16:32:03", "17:02:03", "17:32:03", 
"18:02:03", "18:32:03", "19:02:03", "19:32:03", "20:02:03", "20:32:03", 
"21:02:03", "21:32:03", "22:02:03", "22:32:03", "23:02:03", "23:32:03", 
"00:02:03")

-Hlynur

EDIT2:添加样本df,

a  b  c
1  3  09:00
2  5  13:00
3  8  16:00
4  7  21:00
5  9  19:00
6  2  22:00

......

然后 d 列会给我组 (1,2,2,4,3,4)

4

1 回答 1

0

这是一种方法。您将时间戳从字符转换为整数,只需将它们的值比较在特定范围内,然后使用相应地为结果列分配一个值ifelse

df <- structure(list(a = 1:100, b = c(100L, 99L, 98L, 97L, 96L, 95L, 94L, 93L, 92L, 91L, 90L, 89L, 88L, 87L, 86L, 85L, 84L, 83L, 
    82L, 81L, 80L, 79L, 78L, 77L, 76L, 75L, 74L, 73L, 72L, 71L, 70L, 69L, 68L, 67L, 66L, 65L, 64L, 63L, 62L, 61L, 60L, 59L, 58L, 
    57L, 56L, 55L, 54L, 53L, 52L, 51L, 50L, 49L, 48L, 47L, 46L, 45L, 44L, 43L, 42L, 41L, 40L, 39L, 38L, 37L, 36L, 35L, 34L, 33L, 
    32L, 31L, 30L, 29L, 28L, 27L, 26L, 25L, 24L, 23L, 22L, 21L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 
    6L, 5L, 4L, 3L, 2L, 1L), c = c("07:02:03", "07:32:03", "08:02:03", "08:32:03", "09:02:03", "09:32:03", "10:02:03", "10:32:03", 
    "11:02:03", "11:32:03", "12:02:03", "12:32:03", "13:02:03", "13:32:03", "14:02:03", "14:32:03", "15:02:03", "15:32:03", "16:02:03", 
    "16:32:03", "17:02:03", "17:32:03", "18:02:03", "18:32:03", "19:02:03", "19:32:03", "20:02:03", "20:32:03", "21:02:03", "21:32:03", 
    "22:02:03", "22:32:03", "23:02:03", "23:32:03", "00:02:03", "00:32:03", "01:02:03", "01:32:03", "02:02:03", "02:32:03", "03:02:03", 
    "03:32:03", "04:02:03", "04:32:03", "05:02:03", "05:32:03", "06:02:03", "06:32:03", "07:02:03", "07:32:03", "08:02:03", "08:32:03", 
    "09:02:03", "09:32:03", "10:02:03", "10:32:03", "11:02:03", "11:32:03", "12:02:03", "12:32:03", "13:02:03", "13:32:03", "14:02:03", 
    "14:32:03", "15:02:03", "15:32:03", "16:02:03", "16:32:03", "17:02:03", "17:32:03", "18:02:03", "18:32:03", "19:02:03", "19:32:03", 
    "20:02:03", "20:32:03", "21:02:03", "21:32:03", "22:02:03", "22:32:03", "23:02:03", "23:32:03", "00:02:03", "00:32:03", "01:02:03", 
    "01:32:03", "02:02:03", "02:32:03", "03:02:03", "03:32:03", "04:02:03", "04:32:03", "05:02:03", "05:32:03", "06:02:03", "06:32:03", 
    "07:02:03", "07:32:03", "08:02:03", "08:32:03")), .Names = c("a", "b", "c"), row.names = c(NA, -100L), class = "data.frame")

df$d <- as.numeric(gsub(":", "", df$c))

df$result <- ifelse(df$d >= 70000 & df$d < 113100, 1, 
                    ifelse(df$d >= 113100 & df$d < 170000, 2, 
                    ifelse(df$d >= 170000 & df$d < 200000, 3, 
                    ifelse(df$d >= 200000 & df$d < 240000, 4, 0))))

df$d <- NULL

head(df, 20)
##     a   b        c result
## 1   1 100 07:02:03      1
## 2   2  99 07:32:03      1
## 3   3  98 08:02:03      1
## 4   4  97 08:32:03      1
## 5   5  96 09:02:03      1
## 6   6  95 09:32:03      1
## 7   7  94 10:02:03      1
## 8   8  93 10:32:03      1
## 9   9  92 11:02:03      1
## 10 10  91 11:32:03      2
## 11 11  90 12:02:03      2
## 12 12  89 12:32:03      2
## 13 13  88 13:02:03      2
## 14 14  87 13:32:03      2
## 15 15  86 14:02:03      2
## 16 16  85 14:32:03      2
## 17 17  84 15:02:03      2
## 18 18  83 15:32:03      2
## 19 19  82 16:02:03      2
## 20 20  81 16:32:03      2

tail(df, 20)
##       a  b        c result
## 81   81 20 23:02:03      4
## 82   82 19 23:32:03      4
## 83   83 18 00:02:03      0
## 84   84 17 00:32:03      0
## 85   85 16 01:02:03      0
## 86   86 15 01:32:03      0
## 87   87 14 02:02:03      0
## 88   88 13 02:32:03      0
## 89   89 12 03:02:03      0
## 90   90 11 03:32:03      0
## 91   91 10 04:02:03      0
## 92   92  9 04:32:03      0
## 93   93  8 05:02:03      0
## 94   94  7 05:32:03      0
## 95   95  6 06:02:03      0
## 96   96  5 06:32:03      0
## 97   97  4 07:02:03      1
## 98   98  3 07:32:03      1
## 99   99  2 08:02:03      1
## 100 100  1 08:32:03      1
于 2013-03-15T08:16:00.060 回答