[R] Fwd: converting character matrix to POSIXct matrix
David Winsemius
dwinsemius at comcast.net
Sat Apr 27 02:28:42 CEST 2013
On Apr 26, 2013, at 3:12 PM, hh wt wrote:
> I thought i copy the list for the few suggestions i received privately.
>
> Thanks to David Winsemius for pointing out that elements of a matrix must
> be atomic. POSIX objects are list, so won't work.
I did state the first point. I didn't actually say that POSIX objects are lists. In fact there are not any "POSIX objects" in R, at least with that exact class name. There is a superclacss of "POSIXt" which some function recognize and perform useful operations on eihter POSIXct and POSIXlt classes . The POSIXlt-class is stored as a list. POSIXct vectors, on the other hand, are of mode "numeric" and have a variety of class-specific functions that support math and comparison operations. I did note that matrices cannot hold vectors with classes that make them non-atomic. Perhaps paradoxically, matrices can hold list items.
--
David.
>
> But data frame is an option, as suggested by arun below.
>
> res<-data.frame(lapply(seq_len(ncol(time.m)),function(i)
> strptime(time.m[,i],"%H:%M:%OS")))
>
> Thanks to all.
>
> Horace
>
>
> ---------- Forwarded message ----------
> From: arun <smartpink111 at yahoo.com>
> Date: Fri, Apr 26, 2013 at 11:22 AM
> Subject: Re: [R] converting character matrix to POSIXct matrix
> To: hh wt <horacetso at gmail.com>
> Cc: R help <r-help at r-project.org>
>
>
>
>
> time.m<- as.matrix(read.table(text='
> "08:00:20.799" "08:00:20.799" "08:00:20.799" "08:00:20.799" "08:00:20.799"
> "08:00:21.996" "08:00:22.071" "08:00:23.821" "08:00:24.370" "08:00:25.573"
> "08:00:29.200" "08:00:29.200" "08:00:29.591" "08:00:30.368" "08:00:30.536"
> "08:00:31.073" "08:00:31.372" "08:00:31.384" "08:00:31.403" "08:00:31.867"
> "08:00:31.867" "08:00:31.867" "08:00:31.971" "08:00:34.571" "08:00:34.571"
> ',sep="",header=FALSE,stringsAsFactors=FALSE))
> colnames(time.m)<- NULL
> op<- options(digits.secs=3)
> res<-data.frame(lapply(seq_len(ncol(time.m)),function(i)
> strptime(time.m[,i],"%H:%M:%OS")))
> colnames(res)<- paste0("X",1:5)
> str(res)
> #'data.frame': 5 obs. of 5 variables:
> # $ X1: POSIXct, format: "2013-04-26 08:00:20.799" "2013-04-26
> 08:00:21.996" ...
> # $ X2: POSIXct, format: "2013-04-26 08:00:20.799" "2013-04-26
> 08:00:22.071" ...
> # $ X3: POSIXct, format: "2013-04-26 08:00:20.799" "2013-04-26
> 08:00:23.821" ...
> # $ X4: POSIXct, format: "2013-04-26 08:00:20.799" "2013-04-26
> 08:00:24.369" ...
> # $ X5: POSIXct, format: "2013-04-26 08:00:20.799" "2013-04-26
> 08:00:25.572" ...
> options(op)
>
> A.K.
>
> ----- Original Message -----
> From: hh wt <horacetso at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Friday, April 26, 2013 1:51 PM
> Subject: [R] converting character matrix to POSIXct matrix
>
> I thought this is a common question but rseek/google searches don't yield
> any relevant hit.
>
> I have a matrix of character strings, which are time stamps,
>
>> time.m[1:5,1:5]
> [,1] [,2] [,3] [,4] [,5]
>
> [1,] "08:00:20.799" "08:00:20.799" "08:00:20.799" "08:00:20.799"
> "08:00:20.799"
> [2,] "08:00:21.996" "08:00:22.071" "08:00:23.821" "08:00:24.370"
> "08:00:25.573"
> [3,] "08:00:29.200" "08:00:29.200" "08:00:29.591" "08:00:30.368"
> "08:00:30.536"
> [4,] "08:00:31.073" "08:00:31.372" "08:00:31.384" "08:00:31.403"
> "08:00:31.867"
> [5,] "08:00:31.867" "08:00:31.867" "08:00:31.971" "08:00:34.571"
> "08:00:34.571"
>
> And i would like to convert it to a POSIXct matrix. I tried this,
>
> time1 = lapply(time.m, function(tt)strptime(tt, "%H:%M:%OS"))
>
> but it yields a list.
>
> Any tip is appreciated.
>
>
> Horace
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list