[R] Applying function to data.frame

Rees, David david.rees at citi.com
Mon Oct 8 16:13:33 CEST 2007


Hi,

If I have the following data.frame 

>y
          time val
1 08:00:05.834   1
2 08:03:13.345   2
3 08:10:12.443   3
> 

and the following function which converts the time string to the number
of milliseconds since midnight

> str_to_millis
function( s )
{
a <- as.numeric( unlist( strsplit(s,":",fixed="TRUE") ) )
m <- a[1]*3600000 + a[2]*60000 + a[3]*1000
}

Then to get the time into millis since midnight I am doing this 

> transform( y, time=str_to_millis( as.character(time) ) )
      time val
1 28805834   1
2 28805834   2
3 28805834   3

but this is obviously wrong, as I get all the time values as the first
element converted rather than each element converted.

Any ideas please?

Many thanks,
David



More information about the R-help mailing list