0

Let's say I have this

+-------+-----+------+
| Month | Day | Hour |
+-------+-----+------+
|     1 |   1 |    1 |
|     1 |   1 |    2 |
|     1 |   1 |    3 |
|     1 |   1 |    4 |
|     1 |   2 |    1 |
|     1 |   2 |    2 |
|     1 |   2 |    3 |
|     1 |   2 |    4 |
|     2 |   1 |    1 |
|     2 |   1 |    2 |
|     2 |   1 |    3 |
|     2 |   1 |    4 |
+-------+-----+------+

I would like to cut by month and day factors to have this

+-------+-----+------+-------+
| Month | Day | Hour | Block |
+-------+-----+------+-------+
|     1 |   1 |    1 | [1,2] |
|     1 |   1 |    2 | [1,2] |
|     1 |   1 |    3 | [3,4] |
|     1 |   1 |    4 | [3,4] |
|     1 |   2 |    1 | [1,2] |
|     1 |   2 |    2 | [1,2] |
|     1 |   2 |    3 | [3,4] |
|     1 |   2 |    4 | [3,4] |
|     2 |   1 |    1 | [1,2] |
|     2 |   1 |    2 | [1,2] |
|     2 |   1 |    3 | [3,4] |
|     2 |   1 |    4 | [3,4] |
+-------+-----+------+-------+

I thought that maybe using by or tapply could be a way but I cannot figure how.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
Wistar
  • 3,770
  • 4
  • 45
  • 70
  • 3
    Please post code from R instead of ascii tables which are difficult to copy and paste – Pierre L Mar 12 '16 at 22:13
  • 1
    I guess one of the group by operations should do it `library(dplyr); df %>% group_by(Month, Day) %>% mutate(Block = cut(Hour, 4))` – akrun Mar 12 '16 at 22:17
  • see [here](http://stackoverflow.com/q/5963269/4303162) for many suggestions on how to provide data in an R question. Most of the time, just posting the output of `dput(data)` is the best option. – Stibu Mar 12 '16 at 22:18
  • Use `cut(x, c(1,seq(2,24, by=2)), include.lowest=TRUE)` – Pierre L Mar 12 '16 at 22:35
  • Cutting hour by month and day makes no sense at all. Or are they just supposed to be random names, like x, y and z? – Hong Ooi Mar 12 '16 at 22:44
  • @PierreLafortune I'll make sure to provide `dput` – Wistar Mar 12 '16 at 22:51
  • @Hond Ooi x, y and z for example – Wistar Mar 12 '16 at 22:51

1 Answers1

0

We can create a sequence for each hour of the day with cut and replace parantheticals with brackets:

df1$Block <- cut(df1$Hour, c(1,seq(2,24, by=2)), include.lowest=TRUE)
df1$Block <- sub("(", "[", df1$Block, fixed=T)
df1
#    Month Day Hour Block
# 1      1   1    1 [1,2]
# 2      1   1    2 [1,2]
# 3      1   1    3 [2,4]
# 4      1   1    4 [2,4]
# 5      1   2    1 [1,2]
# 6      1   2    2 [1,2]
# 7      1   2    3 [2,4]
# 8      1   2    4 [2,4]
# 9      2   1    1 [1,2]
# 10     2   1    2 [1,2]
# 11     2   1    3 [2,4]
# 12     2   1    4 [2,4]
Pierre L
  • 28,203
  • 6
  • 47
  • 69