繁体   English   中英

如何使用 R 将用文字 (Xhours Xminutes) 写入的持续时间转换为数字?

[英]How can I convert time duration written as' in words (Xhours Xminutes) to numbers using R?

我正在使用将时间写成文字的数据......

time <- c('1 hour 1 minute', '2 hours 3 minutes', '45 minutes')

我想将这些时间转换为以下格式...

time <- c(61,123,45)

我想知道是否有人有使用 r 的简单方法?

hours <- stringr::str_extract_all(time, "[0-9]+(?=\\s*hour)")
hours <- replace(hours, sapply(hours, Negate(length)), "0")
hours <- as.integer(unlist(hours))
hours
# [1] 1 2 0
minutes <- stringr::str_extract_all(time, "[0-9]+(?=\\s*minute)")
minutes <- replace(minutes, sapply(minutes, Negate(length)), "0")
minutes <- as.integer(unlist(minutes))
minutes
# [1]  1  3 45
hours*60 + minutes
# [1]  61 123  45

或者更紧凑一点:

mtx <- cbind(
  stringr::str_extract_all(time, "[0-9]+(?=\\s*hour)", TRUE), 
  stringr::str_extract_all(time, "[0-9]+(?=\\s*minute)", TRUE))
storage.mode(mtx) <- "numeric"
mtx[is.na(mtx)] <- 0
mtx
#      [,1] [,2]
# [1,]    1    1
# [2,]    2    3
# [3,]    0   45
60 * mtx[,1] + mtx[,2]
# [1]  61 123  45

### or
mtx %*% c(60, 1)
#      [,1]
# [1,]   61
# [2,]  123
# [3,]   45

https://stackoverflow.com/a/20791975建议使用storage.mode

也许您可以将 digit-space-"hour(s)" 替换为 "+digit*60" 并将 digit-"minute(s)" 替换为 "+digit" 并评估表达式。

library(tidyverse) #or stringr and purrr

time %>% 
  str_replace('(\\d+) hour(s)?', '+\\1*60') %>% 
  str_replace('(\\d+) minute(s)?', '+\\1') %>% 
  map_dbl(~ eval(parse(text = .)))

# [1]  61 123  45

用你自己的 function 试试:

mtime = function(tx){
  utx = unlist(strsplit(tx,split=" ",perl=T))
  if (length(utx)==4) {
    sum(as.numeric(utx[c(1,3)])*c(60,1))
  } else as.numeric(utx[c(1)]) 
}

stime = sapply(time,mtime)

# 1 hour 1 minute 2 hours 3 minutes        45 minutes 
#              61               123                45 

对于你的例子,下一个作品:

time <- sapply(sub("minutes*","*1",sub("hours*","*60 +",time)),function(x) eval(parse(text=x)), USE.NAMES = FALSE)
time

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM