Reformatting times into data that can be manipulated

I have a data set that needs a lot of cleaning up in the formatting, and at some point I would like to calculate the time that people in the study spent fishing. This would be easy if the times that fishing started and stopped were in a normal format, but for some reason the data are not in a useful decimal format. For example, 10:45 in the morning is recorded as 10.45, while 11:10 would be 11.10.

I have already tried asking Excel to replace all periods with colons (it won't do it, and others have already tried to help me make Excel cooperate). I can think of a couple of approaches to doing this in R but am not sure how to proceed with either. First, if there was some way to divide the part after the decimal (ONLY) by 60 then the decimal format would make sense. Then, 10.45 would be 10.75 and subtraction would be no problem. Or, it would also work to just write some kind of simple code to replace the periods with colons and then (I'm thinking?) manipulate it using something like the chron package.

I do not know how you would code either of these options - does anyone have any suggestions? I think dividing by 60 could be accomplished with function that broke apart the pieces before and after the period (like the opposite of paste) but I can't find the name of such a function. The first one would also be simpler I imagine, although the second one would actually probably shed light on a larger problem I have with R, which is trying to figure out how to make commands generalizable. I understand how I could force it to change all of the 10.45 to 10:45, but wish I knew if there was a format just to say "take XX.XX and change to XX:XX" no matter what the actual digits were. But one thing at a time I suppose.


In the absence of an example, I present as.difftime for this, :

> d <- as.difftime('10.45', format='%H.%M')
> d
Time difference of 10.75 hours
> as.numeric(d)
[1] 10.75

For your comment, you must ensure that you read the time values as strings.

Time.Start = c(9.10, 9.10, 9.10, 9.10, 9.10, 9.10)
Time.Stop = c(14.25, 14.25, 14.25, 14.25, 14.25, 14.25)

Turn them into character strings. It would be much better to just read them as strings in the first place, but we can kludge them with sprintf:

Time.Stop <- sprintf('%.2f', Time.Stop)
Time.Start <- sprintf('%.2f', Time.Start)

Then parse and take the difference. - is returning an object of class difftime , similar to above.

strptime(Time.Stop, format='%H.%M') - strptime(Time.Start, format='%H.%M')

## Time differences in hours
## [1] 5.25 5.25 5.25 5.25 5.25 5.25
## attr(,"tzone")
## [1] ""
链接地址: http://www.djcxy.com/p/38314.html

上一篇: as.numeric用逗号分隔符?

下一篇: 将时间重新格式化为可以操纵的数据