[R] seconds to h:m:s format
Jim Lemon
drjimlemon at gmail.com
Thu Nov 12 07:07:38 CET 2015
Any date would do as you are only getting the H:M:S at the end. I just
chose the zero date.
Jim
On Thu, Nov 12, 2015 at 1:25 PM, Omar André Gonzáles Díaz <
oma.gonzales at gmail.com> wrote:
> Thank you, Jim.
>
> Just to understand it:
>
> You replicated 10 times: "1970-01-01". Why this specific date?
>
>
>
>
>
> 2015-11-11 20:22 GMT-05:00 Jim Lemon <drjimlemon at gmail.com>:
>
>> Hi Omar,
>> There is some sort of error in your structure definition, but the
>> following works for me:
>>
>> session.duration.fuente <-
>> data.frame(mes=c(rep("oct",5),rep("nov",5)),
>> fuente=c("adwords", "directo", "organico", "redes sociales",
>> "referral", "adwords", "directo", "organico", "redes sociales",
>> "referral"),
>> avg.session.duration = c(970178, 1642455, 780485,
>> 3170400, 179184, 352995, 833827, 260610, 2318928, 49836))
>>
>> base_dates<-strptime(rep("1970-01-01",10),"%Y-%m-%d")
>> format(base_dates+session.duration.fuente$avg.session.duration,"%H:%M:%S")
>> [1] "05:29:38" "00:14:15" "00:48:05" "16:40:00" "01:46:24" "02:03:15"
>> [7] "15:37:07" "00:23:30" "20:08:48" "13:50:36"
>>
>> Jim
>>
>> On Thu, Nov 12, 2015 at 10:15 AM, Omar André Gonzáles Díaz <
>> oma.gonzales at gmail.com> wrote:
>>
>>> Hi,
>>>
>>> I've a data frame with 3 columns: "mes", "fuente",
>>> "avg.sessions.duration".
>>>
>>> "avg.sessions.duration" is a column containing seconds.
>>>
>>> I need you help with:
>>>
>>> 1.- Help to put these values in "h:m:s" format.
>>> .
>>>
>>> =======================================================
>>>
>>> I've found this german page:
>>>
>>> http://forum.r-statistik.de/viewtopic.php?f=25&t=5284
>>>
>>> So I've tried:
>>>
>>> for (i in 1:nrow(session.duration.fuente)) {
>>>
>>> session.duration.fuente$avg.session.duration <-
>>> format(as.POSIXct('0001-01-01 00:00:00') +
>>> session.duration.fuente$avg.session.duration[i], "%H:%M:%S")
>>>
>>> }
>>>
>>> but got this error:
>>>
>>> Error in unclass(e1) + unclass(e2) :
>>> non-numeric argument to binary operator
>>>
>>> =======================================================
>>>
>>> After that I've tried: strptime:
>>>
>>> session.duration.fuente$avg.session.duration <-
>>> strptime(session.duration.fuente$avg.session.duration, "%H:%M:%OS")
>>>
>>> But got NAs.
>>>
>>> ========================================================
>>>
>>> Here is the data:
>>>
>>>
>>> session.duration.fuente <- structure(list(mes = structure(c(1L, 1L, 1L,
>>> 1L,
>>> 1L, 2L, 2L,
>>> 2L, 2L, 2L), .Label = c("oct", "nov"), class = c("ordered", "factor"
>>> )), fuente = c("adwords", "directo", "organico", "redes sociales",
>>> "referral", "adwords", "directo", "organico", "redes sociales",
>>> "referral"), avg.session.duration = c(970178, 1642455, 780485,
>>> 3170400, 179184, 352995, 833827, 260610, 2318928, 49836)), row.names =
>>> c(NA,
>>> -10L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars =
>>> list(
>>> mes), drop = TRUE, .Names = c("mes", "fuente", "avg.session.duration"
>>> ))
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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]]
More information about the R-help
mailing list