0

I don't know how to convert the rows of a matrix in R from character to POSIXt or another similar class. These are my character rows:

  [1] "2015.07.06 13.41.00.033" "2015.07.06 13.40.00.033" "2015.07.06 13.39.00.033"
  [4] "2015.07.06 13.38.00.033" "2015.07.06 13.37.00.007" "2015.07.06 13.36.00.007"
  [7] "2015.07.06 13.35.00.007" "2015.07.06 13.34.00.007" "2015.07.06 13.33.00.007"
 [10] "2015.07.06 13.32.00.007" "2015.07.06 13.31.00.007" "2015.07.06 13.30.00.007"
 [13] "2015.07.06 13.29.00.007" "2015.07.06 13.28.00.007" "2015.07.06 13.27.00.007"
 [16] "2015.07.06 13.26.00.007" "2015.07.06 13.25.00.007" "2015.07.06 13.24.00.007"
 [19] "2015.07.06 13.23.00.007" 
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Michele Della Mea
  • 966
  • 2
  • 16
  • 35

2 Answers2

1

A matrix can only contain one type of data, unlike a list or data.frame. Additionally, a matrix (usually) only contains atomic types*, which means your POSIXct object would be converted to numeric.

> matrix(Sys.time())
           [,1]
[1,] 1436191385

So the answer to your question is, "you can't do that with a matrix." Use a data.frame instead, or provide more detail in your question about what problem you're actually trying to solve by doing this.

*A matrix can contain list elements, which are not atomic objects, but that is a very unusual case and likely not what the user intended.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
0

You could try

options(digits.secs=6)
times <- strptime(times,"%Y.%m.%d %H.%M.%OS", tz= " ")
#> times
# [1] "2015-07-06 13:41:00.033" "2015-07-06 13:40:00.033" "2015-07-06 13:39:00.033" "2015-07-06 13:38:00.033" "2015-07-06 13:37:00.007"
# [6] "2015-07-06 13:36:00.007" "2015-07-06 13:35:00.007" "2015-07-06 13:34:00.007" "2015-07-06 13:33:00.007" "2015-07-06 13:32:00.007"
#[11] "2015-07-06 13:31:00.007" "2015-07-06 13:30:00.007" "2015-07-06 13:29:00.007" "2015-07-06 13:28:00.007" "2015-07-06 13:27:00.007"
#[16] "2015-07-06 13:26:00.007" "2015-07-06 13:25:00.007" "2015-07-06 13:24:00.007" "2015-07-06 13:23:00.007"
#> class(times)
#[1] "POSIXlt" "POSIXt"

data

times <-c("2015.07.06 13.41.00.033", "2015.07.06 13.40.00.033", "2015.07.06 13.39.00.033",
"2015.07.06 13.38.00.033", "2015.07.06 13.37.00.007", "2015.07.06 13.36.00.007",
"2015.07.06 13.35.00.007", "2015.07.06 13.34.00.007", "2015.07.06 13.33.00.007",
"2015.07.06 13.32.00.007", "2015.07.06 13.31.00.007" ,"2015.07.06 13.30.00.007",
"2015.07.06 13.29.00.007", "2015.07.06 13.28.00.007", "2015.07.06 13.27.00.007",
"2015.07.06 13.26.00.007", "2015.07.06 13.25.00.007", "2015.07.06 13.24.00.007",
"2015.07.06 13.23.00.007")

If you want to use these POSIXt entries for a time series of your data, a convenient way could consist in converting the dataframe into an xts object.

This could be achieved, e.g., with

library(xts)
my_xts <- xts(times, data) 

where data are the observations, stored as numerical values, that correspond to the above sequence of points in time.

Hope this helps.

RHertel
  • 23,412
  • 5
  • 38
  • 64