[R] Applying function to data.frame
jim holtman
jholtman at gmail.com
Mon Oct 8 16:24:44 CEST 2007
try this:
> x <- read.table(textConnection(" time val
+ 1 08:00:05.834 1
+ 2 08:03:13.345 2
+ 3 08:10:12.443 3"), header=TRUE)
> z <- strsplit(as.character(x$time), ":")
> newtime <- lapply(z, function(a) as.numeric(a) %*% c(3600000, 60000, 1000))
> x$time <- unlist(newtime)
> x
time val
1 28805834 1
2 28993345 2
3 29412443 3
>
On 10/8/07, Rees, David <david.rees at citi.com> wrote:
> 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
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list