replace_time: Replace Time Stamps With Words

Description Usage Arguments Value Examples

View source: R/replace_time.R

Description

Replaces time stamps with word equivalents.

Usage

1
2
3
replace_time(x,
  pattern = "(2[0-3]|[01]?[0-9]):([0-5][0-9])[.:]?([0-5]?[0-9])?",
  replacement = NULL, ...)

Arguments

x

The text variable.

pattern

Character time regex string to be matched in the given character vector.

replacement

A function to operate on the extracted matches or a character string which is a replacement for the matched pattern.

...

ignored.

Value

Returns a vector with the pattern replaced.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
x <- c(
    NA, '12:47 to "twelve forty-seven" and also 8:35:02', 
    'what about 14:24.5', 'And then 99:99:99?'
)

## Textual: Word version
replace_time(x)

## Normalization: <<TIME>>
replace_time(x, replacement = '<<TIME>>')

## Normalization: hh:mm:ss or hh:mm
replace_time(x, replacement = function(y){
        z <- unlist(strsplit(y, '[:.]'))
        z[1] <- 'hh'
        z[2] <- 'mm'
        if(!is.na(z[3])) z[3] <- 'ss'
        glue_collapse(z, ':')
    }
)

## Textual: Word version (forced seconds)
replace_time(x, replacement = function(y){
        z <- replace_number(unlist(strsplit(y, '[:.]')))
        z[3] <- paste0('and ', ifelse(is.na(z[3]), '0', z[3]), ' seconds')
        paste(z, collapse = ' ')
    }
)
 
## Normalization: hh:mm:ss
replace_time(x, replacement = function(y){
        z <- unlist(strsplit(y, '[:.]'))
        z[1] <- 'hh'
        z[2] <- 'mm'
        z[3] <- 'ss'
        glue_collapse(z, ':')
    }
)

Example output

[1] NA                                                                                       
[2] "twelve forty-seven to \"twelve forty-seven\" and also eight thirty-five and two seconds"
[3] "what about fourteen twenty-four and five seconds"                                       
[4] "And then 99:99:99?"                                                                     
[1] NA                                                    
[2] "<<TIME>> to \"twelve forty-seven\" and also <<TIME>>"
[3] "what about <<TIME>>"                                 
[4] "And then 99:99:99?"                                  
[1] NA                                                 
[2] "hh:mm to \"twelve forty-seven\" and also hh:mm:ss"
[3] "what about hh:mm:ss"                              
[4] "And then 99:99:99?"                               
[1] NA                                                                                                     
[2] "twelve forty-seven and 0 seconds to \"twelve forty-seven\" and also eight thirty-five and two seconds"
[3] "what about fourteen twenty-four and five seconds"                                                     
[4] "And then 99:99:99?"                                                                                   
[1] NA                                                    
[2] "hh:mm:ss to \"twelve forty-seven\" and also hh:mm:ss"
[3] "what about hh:mm:ss"                                 
[4] "And then 99:99:99?"                                  

textclean documentation built on May 2, 2019, 7:22 a.m.