Hello, I'm trying to calculate an integration and x-axis is a time (format : %Y-%m-%d %H:%M:%S"). I use diff(date, units="mins") in a loop for but sometimes the results stay in seconds (95% is ok). Examples for 2 sets of data are given below (first result stays in seconds whereas the second in minutes as expected). Have you already seen this behaviour ? Any idea to solve this problem ? Thanks in advance. Have a good week-end, Ptit Bleu. ------------------------------------------------------------------------> strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S")[1] "2009-03-26 11:21:31" "2009-03-26 11:22:17" "2009-03-26 11:27:18" "2009-03-26 11:36:59" "2009-03-26 11:41:59" "2009-03-26 11:46:59" [7] "2009-03-26 11:51:59" "2009-03-26 11:57:00" "2009-03-26 12:02:00" "2009-03-26 12:07:00" "2009-03-26 12:12:00" "2009-03-26 12:17:00" [13] "2009-03-26 12:22:00" "2009-03-26 12:27:01" "2009-03-26 12:32:01" "2009-03-26 12:37:01" "2009-03-26 12:42:01" "2009-03-26 12:47:01" [19] "2009-03-26 12:52:01" "2009-03-26 12:57:01" "2009-03-26 13:02:02" "2009-03-26 13:07:02" "2009-03-26 13:12:03" "2009-03-26 13:17:03" [25] "2009-03-26 13:22:03" "2009-03-26 13:27:03" "2009-03-26 13:32:03" "2009-03-26 13:37:03" "2009-03-26 13:42:03" "2009-03-26 13:47:03" [31] "2009-03-26 13:52:03" "2009-03-26 13:57:04" "2009-03-26 14:01:02" "2009-03-26 14:06:05" "2009-03-26 14:11:05" "2009-03-26 14:16:06" [37] "2009-03-26 14:21:06" "2009-03-26 14:26:08" "2009-03-26 14:31:09" "2009-03-26 14:36:10" "2009-03-26 14:41:10" "2009-03-26 14:46:15" [43] "2009-03-26 14:51:15" "2009-03-26 14:56:15" "2009-03-26 15:01:15" "2009-03-26 15:06:17" "2009-03-26 15:11:17" "2009-03-26 15:16:19" [49] "2009-03-26 15:21:19" "2009-03-26 15:26:19" "2009-03-26 15:31:22" "2009-03-26 15:36:23" "2009-03-26 15:41:24" "2009-03-26 15:46:24" [55] "2009-03-26 15:51:25" "2009-03-26 15:56:25" "2009-03-26 16:01:25" "2009-03-26 16:06:26" "2009-03-26 16:11:26" "2009-03-26 16:16:26" [61] "2009-03-26 16:21:27" "2009-03-26 16:26:27" "2009-03-26 16:31:28" "2009-03-26 16:36:28" "2009-03-26 16:41:29" "2009-03-26 16:46:30" [67] "2009-03-26 16:51:31" "2009-03-26 16:56:31" "2009-03-26 17:01:32" "2009-03-26 17:06:32" "2009-03-26 17:11:33" "2009-03-26 17:16:33" [73] "2009-03-26 17:21:33" "2009-03-26 17:26:35" "2009-03-26 17:31:36" "2009-03-26 17:36:36" "2009-03-26 17:41:36" "2009-03-26 17:46:36" [79] "2009-03-26 17:51:39" "2009-03-26 17:56:40" "2009-03-26 18:01:40" "2009-03-26 18:06:40" "2009-03-26 18:11:40" "2009-03-26 18:16:40" [85] "2009-03-26 18:21:41" "2009-03-26 18:26:41" "2009-03-26 18:31:41" "2009-03-26 18:36:41" "2009-03-26 18:41:41" "2009-03-26 18:46:41" [91] "2009-03-26 18:51:42" "2009-03-26 18:56:42" "2009-03-26 19:06:42"> > as.numeric(diff(strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S"), > units="mins"))[1] 46 301 581 300 300 300 301 300 300 300 300 300 301 300 300 300 300 300 300 301 300 301 300 300 300 300 300 300 300 300 301 238 303 300 301 300 302 301 [39] 301 300 305 300 300 300 302 300 302 300 300 303 301 301 300 301 300 300 301 300 300 301 300 301 300 301 301 301 300 301 300 301 300 300 302 301 300 300 [77] 300 303 301 300 300 300 300 301 300 300 300 300 300 301 300 600 ------------------------------------------------------------------------> strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S")[1] "2009-03-26 11:22:24" "2009-03-26 11:27:25" "2009-03-26 11:37:04" "2009-03-26 11:42:04" "2009-03-26 11:47:04" "2009-03-26 11:52:04" [7] "2009-03-26 11:57:04" "2009-03-26 12:02:05" "2009-03-26 12:07:06" "2009-03-26 12:12:06" "2009-03-26 12:17:06" "2009-03-26 12:22:06" [13] "2009-03-26 12:27:07" "2009-03-26 12:32:07" "2009-03-26 12:37:07" "2009-03-26 12:42:07" "2009-03-26 12:47:07" "2009-03-26 12:52:08" [19] "2009-03-26 12:57:08" "2009-03-26 13:02:08" "2009-03-26 13:07:09" "2009-03-26 13:12:09" "2009-03-26 13:17:09" "2009-03-26 13:22:09" [25] "2009-03-26 13:27:09" "2009-03-26 13:32:09" "2009-03-26 13:37:09" "2009-03-26 13:42:09" "2009-03-26 13:47:09" "2009-03-26 13:52:09" [31] "2009-03-26 13:57:10" "2009-03-26 14:01:08" "2009-03-26 14:06:11" "2009-03-26 14:11:11" "2009-03-26 14:16:12" "2009-03-26 14:21:12" [37] "2009-03-26 14:26:15" "2009-03-26 14:31:18" "2009-03-26 14:36:18" "2009-03-26 14:41:19" "2009-03-26 14:46:22" "2009-03-26 14:51:22" [43] "2009-03-26 14:56:23" "2009-03-26 15:01:24" "2009-03-26 15:06:24" "2009-03-26 15:11:24" "2009-03-26 15:16:24" "2009-03-26 15:21:24" [49] "2009-03-26 15:26:24" "2009-03-26 15:31:28" "2009-03-26 15:36:29" "2009-03-26 15:41:29" "2009-03-26 15:46:30" "2009-03-26 15:51:33" [55] "2009-03-26 15:56:33" "2009-03-26 16:01:33" "2009-03-26 16:06:33" "2009-03-26 16:11:34" "2009-03-26 16:16:34" "2009-03-26 16:21:35" [61] "2009-03-26 16:26:35" "2009-03-26 16:31:35" "2009-03-26 16:36:35" "2009-03-26 16:41:35" "2009-03-26 16:46:36" "2009-03-26 16:51:37" [67] "2009-03-26 16:56:37" "2009-03-26 17:01:37" "2009-03-26 17:06:38" "2009-03-26 17:11:38" "2009-03-26 17:16:38" "2009-03-26 17:21:38" [73] "2009-03-26 17:26:40" "2009-03-26 17:31:43" "2009-03-26 17:36:43" "2009-03-26 17:41:43" "2009-03-26 17:46:44" "2009-03-26 17:51:45" [79] "2009-03-26 17:56:46" "2009-03-26 18:01:46" "2009-03-26 18:06:46" "2009-03-26 18:11:47" "2009-03-26 18:16:47" "2009-03-26 18:21:48" [85] "2009-03-26 18:26:48" "2009-03-26 18:31:48" "2009-03-26 18:36:48" "2009-03-26 18:41:48" "2009-03-26 18:46:48" "2009-03-26 18:51:48" [91] "2009-03-26 18:56:48" "2009-03-26 19:01:48"> > as.numeric(diff(strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S"), > units="mins"))[1] 5.016667 9.650000 5.000000 5.000000 5.000000 5.000000 5.016667 5.016667 5.000000 5.000000 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 [17] 5.016667 5.000000 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.016667 3.966667 5.050000 [33] 5.000000 5.016667 5.000000 5.050000 5.050000 5.000000 5.016667 5.050000 5.000000 5.016667 5.016667 5.000000 5.000000 5.000000 5.000000 5.000000 [49] 5.066667 5.016667 5.000000 5.016667 5.050000 5.000000 5.000000 5.000000 5.016667 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 5.016667 [65] 5.016667 5.000000 5.000000 5.016667 5.000000 5.000000 5.000000 5.033333 5.050000 5.000000 5.000000 5.016667 5.016667 5.016667 5.000000 5.000000 [81] 5.016667 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000>------------------------------------------------------------------------ -- View this message in context: http://www.nabble.com/Results-sometimes-in-seconds-with-difftime-unit%3Dmins-tp22743564p22743564.html Sent from the R help mailing list archive at Nabble.com.
jim holtman
2009-Mar-27 15:55 UTC
[R] Re sults sometimes in seconds with difftime unit=mins
I think the problem is is that 'diff' does not have a 'units' parameter; 'difftime' does. Here is a way of doing it:> x[1] "2009-03-27 13:00:00 EDT" "2009-03-27 13:00:35 EDT" "2009-03-27 13:01:10 EDT" "2009-03-27 13:01:45 EDT" "2009-03-27 13:02:20 EDT" "2009-03-27 13:02:55 EDT" "2009-03-27 13:03:30 EDT" [8] "2009-03-27 13:04:05 EDT" "2009-03-27 13:04:40 EDT" "2009-03-27 13:05:15 EDT"> difftime(tail(x, -1), head(x, -1), units='mins')Time differences in mins [1] 0.5833333 0.5833333 0.5833333 0.5833333 0.5833333 0.5833333 0.5833333 0.5833333 0.5833333 attr(,"tzone") [1] ""> diff(x, units='mins') # 'units' ignoredTime differences in secs [1] 35 35 35 35 35 35 35 35 35 attr(,"tzone") [1] "">On Fri, Mar 27, 2009 at 11:04 AM, Ptit_Bleu <ptit_bleu at yahoo.fr> wrote:> > Hello, > > I'm trying to calculate an integration and x-axis is a time (format : > %Y-%m-%d %H:%M:%S"). > I use diff(date, units="mins") in a loop for but sometimes the results stay > in seconds (95% is ok). > > Examples for 2 sets of data are given below (first result stays in seconds > whereas the second in minutes as expected). > Have you already seen this behaviour ? > Any idea to solve this problem ? > > Thanks in advance. > Have a good week-end, > Ptit Bleu. > > ------------------------------------------------------------------------ >> strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S") > ?[1] "2009-03-26 11:21:31" "2009-03-26 11:22:17" "2009-03-26 11:27:18" > "2009-03-26 11:36:59" "2009-03-26 11:41:59" "2009-03-26 11:46:59" > ?[7] "2009-03-26 11:51:59" "2009-03-26 11:57:00" "2009-03-26 12:02:00" > "2009-03-26 12:07:00" "2009-03-26 12:12:00" "2009-03-26 12:17:00" > [13] "2009-03-26 12:22:00" "2009-03-26 12:27:01" "2009-03-26 12:32:01" > "2009-03-26 12:37:01" "2009-03-26 12:42:01" "2009-03-26 12:47:01" > [19] "2009-03-26 12:52:01" "2009-03-26 12:57:01" "2009-03-26 13:02:02" > "2009-03-26 13:07:02" "2009-03-26 13:12:03" "2009-03-26 13:17:03" > [25] "2009-03-26 13:22:03" "2009-03-26 13:27:03" "2009-03-26 13:32:03" > "2009-03-26 13:37:03" "2009-03-26 13:42:03" "2009-03-26 13:47:03" > [31] "2009-03-26 13:52:03" "2009-03-26 13:57:04" "2009-03-26 14:01:02" > "2009-03-26 14:06:05" "2009-03-26 14:11:05" "2009-03-26 14:16:06" > [37] "2009-03-26 14:21:06" "2009-03-26 14:26:08" "2009-03-26 14:31:09" > "2009-03-26 14:36:10" "2009-03-26 14:41:10" "2009-03-26 14:46:15" > [43] "2009-03-26 14:51:15" "2009-03-26 14:56:15" "2009-03-26 15:01:15" > "2009-03-26 15:06:17" "2009-03-26 15:11:17" "2009-03-26 15:16:19" > [49] "2009-03-26 15:21:19" "2009-03-26 15:26:19" "2009-03-26 15:31:22" > "2009-03-26 15:36:23" "2009-03-26 15:41:24" "2009-03-26 15:46:24" > [55] "2009-03-26 15:51:25" "2009-03-26 15:56:25" "2009-03-26 16:01:25" > "2009-03-26 16:06:26" "2009-03-26 16:11:26" "2009-03-26 16:16:26" > [61] "2009-03-26 16:21:27" "2009-03-26 16:26:27" "2009-03-26 16:31:28" > "2009-03-26 16:36:28" "2009-03-26 16:41:29" "2009-03-26 16:46:30" > [67] "2009-03-26 16:51:31" "2009-03-26 16:56:31" "2009-03-26 17:01:32" > "2009-03-26 17:06:32" "2009-03-26 17:11:33" "2009-03-26 17:16:33" > [73] "2009-03-26 17:21:33" "2009-03-26 17:26:35" "2009-03-26 17:31:36" > "2009-03-26 17:36:36" "2009-03-26 17:41:36" "2009-03-26 17:46:36" > [79] "2009-03-26 17:51:39" "2009-03-26 17:56:40" "2009-03-26 18:01:40" > "2009-03-26 18:06:40" "2009-03-26 18:11:40" "2009-03-26 18:16:40" > [85] "2009-03-26 18:21:41" "2009-03-26 18:26:41" "2009-03-26 18:31:41" > "2009-03-26 18:36:41" "2009-03-26 18:41:41" "2009-03-26 18:46:41" > [91] "2009-03-26 18:51:42" "2009-03-26 18:56:42" "2009-03-26 19:06:42" >> >> as.numeric(diff(strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S"), >> units="mins")) > ?[1] ?46 301 581 300 300 300 301 300 300 300 300 300 301 300 300 300 300 300 > 300 301 300 301 300 300 300 300 300 300 300 300 301 238 303 300 301 300 302 > 301 > [39] 301 300 305 300 300 300 302 300 302 300 300 303 301 301 300 301 300 300 > 301 300 300 301 300 301 300 301 301 301 300 301 300 301 300 300 302 301 300 > 300 > [77] 300 303 301 300 300 300 300 301 300 300 300 300 300 301 300 600 > > ------------------------------------------------------------------------ >> strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S") > ?[1] "2009-03-26 11:22:24" "2009-03-26 11:27:25" "2009-03-26 11:37:04" > "2009-03-26 11:42:04" "2009-03-26 11:47:04" "2009-03-26 11:52:04" > ?[7] "2009-03-26 11:57:04" "2009-03-26 12:02:05" "2009-03-26 12:07:06" > "2009-03-26 12:12:06" "2009-03-26 12:17:06" "2009-03-26 12:22:06" > [13] "2009-03-26 12:27:07" "2009-03-26 12:32:07" "2009-03-26 12:37:07" > "2009-03-26 12:42:07" "2009-03-26 12:47:07" "2009-03-26 12:52:08" > [19] "2009-03-26 12:57:08" "2009-03-26 13:02:08" "2009-03-26 13:07:09" > "2009-03-26 13:12:09" "2009-03-26 13:17:09" "2009-03-26 13:22:09" > [25] "2009-03-26 13:27:09" "2009-03-26 13:32:09" "2009-03-26 13:37:09" > "2009-03-26 13:42:09" "2009-03-26 13:47:09" "2009-03-26 13:52:09" > [31] "2009-03-26 13:57:10" "2009-03-26 14:01:08" "2009-03-26 14:06:11" > "2009-03-26 14:11:11" "2009-03-26 14:16:12" "2009-03-26 14:21:12" > [37] "2009-03-26 14:26:15" "2009-03-26 14:31:18" "2009-03-26 14:36:18" > "2009-03-26 14:41:19" "2009-03-26 14:46:22" "2009-03-26 14:51:22" > [43] "2009-03-26 14:56:23" "2009-03-26 15:01:24" "2009-03-26 15:06:24" > "2009-03-26 15:11:24" "2009-03-26 15:16:24" "2009-03-26 15:21:24" > [49] "2009-03-26 15:26:24" "2009-03-26 15:31:28" "2009-03-26 15:36:29" > "2009-03-26 15:41:29" "2009-03-26 15:46:30" "2009-03-26 15:51:33" > [55] "2009-03-26 15:56:33" "2009-03-26 16:01:33" "2009-03-26 16:06:33" > "2009-03-26 16:11:34" "2009-03-26 16:16:34" "2009-03-26 16:21:35" > [61] "2009-03-26 16:26:35" "2009-03-26 16:31:35" "2009-03-26 16:36:35" > "2009-03-26 16:41:35" "2009-03-26 16:46:36" "2009-03-26 16:51:37" > [67] "2009-03-26 16:56:37" "2009-03-26 17:01:37" "2009-03-26 17:06:38" > "2009-03-26 17:11:38" "2009-03-26 17:16:38" "2009-03-26 17:21:38" > [73] "2009-03-26 17:26:40" "2009-03-26 17:31:43" "2009-03-26 17:36:43" > "2009-03-26 17:41:43" "2009-03-26 17:46:44" "2009-03-26 17:51:45" > [79] "2009-03-26 17:56:46" "2009-03-26 18:01:46" "2009-03-26 18:06:46" > "2009-03-26 18:11:47" "2009-03-26 18:16:47" "2009-03-26 18:21:48" > [85] "2009-03-26 18:26:48" "2009-03-26 18:31:48" "2009-03-26 18:36:48" > "2009-03-26 18:41:48" "2009-03-26 18:46:48" "2009-03-26 18:51:48" > [91] "2009-03-26 18:56:48" "2009-03-26 19:01:48" >> >> as.numeric(diff(strptime(datajour$Date, format="%Y-%m-%d %H:%M:%S"), >> units="mins")) > ?[1] 5.016667 9.650000 5.000000 5.000000 5.000000 5.000000 5.016667 5.016667 > 5.000000 5.000000 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 > [17] 5.016667 5.000000 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 > 5.000000 5.000000 5.000000 5.000000 5.000000 5.016667 3.966667 5.050000 > [33] 5.000000 5.016667 5.000000 5.050000 5.050000 5.000000 5.016667 5.050000 > 5.000000 5.016667 5.016667 5.000000 5.000000 5.000000 5.000000 5.000000 > [49] 5.066667 5.016667 5.000000 5.016667 5.050000 5.000000 5.000000 5.000000 > 5.016667 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 5.016667 > [65] 5.016667 5.000000 5.000000 5.016667 5.000000 5.000000 5.000000 5.033333 > 5.050000 5.000000 5.000000 5.016667 5.016667 5.016667 5.000000 5.000000 > [81] 5.016667 5.000000 5.016667 5.000000 5.000000 5.000000 5.000000 5.000000 > 5.000000 5.000000 5.000000 >> > ------------------------------------------------------------------------ > > -- > View this message in context: http://www.nabble.com/Results-sometimes-in-seconds-with-difftime-unit%3Dmins-tp22743564p22743564.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?