I used some time calculations in R, subtracting POSIXct objects. Result is fine, and I understand it is in seconds. However when I print it, I get some intelligent printing, returning either hours or minutes when the integer is big enough. E.g. I get 2.01666666666667 and I don't know whether it's seconds (doubt it), minutes or hours. In this particular case it was minutes, but I've had very similar digits being hours. How do I force print() to give me what I want (i.e. always either minutes or hours)
Asked
Active
Viewed 35 times
1 Answers
1
If I do "some time calculations in R, subtracting POSIXct objects"
x <- Sys.time()
y <- Sys.time()
z <- y-x
the result is
print(z)
# Time difference of 0.01800108 secs
And if I want it in minutes, I can do
units(z) <- "mins"
print(z)
# Time difference of 0.000300018 mins

lukeA
- 53,097
- 5
- 97
- 100
-
Thanks for the help, and the downgrading, I am still in the steep part of the learning curve. In your example z is class "difftime"; however in my case z would be "numeric", since I use it in a numerical calculation. So units(z) does not apply. Print(z) gets me either minutes or hours as convenient, and I haven't found out how to control this. – VinceIT Jan 07 '16 at 10:50
-
You're welcome. I didn't downgrade anything. However, just from observing the activity on SO's R tag a bit it should be clear how to ask well. In short: Edit your question and provide a reproducible example of your code and why it failed instead of describing something in words alone. Here's how: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – lukeA Jan 07 '16 at 12:05