[R] extracting numerical data from text field

Gabor Grothendieck ggrothendieck at myway.com
Thu Mar 24 05:13:04 CET 2005


Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

> 
> Luis Tercero <luis.tercero <at> ebi-wasser.uni-karlsruhe.de> writes:
> 
> : 
> : I have imported a data frame that looks like this:
> : 
> :            Measurement.Date.and.Time Z.Average..nm.   PDI
> : 572 Dienstag, 22. März 2005 11:05:59          366,4 0,468
> : 573 Dienstag, 22. März 2005 11:09:30          353,4 0,532
> : 574 Dienstag, 22. März 2005 11:12:59            343 0,428
> : 575 Dienstag, 22. März 2005 11:16:28          354,1 0,433
> : 576 Dienstag, 22. März 2005 11:19:59          341,9 0,349
> : 577 Dienstag, 22. März 2005 11:23:29          334,9 0,429
> : ...
> : 
> : Would there be a way to extract the time in numerical form from the
> : Measurement.Date.and.Time field?  What I would like to do is a time
> : series where, for example,
> : Dienstag, 22. März 2005 11:05:59 is time=0 min
> : Dienstag, 22. März 2005 11:09:30 is time=3.5 min, etc.
> : 
> : Thank you in advance for your help.
> : 
> : Luis
> 
> Make sure that you are in a German locale:
> 
>   # this works on Windows XP.  On other OS, "ge" code may differ.
>   Sys.setlocale("LC_TIME", "ge") 
> 
> Then if DF is your data frame use strptime (see ?strptime for more
> on the % codes):
> 
>   dat <- strptime(DF[,1], "%A, %d. %B %Y %H:%M:%S")
>   dat - dat[1]   # difference in time since the first date time

One other comment.

I assumed your data time field is stored as character in the data
frame.  If its stored as a factor then you need to convert it to
character first using as.character.  If its already stored as a 
POSIXct date time then all you have to do is subtract off the
first one.  (Note that if you put the output of dput(DF) in your
post then people will be able to exactly recreate your data frame
and then know what you have.)

Also, RNews 4/1 has a table with lots of date time processing
idioms.




More information about the R-help mailing list