将整数列转换为HH:MM [英] Convert an integer column to time HH:MM
问题描述
我正在尝试学习R,而且我仍然遇到一个数据集中的一列从整数值转换为时间的问题。上述列以5分钟的时间打破了日子。使用以下格式:5将是00:05,105将是01:05,而1105将是11:05。
如果我使用:
strptime(activity [,interval],format =%H%M)
/ pre>
所有对于低于1000的值都返回NA。
任何想法关于如何使用适用的家庭做同样的过程将不胜感激
我知道这是一个很基本的问题,但我无法自己弄清楚。 p>
非常感谢
编辑:根据请求的活动[n,间隔]列(此列已17568行,包括数字从5到2355几天),15个第一个元素如下所示:
活动[1: 15,间隔]
[1] 0 5 10 15 20 25 30 35 40 45 50 55 100 105 110
应该看起来像这样
活动[1:15间隔]
[1] 0000 0005 0010 0015 0020 0025 0030 0035 0040 0045
[11] 0050 0055 0100 0105 0110
解决方案这是一个建议
temp< - c(0,5,10, 20,25,30,35,40,45,50,55,100,105,110)#您的数据
pre>
temp2< - mapply(function(x,y)paste0(rep(x,y) collapse =),0,4 - nchar(temp))
temp < - paste0(temp2,temp)
temp
#[1]00000005 002000200040004500500055010001050110
然后你可以做
格式(strptime(temp, format =%H%M),format =%H:%M)
#[1]00:0000:0500:1000:1500 :2000:2500:3000:3500:4000:4500:5000:5501:0001:0501 :10
I'm trying to learn R, and I'm stuck with a problem regarding the conversion of one column in a data set from integer values to time.
The aforementioned column breaks the days in 5 minute portions. Using the following format: 5 would be 00:05, 105 would be 01:05 and 1105 would be 11:05.
if I use:
strptime(activity[,"interval"],format="%H%M")
The resulting object returns "NA" for all the values that are below 1000.
Any ideas on how to make that same process using apply family would be greatly appreciated
I know this is a pretty basic question but I am not able to figure it out myself.
Thank you very much
Edit: As requested the activity[n,"interval"] column (This column has 17568 rows, comprising numbers from 5 to 2355 for several days) and the 15 first elements look like this:
activity[1:15,"interval"] [1] 0 5 10 15 20 25 30 35 40 45 50 55 100 105 110
And should look like this
activity[1:15,"interval"] [1] 0000 0005 0010 0015 0020 0025 0030 0035 0040 0045 [11] 0050 0055 0100 0105 0110
解决方案Here's one suggestion
temp <- c(0 , 5 , 10, 15 ,20 , 25 ,30 ,35, 40, 45 , 50 ,55 ,100 ,105, 110) # Your data temp2 <- mapply(function(x, y) paste0(rep(x, y), collapse = ""), 0, 4 - nchar(temp)) temp <- paste0(temp2, temp) temp # [1] "0000" "0005" "0010" "0015" "0020" "0025" "0030" "0035" "0040" "0045" "0050" "0055" "0100" "0105" "0110"
Then you can do
format(strptime(temp, format="%H%M"), format = "%H:%M") #[1] "00:00" "00:05" "00:10" "00:15" "00:20" "00:25" "00:30" "00:35" "00:40" "00:45" "00:50" "00:55" "01:00" "01:05" "01:10"
这篇关于将整数列转换为HH:MM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!