Most efficient way to have R recognize " 41520092010" as "04/15/2009 20:10"

Scott

I need to find the duration of a large number of events by using the start and end time variables in a dataset, but both the variables encode the time in the annoying format "mmddyyyyhhmm," with the cherry on top being that the first nine months are encoded as single digits (January is " 1" rather than "01"). At least the time uses a twenty-four clock (assuming the people filling out each event did it right).

I know there has to be a fairly simple way to do this, but I can't think of one and suspect one of you fine folks have it memorized and can write it out in a couple of seconds.

Rich Scriven

If you have a vector x with character values for conversion ...

x <- c("41520092010", "11520092010", "121520092010")

... you can check this vector for 11 characters (or whatever). If an element has 11 characters, we paste a zero on the front, then convert the whole vector to POSIXt.

as.POSIXct(
    ifelse(nchar(x) == 11, paste0("0", x), x), 
    format = "%m%d%Y%H%M",
    tz = "UTC"
)
# [1] "2009-04-15 20:10:00 UTC" "2009-01-15 20:10:00 UTC" 
# [3] "2009-12-15 20:10:00 UTC"

If you don't like ifelse(), you can use replace().

replace(x, nchar(x) == 11, paste0("0", x[nchar(x) == 11]))

or formatC()

formatC(as.numeric(x), digits = 12, width = 12, flag = "0")

The most efficient of these is likely formatC().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Most efficient way to randomize a matrix in R or in Python

From Dev

Most efficient way to have multiple toggle items on a page

From Dev

Most efficient way to implement this?

From Dev

Most efficient way to execute this

From Dev

R: Whats the most efficient way to code this formula in R?

From Dev

Most efficient way to label groups based on sequential values in R

From Dev

Most efficient way to get index in a sorted vector in R?

From Dev

Most efficient way to calculate pairwise partial correlations in base R?

From Dev

Most efficient way to compute a polynomial

From Dev

query with calculations most efficient way

From Dev

Most efficient way to split sentence

From Dev

Most efficient way to rename an image

From Dev

Most efficient way to output a newline

From Dev

Most efficient way to loop through '...'

From Dev

Most efficient way to determine an intersection

From Dev

Most efficient way to count occurrences?

From Dev

Most efficient way to concatenate Strings

From Dev

Most efficient way to compute a polynomial

From Dev

Most efficient way to store this data

From Dev

Is this the most efficient way to write this method?

From Dev

Most efficient way of MySQL rows?

From Dev

Most efficient way to encrypt files?

From Dev

query with calculations most efficient way

From Dev

Most efficient way to search in a table

From Dev

Most efficient way to write a buffer

From Dev

Most efficient way to look for these inconsistencies?

From Dev

What is the most efficient way with Python to merge rows in a CSV which have a single duplicate field?

From Dev

What is the most efficient way with Python to merge rows in a CSV which have a single duplicate field?

From Dev

What is the most efficient way to have background images of different sizes in a responsive design website?

Related Related

  1. 1

    Most efficient way to randomize a matrix in R or in Python

  2. 2

    Most efficient way to have multiple toggle items on a page

  3. 3

    Most efficient way to implement this?

  4. 4

    Most efficient way to execute this

  5. 5

    R: Whats the most efficient way to code this formula in R?

  6. 6

    Most efficient way to label groups based on sequential values in R

  7. 7

    Most efficient way to get index in a sorted vector in R?

  8. 8

    Most efficient way to calculate pairwise partial correlations in base R?

  9. 9

    Most efficient way to compute a polynomial

  10. 10

    query with calculations most efficient way

  11. 11

    Most efficient way to split sentence

  12. 12

    Most efficient way to rename an image

  13. 13

    Most efficient way to output a newline

  14. 14

    Most efficient way to loop through '...'

  15. 15

    Most efficient way to determine an intersection

  16. 16

    Most efficient way to count occurrences?

  17. 17

    Most efficient way to concatenate Strings

  18. 18

    Most efficient way to compute a polynomial

  19. 19

    Most efficient way to store this data

  20. 20

    Is this the most efficient way to write this method?

  21. 21

    Most efficient way of MySQL rows?

  22. 22

    Most efficient way to encrypt files?

  23. 23

    query with calculations most efficient way

  24. 24

    Most efficient way to search in a table

  25. 25

    Most efficient way to write a buffer

  26. 26

    Most efficient way to look for these inconsistencies?

  27. 27

    What is the most efficient way with Python to merge rows in a CSV which have a single duplicate field?

  28. 28

    What is the most efficient way with Python to merge rows in a CSV which have a single duplicate field?

  29. 29

    What is the most efficient way to have background images of different sizes in a responsive design website?

HotTag

Archive