help needed for strptime "000000" !

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

help needed for strptime "000000" !

JESSICA
This post has NOT been accepted by the mailing list yet.
Dear R gurus,

I have a text file stored in notepad:

DATE                         TIME     Q
2004-11-01 233311 1
2004-11-01 234227 1
2004-11-01 234327 1
2004-11-01 234734 2
2004-11-02 000157 3
2004-11-02 000250 10
2004-11-02 000523 16
2004-11-02 000546 1
2004-11-02 000556 2
2004-11-02 000606 1
2004-11-02 000649 3
2004-11-02 000921 1

I  run

x<-read.table("C:/R/Sample.txt",header=T)
then get a table

 DATE                             TIME  Q
1  2004-11-01           233311  1
2  2004-11-01           234227  1
3  2004-11-01           234327  1
4  2004-11-01           234734  2
5  2004-11-02                 157  3
6  2004-11-02                250  10
7  2004-11-02                523  16
8  2004-11-02                546   1
9  2004-11-02                556   2
10 2004-11-02               606   1
11 2004-11-02               649   3
12 2004-11-02               921   1

As you can see  all the "0" are missing ! I dont know why .

I am trying to

strptime(x$TIME,"%H%M%S")

and get :

 [1] "2011-02-16 23:33:11" "2011-02-16 23:42:27" "2011-02-16 23:43:27"
 [4] "2011-02-16 23:47:34" NA                    NA                  
 [7] NA                    NA                    NA                  
[10] NA                    NA                    NA      


How can I convert the whole x$TIME column to the time format  ?


Thnaks in advance !
Reply | Threaded
Open this post in threaded view
|

Re: help needed for strptime "000000" !

Dieter Menne
JESSICA wrote
I have a text file stored in notepad:

DATE                         TIME     Q
2004-11-01 233311 1
2004-11-01 234227 1
...
I  run

x<-read.table("C:/R/Sample.txt",header=T)
then get a table

...
I am trying to

strptime(x$TIME,"%H%M%S")

and get :

 [1] "2011-02-16 23:33:11" "2011-02-16 23:42:27" "2011-02-16 23:43:27"
 [4] "2011-02-16 23:47:34" NA                    NA                  
 [7] NA                    NA                    NA                  
[10] NA                    NA                    NA      
Try (but I a note sure what to do with the DATE column.
Dieter

x<-read.table("datesample.txt",
  colClasses=c("Date","character","integer"), header=T)
class(x$TIME)= "POSIXct"
strptime(x$TIME,"%H%M%S")
Reply | Threaded
Open this post in threaded view
|

Re: help needed for strptime "000000" !

JESSICA
Hi thanks ! that works perfectly !  now I am wodering how to create a new table ,

I run the codes:

x<-read.table("C:/R/DATA.txt",colClasses=c("Date","character","integer"), header=T)
DateTime<-as.POSIXct(paste(x[,1],x[,2]),format="%Y-%m-%d%H%M%S")

and get :

"2004-11-01 23:33:11 GMT"
"2004-11-01 23:42:27 GMT"
“2004-11-01 23:43:27 GMT”
“2004-11-01 23:47:34 GMT”
“2004-11-02 00:01:57 GMT”
“2004-11-02 00:02:50 GMT”
“2004-11-02 00:05:23 GMT “
“2004-11-02 00:05:46 GMT “
“2004-11-02 00:05:06 GMT”
“2004-11-02 00:06:49 GMT”
“2004-11-02 00:09:21 GMT”

I want to create a new table which has two columns

       DateTime                 Q
2004-11-01 23:33:11        1
2004-11-01 23:42:27        1
2004-11-01 23:43:27        1  
2004-11-01 23:47:34        2
2004-11-02 00:01:57        3
2004-11-02 00:02:50        10
2004-11-02 00:05:23        16
2004-11-02 00:05:46        1
2004-11-02 00:05:06        2
2004-11-02 00:06:49        1
2004-11-02 00:09:21        3

I tried  cbind(DateTime,x$Q) , but it failed , I know it is a little stupid question but I still can not figure out how to get a new table like above.

Any help would be greatly appreciated !
Reply | Threaded
Open this post in threaded view
|

Re: help needed for strptime "000000" !

JESSICA
This post has NOT been accepted by the mailing list yet.
I jsut got a way to get the table I want and want to share,  which are  :

x<-read.table("C:/R/DATA.txt",colClasses=c("Date","character","integer"), header=T)
DateTime<-as.POSIXct(paste(x[,1],x[,2]),format="%Y-%m-%d%H%M%S")

paste<-paste(DateTime,x$Q,sep=',')
D<-read.table(textConnection(paste),sep=',',header=F)