Shambhavi Srivastava Shambhavi Srivastava - 13 days ago 6x
R Question

R converting hour min in minutes

I have the dataset (named A) whose column 'TimeColumn' is shown in the snapshot.
I want to accomplish the following:

Step1: For all values in min, extract the digit only

Step2: For all values in hour and min, convert it to min and extract the resultant digit only

Step3: For each row, add the extracted value in a new column

So for example, if the entry is 119 min, I want to extract 119. If the entry is 1hr 30min, I want to extract 90.

I used the following code and it successfully extracts the digit. However, it doesn't accomplish the task when time is in hr min. E.g. the '1' in '1hr 30min' is only extracted but '30' is not.

for (num in seq(1,length(A$TimeColumn), by=1)) #replace 100 by nrow(A)
cat("Row number",num, "is",as.numeric(gsub("([0-9]+).*$", "\\1",A$TimeColumn[num] )))

Column containing time in min or in hr min


We can extract the numbers from the 'TimeColumn' using str_extract_all (from stringr) into a list, loop through the list elements (sapply(...)), convert it to 'numeric' ('x1'), if the length is greater than 1, then do the arithmetic to convert the hour to minutes or else return the minutes, and assign this to new column 'Min'.

A$Min <- sapply(str_extract_all(A$TimeColumn, "\\d+"), function(x) {
              x1 <- as.numeric(x)
          if(length(x1)>1) x1[1]*60 + x1[2] else x1 })
#[1]  98 119 105  90  92 135 104 100 104  93

A base R option would be to replace the 'h' with * 60 + using sub, remove the 'min' substring and do an evaluation

unname(sapply(sub("\\s+min", "", sub("h", "* 60 +", A$TimeColumn)), 
              function(x) eval(parse(text=x))))
#[1]  98 119 105  90  92 135 104 100 104  93


A <- structure(list(TimeColumn = c("98 min", "119 min", "105 min", 
"1h 30 min", "92 min", "135 min", "104 min", "100 min", "104 min", 
"93 min")), .Names = "TimeColumn", row.names = c(NA, -10L), 
class = "data.frame")