0

I have the following columns in a dataframe which difference between each row is 0.012 s :

Time
 0
 0.012
 0.024
 0.036
 0.048
 0.060
 0.072
 0.084
 0.096
 0.108

I want to come up with intervals starting from beginning increasing by 0.030, so intervals or time window of every 0.03 later to be used in group by.

chessosapiens
  • 3,159
  • 10
  • 36
  • 58
  • Could you please include failed attempts such that those can be ruled out? – NelsonGon Sep 12 '19 at 11:32
  • 1
    i was going to use Cut , or findinterval but i don't have any number of blocks, the only thing i have this 0.03 window length – chessosapiens Sep 12 '19 at 11:34
  • Alright, could you please copy and paste what the data would look like after transformation? I'm personally more of a look at the data person(someone else probably is so it might help them too). – NelsonGon Sep 12 '19 at 11:36

1 Answers1

2

You can try findInterval like

findInterval(df$Time, seq(min(df$Time), max(df$Time), 0.03))
#[1] 1 1 1 2 2 3 3 3 4 4

We can also use cut

breaks <- seq(min(df$Time), max(df$Time), 0.03)
cut(df$Time, c(breaks, Inf), labels = breaks, include.lowest = TRUE)
#[1] 0    0    0    0.03 0.03 0.03 0.06 0.06 0.09 0.09

data

df <- structure(list(Time = c(0, 0.012, 0.024, 0.036, 0.048, 0.06, 
0.072, 0.084, 0.096, 0.108)), class = "data.frame", row.names = c(NA, -10L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • how can we modify time interval in a way that each group be represented by it's starting time not 1 or 2, data is too large that it is not possible to set them manually. – chessosapiens Sep 12 '19 at 11:48
  • @chessosapiens Can you show what output you expect ? – Ronak Shah Sep 12 '19 at 12:09
  • `lowest <- formatC(min(df$Time), digits = 4, format = "f")` `highest <- formatC(max(df$Time) + 1, digits = 4, format = "f") ` and then something like `df$Time_intervals <-cut(df$Time, breaks = seq(lowest, highest, 0.030), include.lowest = TRUE)` should append the intervals, if that is what you want. – deepseefan Sep 12 '19 at 12:15
  • @RonakShah either of these: 0 , 0.03 , 0.06 , 0.09 ,,, or first row in each integral : 0 , 0.036 , 0.072 , 0.096 – chessosapiens Sep 12 '19 at 12:23
  • @chessosapiens We can use `cut` in that case. See updated answer. – Ronak Shah Sep 12 '19 at 12:42