将代表天的时间十进制/分数转换为以R为单位的实际时间? [英] Converting a time decimal/fraction representing days to its actual time in R?

查看:198
本文介绍了将代表天的时间十进制/分数转换为以R为单位的实际时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据集中的R中有一列,其值已转换为我认为是基于的时间小数部分。我一直在使用此站点作为参考: http:// www.calculatorsoup.com/calculators/time/decimal-to-time-calculator.php

I have a column in R on my dataset with values that have been converted into what I believe is a time decimal fraction based on days. I have been using this site as a reference: http://www.calculatorsoup.com/calculators/time/decimal-to-time-calculator.php

转换0.3408565 对应通过执行以下操作来达到8:10:50的时间:

Which converting 0.3408565 days corresponds to the time of 8:10:50 by doing the following:

0.3408565 * 24 hours/day = 8.180555544 = 8 hours

0.180555544 * 60 minutes = 10.83333264 = 10 minutes

0.83333264 * 60 seconds = 49.9999584 = 50 seconds

如您所见,它正在提取余数并进行进一步的乘法运算,但是我不确定如何在R中实现该过程。我想转换我的所有值使用此列,因此我可以分别用它们的实际对应时间8:10:50、09:16:29、10:33:38替换0.3408565、0.3864468、0.4400231等。

As you can see it is extracting the remainder and doing further multiplication, however I'm not sure how I could implement the process within R. I want to convert all the values in my column using this so I can replace 0.3408565, 0.3864468, 0.4400231 etc with their actual time counterparts 8:10:50, 09:16:29, 10:33:38 respectively. This seems to be how it is done in Excel too.

我尝试编写自己的函数,该函数用英语完成:

I tried writing my own function which in English does:

convertTime <- function(x)
{
x * 60 = Hour
x2 * 24 = Min
x3 * 24 = Sec

hourChar <- as.character(Hour)
minChar <- as.character(Min)
secChar <- as.character(Sec)
paste(hourChar, minChar, secChar, sep=":")
}

x为0.3408565,但当然不会提取余数。我可能会过度复杂化,但是我想使用每个值作为要转换的输入来遍历列中的所有值。

With x being 0.3408565, but of course this doesn't extract the remainder. I might be seriously overcomplicating it, but I want to loop through all values in the column using each value as input to convert.

最后,我查看了诸如 lubridate之类的软件包,但找不到与我的问题有关的任何东西。

Lastly, I have looked at packages such as 'lubridate' but cannot find anything that is relevant to my problem.

我还浏览了我的具体情况,但找不到与我的问题真正相关的任何内容。是否有任何可以帮助我的软件包或解决方案?

I also browsed here for my specific situation but couldn't find anything really relevant to my problem. Is there any package or solution that could help me?

编辑:我想说,这两个答案对于遇到此类问题的其他人都非常有效,谢谢你们! >

I want to say both answers work perfectly for anyone else having this kind of problem, thank you to you both!

推荐答案

这是一个自定义函数

convertTime <- function(x){
    Hour = (x * 24) %% 24 #For x>1, only the value after decimal will be considered
    Minutes = (Hour %% 1) * 60
    Seconds = (Minutes %% 1) * 60

    hrs = ifelse (Hour < 10, paste("0",floor(Hour),sep = ""), as.character(floor(Hour)))
    mins = ifelse (Minutes < 10, paste("0",floor(Minutes),sep = ""), as.character(floor(Minutes)))    
    secs = ifelse (Seconds < 10, paste("0",round(Seconds,0),sep = ""), as.character(round(Seconds,0)))

    return(paste(hrs, mins, secs, sep = ":"))
}

用法

a = seq(0,1,0.2)
convertTime(a)
#[1] "00:00:00" "04:48:00" "09:36:00" "14:24:00" "19:12:00" "00:00:00"

b = 0.3408565
convertTime(b)
# [1] "08:10:50"

这篇关于将代表天的时间十进制/分数转换为以R为单位的实际时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆