Home > Software engineering >  Create categorical variable based on time or index range/interval in R
Create categorical variable based on time or index range/interval in R

Time:06-14

The following dataset contains multiple variables (e.g., x1.AI_mean) values measured per minute (x1.time) during one day. Making it 1440 observations per day. I want to create a categorical variable that gives first 1440 observations ID1, the next 1440 observations (so the second day) ID2 and so forth stored in a new variable. Is this possible by creating an interval range [1;1440] or based on the x1.time variable?

    X  x1.time x1.AI_mean x1.MAD_mean  x1.SD_mean x1.ENMO_t_mean ...
1   1 00:00:00  0.3233516 0.003415065 0.004775937     0.02392688 ...
2   2 00:01:00  0.2841591 0.003029287 0.004059719     0.02370636 ...
3   3 00:02:00  0.2397491 0.002784242 0.003544154     0.02370148 ...
4   4 00:03:00  0.2307666 0.002693856 0.003508254     0.02398960 ...
5   5 00:04:00  0.2444569 0.002704939 0.003618171     0.02434014 ...
6   6 00:05:00  0.2226963 0.002611358 0.003302321     0.02355491 ...
7   7 00:06:00  0.2238880 0.002650730 0.003380582     0.02341979 ...
8   8 00:07:00  0.3994381 0.004197095 0.009734539     0.02383396 ...
9   9 00:08:00  0.9210158 0.010238607 0.017880868     0.02517337 ...
10 10 00:09:00  0.2344564 0.002713301 0.003502785     0.02341302 ...
11 11 00:10:00  0.3451940 0.003814439 0.006974292     0.02412211 ...
......

Thanks in advance :)

CodePudding user response:

Try

paste0("ID", ceiling(1:nrow(df) / 1440))

or

paste0("ID", (1:nrow(df)-1) %/% 1440   1)
  • Related