0

I have data frame with my time as a factor currently in form 00:00:00 (min:sec:millisec). My dataset spans 30 minutes of time. I want to bin each min so I will have 30 bins in order to visualize the frequency of my observations for each minute. I have lots of these data frames and want to try and avoid making extraneous variables. Dataset

>dput(head(attackfemale1,20))

structure(list(X = c(9L, 17L, 33L, 36L, 46L, 62L, 81L, 102L, 
168L, 201L, 257L, 259L, 300L, 303L, 308L, 312L, 375L), frame = c(54619L, 
55577L, 57427L, 57847L, 58743L, 60145L, 61823L, 64373L, 72701L, 
75933L, 83509L, 84173L, 90563L, 90831L, 91647L, 92115L, 101641L
), time.min.sec. = structure(1:17, .Label = c("30:20.63", "30:52.57", 
"31:54.23", "32:08.23", "32:38.10", "33:24.83", "34:20.77", "35:45.77", 
"40:23.37", "42:11.10", "46:23.63", "46:45.77", "50:18.77", "50:27.70", 
"50:54.90", "51:10.50", "56:28.03"), class = "factor"), command = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = "attack female", class = "factor"), ID = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = "XCB1", class = "factor")), row.names = c(NA, 17L
), class = "data.frame")
> 

aychang
  • 1
  • 1
  • Possible, yes. Good practice? We cannot say without having a look on your data; see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Questionable if treating time as factor is best in this case. – Pax Jun 07 '22 at 15:27
  • I have added an image of the dataset for context – aychang Jun 07 '22 at 15:35
  • Is this what the provided link recommends? – Pax Jun 07 '22 at 15:37
  • 1
    If your data frame is called `df`, please paste `dput(head(df,20))` into the original question. As @Pax is indicating, help can be provided to you, but the optimal approach will depend on your colleagues knowing the structure/type etc of the actual data. – langtang Jun 07 '22 at 15:56

1 Answers1

0

So, if your data looks roughly as follows (according to your screenshot):

data <- data.frame(
  time = c("01:02.11", "01:04.11", "03:23.32"),
  value = c(1, 2, 3)
)

Is this what you need?

data[ ,"minute"] <- as.factor(substring(data[, "time"], 1, 2))

print(data)

#       time value minute
# 1 01:02.11     1     01
# 2 01:04.11     2     01
# 3 03:23.32     3     03
Daniel Hoop
  • 652
  • 1
  • 5
  • 16