2011-11-20 6 views
5

私は時間関数を変換するR関数のヘルプを全部探しています「00:15:00」や「01:00:00」や「00:00:06」や「1960年代」のような日時オブジェクトには「15分」「1時間」「6秒」「1日」のいずれかを入力します。 01-02 00:00:00 "(これはわからない)私はこのような関数が存在することを確信しているか、それをプログラミングするのを避けるためのきちんとした方法があります...timespanから(例えば「15分」または「2秒」)から「00:15:00」または「00:00:02」

もっと具体的には、以下のようにしたいと思います。関数名transform.span.to.time ):

times("00:15:00") 

は午後12時15分00秒「のようなものを返すtransform.span.to.time( "15分")のような関数と同様に同じ結果が得られるはず

library(chron) 

times(transform.span.to.time("15 min")) 

"存在するか、それを行うにはどのようにトリックが存在するのですか?

答えて

3

基底関数?cut.POSIXtbreaksの値の指定されたセットは、この作業を行います。

breaks: a vector of cut points _or_ number giving the number of 
     intervals which ‘x’ is to be cut into *_or_ an interval 
     specification, one of ‘"sec"’, ‘"min"’, ‘"hour"’, ‘"day"’, 
     ‘"DSTday"’, ‘"week"’, ‘"month"’, ‘"quarter"’ or ‘"year"’, 
     optionally preceded by an integer and a space, or followed by 
     ‘"s"’. For ‘"Date"’ objects only ‘"day"’, ‘"week"’, 
     ‘"month"’, ‘"quarter"’ and ‘"year"’ are allowed.* 

cut.POSIXtを入力してソースコードを参照してください、関連するセクションは、この始まる:

else if (is.character(breaks) && length(breaks) == 1L) { 

このセクションのコードを採用して、あなたのニーズに対応することができます。

6

数字と単位を区切る1つのスペースと、「秒」単位の後にスペースを入れないものとします。これは、混合ユニットを扱います:

test <- "0 hours 15 min 0 secs" 

transform.span <- function(test){ 
    testh <-   if(!grepl(" hour | hours ", "0 hours 15 min 0 secs")){ 
      # First consequent if no hours 
             sub("^", "0:", test)} else { 
             sub(" hour | hours ", ":", test)} 
    testm <- if(!grepl(" min | minutes ", testh)) {  
      # first consequent if no minutes 
             sub(" min | minutes ", "0:", testh)} else{ 
             sub(" min | minutes ", ":", testh) } 

    test.s <- if(!grepl(" sec| secs| seconds", testm)) { 
       # first consequent if no seconds 
             sub(" sec| secs| seconds", "0", testm)} else{ 
             sub(" sec| secs| seconds", "", testm)} 

    return(times(test.s)) } 
### Use 
> transform.span(test) 
[1] 00:15:00 
> test2 <- "21 hours 15 min 38 secs" 
> transform.span(test2) 
[1] 21:15:38 
2

@DWin:ありがとうございます。 DWIN例に基づいて

は、私は少しを再配置し、ここでの結果である:

transform.span<-function(timeSpan) { 
    timeSpanH <- if(!grepl(" hour | hours | hour| hours|hour |hours |hour|hours", timeSpan)) { 
       # First consequent if no hours 
       sub("^", "00:", timeSpan) 
      } else { 
       sub(" hour | hours | hour| hours|hour |hours |hour|hours", ":", timeSpan) 
      } 
    timeSpanM <- if(!grepl(" min | minutes | min| minutes|min |minutes |min|minutes", timeSpanH)) {  
       # first consequent if no minutes 
       paste("00:", timeSpanH, sep="") 
      } else{ 
       sub(" min | minutes | min| minutes|min |minutes |min|minutes", ":", timeSpanH) 
      } 
    timeSpanS <- if(!grepl(" sec| secs| seconds|sec|secs|seconds", timeSpanM)) { 
       # first consequent if no seconds 
       paste(timeSpanM, "00", sep="") 
      } else{ 
       sub(" sec| secs| seconds|sec|secs|seconds", "", timeSpanM) 
      } 

    return(timeSpanS) 
} 
### Use 
test <- "1 hour 2 min 1 sec" 
times(transform.span(test)) 
test1hour <- "1 hour" 
times(transform.span(test1hour)) 
test15min <- "15 min" 
times(transform.span(test15min)) 
test4sec <- "4 sec" 
times(transform.span(test4sec)) 
3

あなたはdifftimeと期間を定義することができます。たとえば

span2time <- function(span, units = c('mins', 'secs', 'hours')) { 
    span.dt <- as.difftime(span, units = match.arg(units)) 
    format(as.POSIXct("1970-01-01") + span.dt, "%H:%M:%S") 
} 

を:

> span2time(15) 
[1] "00:15:00" 

EDIT:chronのに受け入れ可能な文字列を生成するように変更されました。。

4

最初の解決策は、gsubfnパッケージでstrapplyを使用して、日に変換します。 1時間は1/24日です。 2番目の解は、Rの式に変換され、日の数を計算し、それを評価します。

library(chron) 

transform.span.to.time2 <- function(x) { 
    x <- paste(x, 0) 
    x <- sub("h\\w*", "*3600+", x, ignore.case = TRUE) 
    x <- sub("m\\w*", "*60+", x, ignore.case = TRUE) 
    x <- sub("s\\w*", "+", x, ignore.case = TRUE) 
    unname(sapply(x, function(x) eval(parse(text = x)))/(24*3600)) 
} 

試験:

> x <- c("12 hours 3 min 1 sec", "22h", "18 MINUTES 23 SECONDS") 
> 
> times(transform.span.to.time(x)) 
[1] 12:03:01 22:00:00 00:18:23 
> 
> times(transform.span.to.time2(x)) 
[1] 12:03:01 22:00:00 00:18:23 
ここ

library(gsubfn) 
library(chron) 

unit2days <- function(d, u) 
    as.numeric(d) * switch(tolower(u), s = 1, m = 60, h = 3600)/(24 * 3600) 
transform.span.to.time <- function(x) 
    sapply(strapply(x, "(\\d+) *(\\w)", unit2days), sum) 

は、第二の溶液であります

関連する問題