r - splicing time intervals posixct -


i have following time intervals split 10 equally spaced instances.

 head(data)              stoptime           starttime 1 2014-08-19 14:52:04 2014-08-19 15:22:04 2 2014-08-19 16:27:14 2014-08-19 17:17:33 3 2014-08-19 18:05:59 2014-08-19 18:09:12 4 2014-08-19 17:25:35 2014-08-19 17:29:06 5 2014-08-19 18:23:29 2014-08-19 18:57:34 6 2014-08-19 07:39:15 2014-08-19 07:48:49  

i able take midpoint using code

 one_day$midtime = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) /2 , origin = '1970-01-01') 

however, when try extend code ten equally spaced instances goes wrong. why happening , how can fix code?

 one_day$first = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .1 , origin = '1970-01-01') one_day$second = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .2, origin = '1970-01-01') one_day$thrid = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .3, origin = '1970-01-01') one_day$fourth = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .4, origin = '1970-01-01') one_day$fifth = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .5, origin = '1970-01-01') one_day$sixth = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .6, origin = '1970-01-01') one_day$seventh = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .7, origin = '1970-01-01') one_day$eighth = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .8, origin = '1970-01-01') one_day$ninth = as.posixct((as.numeric(one_day$stoptime) + as.numeric(one_day$starttime)) * .9, origin = '1970-01-01') 
 head(one_day)   diff.time            stoptime           starttime             midtime               first 1      1800 2014-08-19 14:52:04 2014-08-19 15:22:04 2014-08-19 15:07:04 1978-12-05 03:49:24 2      3019 2014-08-19 16:27:14 2014-08-19 17:17:33 2014-08-19 16:52:23 1978-12-05 04:10:28 3       193 2014-08-19 18:05:59 2014-08-19 18:09:12 2014-08-19 18:07:35 1978-12-05 04:25:31 4       211 2014-08-19 17:25:35 2014-08-19 17:29:06 2014-08-19 17:27:20 1978-12-05 04:17:28 5      2045 2014-08-19 18:23:29 2014-08-19 18:57:34 2014-08-19 18:40:31 1978-12-05 04:32:06 6       574 2014-08-19 07:39:15 2014-08-19 07:48:49 2014-08-19 07:44:02 1978-12-05 02:20:48                second               thrid              fourth               fifth               sixth 1 1987-11-08 12:38:49 1996-10-11 21:28:14 2005-09-15 06:17:39 2014-08-19 15:07:04 2023-07-23 23:56:28 2 1987-11-08 13:20:57 1996-10-11 22:31:26 2005-09-15 07:41:54 2014-08-19 16:52:23 2023-07-24 02:02:52 3 1987-11-08 13:51:02 1996-10-11 23:16:33 2005-09-15 08:42:04 2014-08-19 18:07:35 2023-07-24 03:33:06 4 1987-11-08 13:34:56 1996-10-11 22:52:24 2005-09-15 08:09:52 2014-08-19 17:27:20 2023-07-24 02:44:48 5 1987-11-08 14:04:12 1996-10-11 23:36:18 2005-09-15 09:08:25 2014-08-19 18:40:31 2023-07-24 04:12:37 6 1987-11-08 09:41:36 1996-10-11 17:02:25 2005-09-15 00:23:13 2014-08-19 07:44:02 2023-07-23 15:04:50               seventh              eighth               ninth 1 2032-06-26 08:45:53 2041-05-30 17:35:18 2050-05-04 02:24:43 2 2032-06-26 11:13:20 2041-05-30 20:23:49 2050-05-04 05:34:18 3 2032-06-26 12:58:37 2041-05-30 22:24:08 2050-05-04 07:49:39 4 2032-06-26 12:02:16 2041-05-30 21:19:44 2050-05-04 06:37:12 5 2032-06-26 13:44:44 2041-05-30 23:16:50 2050-05-04 08:48:56 6 2032-06-25 22:25:38 2041-05-30 05:46:27 2050-05-03 13:07:15 
 dput(data1) structure(list(stoptime = structure(c(1408477924, 1408483634,  1408489559, 1408487135, 1408490609, 1408451955, 1408452727, 1408498708,  1408486644, 1408454996), class = c("posixct", "posixt"), tzone = "est"),      starttime = structure(c(1408479724, 1408486653, 1408489752,      1408487346, 1408492654, 1408452529, 1408455826, 1408501153,      1408488389, 1408458514), class = c("posixct", "posixt"), tzone = "est")), .names = c("stoptime",  "starttime"), row.names = c(na, 10l), class = "data.frame")  

1: seq

first of have convert columns of dataframe posixct or posixlt class, because r base function seq has method objects of class.

just see semplified code:

library(lubridate) <- "2014-08-19 14:52:04" b <- "2014-08-19 15:22:04"  <- ymd_hms(a) b <- ymd_hms(b)  [1] "2014-08-19 14:52:04 utc" b [1] "2014-08-19 15:22:04 utc" 

then have use seq function , set parameters length.out value of sequence seeking. code automatically create sequence of values start end equally divided.

seq(a, b, length.out = 10) [1] "2014-08-19 14:52:04 utc" "2014-08-19 14:55:24 utc" [3] "2014-08-19 14:58:44 utc" "2014-08-19 15:02:04 utc" [5] "2014-08-19 15:05:24 utc" "2014-08-19 15:08:44 utc" [7] "2014-08-19 15:12:04 utc" "2014-08-19 15:15:24 utc" [9] "2014-08-19 15:18:44 utc" "2014-08-19 15:22:04 utc" 

2: vectorize step 1

now know how achieve goal, matter of trying how vectorize along values.

i bet there several approaches, here one. mapply function can loop trough elements , match first element (of first object) first element (of second object) , on. keep in mind have specify parameters fixed morearg arguments.

here code:

mapply(seq,        = data1$starttime,        = data1$stoptime,        moreargs = list(length.out = 10),        simplify = f) 

that produces list of desired data not in desired format sadly:

[[1]]  [1] "2014-08-19 14:52:04 utc" "2014-08-19 14:55:24 utc"  [3] "2014-08-19 14:58:44 utc" "2014-08-19 15:02:04 utc"  [5] "2014-08-19 15:05:24 utc" "2014-08-19 15:08:44 utc"  [7] "2014-08-19 15:12:04 utc" "2014-08-19 15:15:24 utc"  [9] "2014-08-19 15:18:44 utc" "2014-08-19 15:22:04 utc"  [[2]]  [1] "2014-08-19 16:27:14 utc" "2014-08-19 16:32:49 utc"  [3] "2014-08-19 16:38:24 utc" "2014-08-19 16:44:00 utc"  [5] "2014-08-19 16:49:35 utc" "2014-08-19 16:55:11 utc"  [7] "2014-08-19 17:00:46 utc" "2014-08-19 17:06:22 utc"  [9] "2014-08-19 17:11:57 utc" "2014-08-19 17:17:33 utc"  [[3]]  [1] "2014-08-19 18:05:59 utc" "2014-08-19 18:06:20 utc"  [3] "2014-08-19 18:06:41 utc" "2014-08-19 18:07:03 utc"  [5] "2014-08-19 18:07:24 utc" "2014-08-19 18:07:46 utc"  [7] "2014-08-19 18:08:07 utc" "2014-08-19 18:08:29 utc"  [9] "2014-08-19 18:08:50 utc" "2014-08-19 18:09:12 utc"  [[4]]  [1] "2014-08-19 17:25:35 utc" "2014-08-19 17:25:58 utc"  [3] "2014-08-19 17:26:21 utc" "2014-08-19 17:26:45 utc"  [5] "2014-08-19 17:27:08 utc" "2014-08-19 17:27:32 utc"  [7] "2014-08-19 17:27:55 utc" "2014-08-19 17:28:19 utc"  [9] "2014-08-19 17:28:42 utc" "2014-08-19 17:29:06 utc"  [[5]]  [1] "2014-08-19 18:23:29 utc" "2014-08-19 18:27:16 utc"  [3] "2014-08-19 18:31:03 utc" "2014-08-19 18:34:50 utc"  [5] "2014-08-19 18:38:37 utc" "2014-08-19 18:42:25 utc"  [7] "2014-08-19 18:46:12 utc" "2014-08-19 18:49:59 utc"  [9] "2014-08-19 18:53:46 utc" "2014-08-19 18:57:34 utc"  [[6]]  [1] "2014-08-19 07:39:15 utc" "2014-08-19 07:40:18 utc"  [3] "2014-08-19 07:41:22 utc" "2014-08-19 07:42:26 utc"  [5] "2014-08-19 07:43:30 utc" "2014-08-19 07:44:33 utc"  [7] "2014-08-19 07:45:37 utc" "2014-08-19 07:46:41 utc"  [9] "2014-08-19 07:47:45 utc" "2014-08-19 07:48:49 utc"  [[7]]  [1] "2014-08-19 07:52:07 utc" "2014-08-19 07:57:51 utc"  [3] "2014-08-19 08:03:35 utc" "2014-08-19 08:09:20 utc"  [5] "2014-08-19 08:15:04 utc" "2014-08-19 08:20:48 utc"  [7] "2014-08-19 08:26:33 utc" "2014-08-19 08:32:17 utc"  [9] "2014-08-19 08:38:01 utc" "2014-08-19 08:43:46 utc"  [[8]]  [1] "2014-08-19 20:38:28 utc" "2014-08-19 20:42:59 utc"  [3] "2014-08-19 20:47:31 utc" "2014-08-19 20:52:03 utc"  [5] "2014-08-19 20:56:34 utc" "2014-08-19 21:01:06 utc"  [7] "2014-08-19 21:05:38 utc" "2014-08-19 21:10:09 utc"  [9] "2014-08-19 21:14:41 utc" "2014-08-19 21:19:13 utc"  [[9]]  [1] "2014-08-19 17:17:24 utc" "2014-08-19 17:20:37 utc"  [3] "2014-08-19 17:23:51 utc" "2014-08-19 17:27:05 utc"  [5] "2014-08-19 17:30:19 utc" "2014-08-19 17:33:33 utc"  [7] "2014-08-19 17:36:47 utc" "2014-08-19 17:40:01 utc"  [9] "2014-08-19 17:43:15 utc" "2014-08-19 17:46:29 utc"  [[10]]  [1] "2014-08-19 08:29:56 utc" "2014-08-19 08:36:26 utc"  [3] "2014-08-19 08:42:57 utc" "2014-08-19 08:49:28 utc"  [5] "2014-08-19 08:55:59 utc" "2014-08-19 09:02:30 utc"  [7] "2014-08-19 09:09:01 utc" "2014-08-19 09:15:32 utc"  [9] "2014-08-19 09:22:03 utc" "2014-08-19 09:28:34 utc" 

at point guess matter of same data manipulation can't figure out way (now).


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -