Reputation: 6806
In below code, the last and the second last R codes produce a bit different result, why? After running the last line why don't I get output "time difference is Time difference of 5 mins"? how could I get such output?
filenames <- c("CH7Data_20130401T130110.csv", "CH7Data_20130401T130610.csv")
timestamps <- gsub(".*_([^.]+).*", "\\1", filenames)
## [1] "20130401T130110" "20130401T130610"
timestamps <- as.POSIXlt(timestamps, format = "%Y%m%dT%H%M%S")
## [1] "2013-04-01 13:01:10 PDT" "2013-04-01 13:06:10 PDT"
timestamps[2]-timestamps[1]
##Time difference of 5 mins
cat("time difference is",timestamps[2]-timestamps[1])
##time difference is 5
Upvotes: 2
Views: 988
Reputation: 1
Try
diff = timestamps[2]-timestamps[1]
cat("time difference is",diff,attr(diff,"units"),"\n")
Upvotes: 0
Reputation: 263481
'difftime'-objects are atomic objects with a special class so they have a print method that changes the output seen at the console. If you want as stated in the comment to print a redundant message that includes what comes to the console and some sort of preamble, then this would work:
> cat("time difference is",capture.output(timestamps[2]-timestamps[1]) )
time difference is Time difference of 5 mins
You could inset a line feed at the end of your preamble text:
> cat("time difference is\n",capture.output(timestamps[2]-timestamps[1]) )
time difference is
Time difference of 5 mins
Upvotes: 5