-3

誰かがこの質問で私を助けてくれることを願っています.

1 つの列が時系列である df があります。

これらの時系列を、時間に基づいて 4 つの異なる値にグループ化する必要があります。時刻を 07:00 ~ 11:30、11:31 ~ 16:59、17:00 ~ 19:59、最後に 20:00 ~ 24:00 にグループ化したい。各グループに 1、2、3、および 4 という名前を付けることができます。

最後に、その列を df に追加します。

よろしくお願いします!

EDIT再現可能な例を追加します。

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

ここにそれを行う1つの方法があります。タイムスタンプを文字から整数に変換し、それらの値が特定の範囲内にあることを比較し、それに応じて結果列に値を割り当てます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 に答える