'Time-intervals match

Assume we have the following time use data for a 6-hour period from 4 pm to 10 pm I'd like to calculate the number of minutes that the same measurement was taken using the formula below. Each measure has an id number and a daynum suggesting when the measurement was taken. There are more than 1000 cases. How can I identify the match based on id (eg. individual measurements) and general?

Output for Id 1

enter image description here

Output for Id 2

enter image description here

Output for General

enter image description here

Sample data:

   structure(list(id = c("1", "1", "2", "2", "2", "Match"), daynum = c(1, 
2, 1, 2, 3, NA), t0 = c("A", "D", "A", "A", "A", NA), t1 = c("A", 
"D", "A", "A", "A", NA), t2 = c("A", "D", "A", "A", "A", NA), 
    t3 = c("A", "D", "A", "A", "A", NA), t4 = c("A", "D", "A", 
    "A", "A", NA), t5 = c("A", "D", "A", "A", "A", NA), t6 = c("A", 
    "E", "A", "A", "A", NA), t7 = c("A", "E", "A", "A", "A", 
    NA), t8 = c("A", "E", "A", "A", "A", NA), t9 = c("A", "A", 
    "A", "A", "A", "15"), t10 = c("A", "A", "A", "B", "A", NA
    ), t11 = c("B", "A", "A", "B", "A", NA), t12 = c("B", "A", 
    "A", "B", "A", NA), t13 = c("C", "A", "A", "B", "C", NA), 
    t14 = c("C", "A", "C", "B", "C", NA), t16 = c("C", "A", "C", 
    "B", "C", NA), t17 = c("C", "A", "C", "B", "C", NA), t18 = c("B", 
    "A", "C", "B", "B", NA), t19 = c("B", "A", "B", "B", "B", 
    NA), t20 = c("A", "A", "B", "B", "B", NA), t21 = c("B", "B", 
    "B", "B", "B", "15"), t22 = c("B", "B", "B", "B", "B", "15"
    ), t23 = c("B", "B", "B", "B", "B", "15"), t24 = c("B", "B", 
    "B", "B", "B", "15")), row.names = c(NA, -6L), spec = structure(list(
    cols = list(id = structure(list(), class = c("collector_character", 
    "collector")), daynum = structure(list(), class = c("collector_double", 
    "collector")), t0 = structure(list(), class = c("collector_character", 
    "collector")), t1 = structure(list(), class = c("collector_character", 
    "collector")), t2 = structure(list(), class = c("collector_character", 
    "collector")), t3 = structure(list(), class = c("collector_character", 
    "collector")), t4 = structure(list(), class = c("collector_character", 
    "collector")), t5 = structure(list(), class = c("collector_character", 
    "collector")), t6 = structure(list(), class = c("collector_character", 
    "collector")), t7 = structure(list(), class = c("collector_character", 
    "collector")), t8 = structure(list(), class = c("collector_character", 
    "collector")), t9 = structure(list(), class = c("collector_character", 
    "collector")), t10 = structure(list(), class = c("collector_character", 
    "collector")), t11 = structure(list(), class = c("collector_character", 
    "collector")), t12 = structure(list(), class = c("collector_character", 
    "collector")), t13 = structure(list(), class = c("collector_character", 
    "collector")), t14 = structure(list(), class = c("collector_character", 
    "collector")), t16 = structure(list(), class = c("collector_character", 
    "collector")), t17 = structure(list(), class = c("collector_character", 
    "collector")), t18 = structure(list(), class = c("collector_character", 
    "collector")), t19 = structure(list(), class = c("collector_character", 
    "collector")), t20 = structure(list(), class = c("collector_character", 
    "collector")), t21 = structure(list(), class = c("collector_character", 
    "collector")), t22 = structure(list(), class = c("collector_character", 
    "collector")), t23 = structure(list(), class = c("collector_character", 
    "collector")), t24 = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))


Solution 1:[1]

This code fiters the dataframe according to 'id' and 'daynum' and returns a 15 if there are only matches in the column..

df = df[1:5, ] #did this to exclude the 'Match' row of the sample data


find_match = function (df,  id = F, daynum = F){
  #subset the data frame after id value
  if(is.numeric(id)){
    df = df[df$id == id,]
  }
  
  if(is.numeric(daynum)){
    df = df[df$daynum == daynum,]
  }
  
  apply(df, 2, function(x){
    col_unique = length(unique(x))
    if(col_unique == 1){return(15)}
    else{return(NA)}
  })

}

find_match(df, id = 1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tlhenvironment