rdatetimematrixfind-occurrences

R: table function for objects in datetime string?


I have a large matrix with a column of datetime information.

The structure is: '2022-01-01_0545'(no seconds).

I'm looking for an effective way to get the number of occurrences of each year, month and day in my matrix. What could be a good approach?

Could I use the table() function or is it better to split the string into its components?

Example:

dates <- c("2022-01-01_0545","2022-03-01_0810","2021-12-13_1003","2022-09-10_0400","2022-03-09_1802")
data <- matrix(0 , nrow = 5, ncol = 2)
data <- cbind(data, dates)
colnames(data) <- NULL

Another example using dput(data[1:5, 10:11):

structure(c("290.603961274028", "281.885433495045", "283.438215255737", 
"275.935544893146", "284.739524498582", "2018-01-01_0000", "2018-01-03_0445", 
"2018-03-07_0045", "2018-01-04_0700", "2018-02-09_0015"), dim = c(5L, 
2L))

Solution

  • Calling your matrix m, here's using substr to extract the components and table() to make tables.

    years = substr(m[, 2], 1, 4)
    months = substr(m[, 2], 6, 7)
    days = substr(m[, 2], 9, 10)
    
    table(years)
    # years
    # 2018 
    #    5 
       
    table(months)
    # months
    # 01 02 03 
    #  3  1  1 
     
    table(days)
    # days
    # 01 03 04 07 09 
    #  1  1  1  1  1