R convert character "111213" into proper time which is "11:12:13"

r convert character to numeric data frame
r convert column to string
as.character in r
convert multiple columns to numeric r
convert integer to numeric in r
r convert double to integer
convert vector to dataframe r
convert character to numeric r dplyr

R convert character "111213" into time "11:12:13".

strptime("111213", format="%H%m%s") gives NA

and

strptime("111213", "%H%m%s") gives 1970-01-01 01:00:13 CET

I think the canonical answer would be as in my comment:

format(strptime("111213", format="%H%M%S"), "%H:%M:%S")
#[1] "11:12:13"

where you can read ?strptime for all the details. format is a generic function, and in this specific case we are using format.POSIXlt.

Another solution is to merely play with string:

paste(substring("111213", c(1,3,5), c(2,4,6)), collapse = ":")
#[1] "11:12:13"

This makes sense because your input is really not a Date-Time: there is no Date.

Convert an R Object to a Character String, This is a helper function for format to produce a single character string describing an R object. Usage. toString(x, ) ## Default S3 method: toString(x, width = NULL​  The R help at ?factor describes a solution to this problem. The solution is to index the levels by the factor itself, and then to convert to numeric: > as.numeric (as.character (numbers)) [1] 9 8 10 8 9. This is an example of nested functions in R, in which you pass the results of one function to a second function. Nested functions are a bit

We can use

library(chron)
times(gsub("(.{2})(?=\\d)", "\\1:", "111213", perl = TRUE))
#[1] 11:12:13

Character Vectors, If you want to convert numbers to character with the maximum possible precision, use format . Value. character creates a character vector of the specified length. Convert an R Object to a Character String Description. This is a helper function for format to produce a single character string describing an R object. Usage

To manipulate time, you can use hms package.

By default, it working with %H:%M;%S (or %X format).

For you specifique time format ("111213"), you need to go through base function as.difftime

hms::as.hms(as.difftime("111213", format = "%H%M%S"))
#> 11:12:13

Converting between vector types, You want to convert between numeric vectors, character vectors, and factors. Solution. Suppose you start with this numeric vector n : n <-  This helper function is used by read.table. When the data object x is a data frame or list, the function is called recursively for each column or list element. Given a vector, the function attempts to convert it to logical, integer, numeric or complex, and failing that converts a character vector to factor unless as.is = TRUE. The first

So if we incorporate also date in similar "integer" format we can obtain command :

strptime("20181017 112233", format="%Y%m%d %H%M%S")

toString Converts its Argument to a Character String, toString Converts its Argument to a Character String. Description. This is a helper function for format . It converts its argument to a string. If the argument is a  Type conversions in R work as you would expect. For example, adding a character string to a numeric vector converts all the elements in the vector to character. Use is. foo to test for data type foo. Returns TRUE or FALSE

Convert Character to Numeric in R (Example for String Variable , This is a helper function for format to produce a single character string describing an R object. Usage. toString(x, ) ## Default S3 method: toString(x, width = NULL​  The format and as.character methods and strftime convert objects from the classes "POSIXlt" and "POSIXct" to character vectors. strptime converts character vectors to class "POSIXlt": its input x is first converted by as.character. Each input string is processed as far as necessary for the format specified: any trailing characters are ignored.

Convert Character to Factor in R, Convert a data object to logical, integer, numeric, complex, character or factor as appropriate. But, if all the columns needs to changed to numeric, use lapply to loop over the columns and convert to numeric by first converting it to character class as the columns were factor. yyz[] <- lapply(yyz, function(x) as.numeric(as.character(x)))

type.convert function, Sometimes you need to explicitly convert factors to either text or numbers. To do this, you use the functions as.character() or as.numeric(). First, convert your  The package supports converting other R classes such as integer and factor to dates in addition to converting character strings. The user does not need to specify any orders or formats, as anytime() and anydate() will guess the format (from a default list of supported formats).

Comments
  • You need strptime("111213", format="%H%M%S"). And you can get a list of formatting parameters by checking ?strptime. %m parse the month; %M parses the minute. %S parses the second.
  • could you please update your solution with 91213 case ? current returns NA
  • @Qbik This does it: format(strptime(sprintf("%06d", 91213), format="%H%M%S"), "%H:%M:%S"). If your input is readily a string, make it an integer first: format(strptime(sprintf("%06d", as.integer("91213")), format="%H%M%S"), "%H:%M:%S")
  • What is class of output object ?
  • hms package works with hms objects wich are also of class difftime. The package creates " A simple class for storing time-of-day values" - it is its purpose