Home > Enterprise >  Creating a loop using non-unique ID-numbers in R instead of using rownumbers for the iteration
Creating a loop using non-unique ID-numbers in R instead of using rownumbers for the iteration

Time:10-13

Really struggled to come up with a proper title but hopefully, this will explain what my problem is:

Let's say I have a dataset (or vector) where one column is a numerical Frame_ID column but they are not unique IDs. For example Frame_ID = c( 2 , 2 ,2 , 3 , 3 , 3 , 4 , 4 , 5 , 5 , 5 , 6 , 6 , 6 , 7 , 7 , 8 , 8 , 8 , 9 , 10 , 10 , 10 , 11 etc.) and this continues until Frame_ID=159753, without any specific pattern or rule that would explain how many times a Frame_ID is repeated. Now I would like to add for every 30 Frame_IDs a corresponding timestamp (1 second per 30 Frame_IDs) in a new column, so it eventually looks like this:

Frame_ID Timestamp
2 14:30:19
2 14:30:19
2 14:30:19
3 14:30:19
... ....
30 14:30:19
31 14:30:20
31 14:30:20

I created this function but besides not really working conceptually I believe, it also uses the row numbers and not the actual Frame_ID values for i. How can I work it out so that it treats the actual Frame_ID values for the iterations and i and not the rownumbers? Hope that makes sense.

Thanks already in advance, any hint is highly appreciated.

real_time <- c()
start = hms('14:30:19')
for (i in 0:159753) {
  start <- data$Frame_ID[i:i 30]
  real_time[i] <- start
  i = i   30
  start = start   1
  print(real_time)
}

CodePudding user response:

Using loops sounds inefficient here. Would this work for you?

data %>%
  mutate(Timestamp = as.character(hms('14:30:19')   floor(Frame_ID/30)))

CodePudding user response:

df <- data.frame(Frame_ID = c(2,2,2,3,30,31,31,39,59,60,69))

df$grp <- floor(df$Frame_ID/30)

df$Timestamp <- as.POSIXct("2000/01/01 14:30:19")   df$grp
df
#>    Frame_ID grp           Timestamp
#> 1         2   0 2000-01-01 14:30:19
#> 2         2   0 2000-01-01 14:30:19
#> 3         2   0 2000-01-01 14:30:19
#> 4         3   0 2000-01-01 14:30:19
#> 5        30   1 2000-01-01 14:30:20
#> 6        31   1 2000-01-01 14:30:20
#> 7        31   1 2000-01-01 14:30:20
#> 8        39   1 2000-01-01 14:30:20
#> 9        59   1 2000-01-01 14:30:20
#> 10       60   2 2000-01-01 14:30:21
#> 11       69   2 2000-01-01 14:30:21

format(df$Timestamp, format = "%H:%M:%S")
#>  [1] "14:30:19" "14:30:19" "14:30:19" "14:30:19" "14:30:20" "14:30:20"
#>  [7] "14:30:20" "14:30:20" "14:30:21" "14:30:21" "14:30:21"
Created on 2021-10-13 by the reprex package (v2.0.1)
  • Related