Selecting dates and time interval from observations in R - r

Having an object of class zoo we can select observations for a range of dates of interest using the function:
window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))
In running this function the following warning message occurs:
Warning messages:
1: In which(in.index & all.indexes >= start & all.indexes <= end) :
Metodi incompatibili ("Ops.POSIXt", "Ops.Date") per ">="
2: In which(in.index & all.indexes >= start & all.indexes <= end) :
Metodi incompatibili ("Ops.POSIXt", "Ops.Date") per "<="
I have checked that the object is of class zoo and that Dates are included in the time series.
How is that possible?
Below the str(z) as requested:
‘zoo’ series from 2006-01-03 to 2013-01-24
Data: num [1:1795, 1:40] 3.65 3.68 3.69 3.72 3.7 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:1795] "1" "2" "3" "4" ...
..$ : chr [1:40] "EURARS" "EURAUD" "EURBRO" "EURCAD" ...
Index: POSIXct[1:1795], format: "2006-01-03" "2006-01-04" "2006-01-05" "2006-01-06" ...
Below the dput(head(z)) as requested:
structure(c(3.6511, 3.6833, 3.6931, 3.7152, 3.7027, 3.6897, 1.62349,
1.62257, 1.62011, 1.6115, 1.60243, 1.61108, 2.802, 2.7692, 2.7727,
2.7741, 2.7238, 2.729, 1.38937, 1.39109, 1.40716, 1.41627, 1.41196,
1.40666, 1.55055, 1.5472, 1.5448, 1.54335, 1.54215, 1.545, 623.73,
624.16, 628.43, 638.11, 632.27, 630.7, 9.6988, 9.7803, 9.7689,
9.802, 9.7492, 9.7354, 2742.03, 2765.68, 2758.65, 2769.27, 2753.3,
2747.31, 29.047, 28.972, 28.9, 28.88, 28.764, 28.792, 7.4616,
7.4601, 7.4612, 7.458, 7.46, 7.4589, 6.8983, 6.9551, 6.9594,
6.9838, 6.9374, 6.9253, 0.6882, 0.68905, 0.68961, 0.6863, 0.68473,
0.68358, 9.3178, 9.3963, 9.3889, 9.4207, 9.3702, 9.3516, 251.72,
250.28, 250.66, 250.39, 249.89, 250.86, 11657.46, 11677.26, 11612.05,
11603.59, 11433.35, 11403.84, 5.5244, 5.5808, 5.5799, 5.6134,
5.5858, 5.5957, 53.5288, 54.0151, 54.0323, 54.0105, 53.5591,
53.6189, 74.96, 74.88, 74.41, 73.94, 73.79, 73.84, 139.6, 140.71,
140.39, 139.09, 138.39, 137.93, 1208.3493, 1210.0214, 1195.7746,
1200.3966, 1181.3457, 1184.9635, 160.65, 162.15, 162.02, 162.53,
161.7, 161.43, 10.9802, 10.997, 10.9947, 10.9635, 10.9909, 10.9874,
12.7724, 12.8255, 12.8746, 12.8338, 12.7859, 12.8273, 4.5416,
4.5702, 4.5623, 4.5597, 4.5308, 4.5229, 7.9654, 7.9248, 7.9254,
7.914, 7.9574, 8.0011, 1.7571, 1.7626, 1.7622, 1.7574, 1.7411,
1.7391, 4.1276, 4.1608, 4.1665, 4.1818, 4.1606, 4.1534, 63.2627,
63.4733, 63.6725, 63.7198, 63.3608, 63.3412, 3.8295, 3.8116,
3.807, 3.805, 3.7609, 3.7799, 3.6732, 3.6815, 3.6834, 3.6842,
3.6644, 3.6492, 34.5475, 34.8363, 34.7254, 34.8369, 34.68, 34.39,
9.3648, 9.3279, 9.3321, 9.3152, 9.3389, 9.3603, 1.9844, 1.9932,
1.9938, 1.9902, 1.9766, 1.9716, 48.9853, 48.9426, 48.6762, 48.3184,
47.9995, 48.0187, 1.6149, 1.6195, 1.6193, 1.6204, 1.6155, 1.6148,
1.6129, 1.6175, 1.6184, 1.6201, 1.6182, 1.6221, 39.2261, 39.1868,
38.7569, 39.1189, 38.6148, 38.6309, 6.0673, 6.114, 6.1208, 6.1484,
6.1095, 6.1082, 1.2019, 1.2119, 1.211, 1.2151, 1.2088, 1.2065,
7.4834, 7.4559, 7.4658, 7.3872, 7.3206, 7.3497), .Dim = c(6L,
40L), .Dimnames = list(c("1", "2", "3", "4", "5", "6"), c("EURARS",
"EURAUD", "EURBRO", "EURCAD", "EURCHF", "EURCLP", "EURCNO", "EURCOP",
"EURCZK", "EURDKK", "EUREGP", "EURGBP", "EURHKD", "EURHUF", "EURIDO",
"EURILS", "EURINO", "EURISK", "EURJPY", "EURKRO", "EURKZT", "EURMAD",
"EURMXN", "EURMYO", "EURNOK", "EURNZD", "EURPEN", "EURPHO", "EURPLN",
"EURRON", "EURRUB", "EURSEK", "EURSGO", "EURTHO", "EURTND", "EURTRY",
"EURTWO", "EURUAH", "EURUSD", "EURZAR")), index = structure(c(1136242800,
1136329200, 1136415600, 1136502000, 1136761200, 1136847600), class = c("POSIXct",
"POSIXt"), tzone = ""), class = "zoo")

You shouldn't be comparing Date with POSIXct
Try this:
window(z, start = as.POSIXct("2006-01-05"), end = as.POSIXct("2006-01-08"))
Alternatively, as #JoshuaUlrich points out in a comment, your data is daily frequency, so you'd be better off using a Date index class for your data to avoid timezone weirdness.
index(z) <- as.Date(index(z))
window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))

It looks like window doesn't convert between Date and POSIXct automatically. You have to specify the times in the same class as in the data:
window(z, start = as.POSIXct("2006-01-05"), end = as.POSIXct("2006-01-08"))
EURARS EURAUD EURBRO EURCAD EURCHF EURCLP EURCNO EURCOP
2006-01-05 23:00:00 3.7152 1.6115 2.7741 1.41627 1.54335 638.11 9.802 2769.27
EURCZK EURDKK EUREGP EURGBP EURHKD EURHUF EURIDO EURILS
2006-01-05 23:00:00 28.88 7.458 6.9838 0.6863 9.4207 250.39 11603.59 5.6134
EURINO EURISK EURJPY EURKRO EURKZT EURMAD EURMXN
2006-01-05 23:00:00 54.0105 73.94 139.09 1200.397 162.53 10.9635 12.8338
EURMYO EURNOK EURNZD EURPEN EURPHO EURPLN EURRON EURRUB
2006-01-05 23:00:00 4.5597 7.914 1.7574 4.1818 63.7198 3.805 3.6842 34.8369
EURSEK EURSGO EURTHO EURTND EURTRY EURTWO EURUAH EURUSD
2006-01-05 23:00:00 9.3152 1.9902 48.3184 1.6204 1.6201 39.1189 6.1484 1.2151
EURZAR
2006-01-05 23:00:00 7.3872

Related

Averaging time-stamped data hourly in R

I've average distance travelled data meters for several days and times of the year. Here's how the dataset I'm working with looks like:
> print(datanet)
Date & Time [Local] meters
1: 2017-06-01 00:00:14 2.333355
2: 2017-06-01 01:00:13 6.952414
3: 2017-06-01 02:00:30 61.727543
4: 2017-06-01 03:00:15 235.873883
5: 2017-06-01 04:00:15 138.136375
---
1428: 2017-07-30 19:00:21 40.602983
1429: 2017-07-30 20:00:47 34.292888
1430: 2017-07-30 21:00:20 303.478297
1431: 2017-07-30 22:00:18 5.741059
Now, I would like to transform this table so that it provides the average distance travelled for each hour of the day from 0 to 23, based on data from multiple days. Here's the code I've been using for that purpose (additionally includes an sd column):
data_travel<-datanet %>%
mutate(
date = ymd_hms(`Date & Time [Local]`),
hour = hour(date)
) %>%
group_by(hour) %>%
summarise(
avg_meters = mean(meters),
sd_meters = sd(meters)
)
This works, but sadly the last hour of the day 23 always shows NA values:
> head(data_travel[19:24,])
# A tibble: 6 x 3
hour avg_meters sd_meters
<int> <dbl> <dbl>
1 18 57.4 109.
2 19 96.5 177.
3 20 121. 248.
4 21 141. 299.
5 22 76.4 86.4
6 23 NA NaN
Does somebody have an idea of how I could modify this code so that I also get the average distance travelled avg_meters and sd for hour 23? Any input is appreciated!
> dput(datanet[1:700,])
structure(list(`Date & Time [Local]` = structure(c(1464732013,
1464735613, 1464739229, 1464742813, 1464746413, 1464750018, 1464753629,
1464757213, 1464760813, 1464764430, 1464768013, 1464771629, 1464775214,
1464778818, 1464782429, 1464786013, 1464789623, 1464793214, 1464796813,
1464800413, 1464804029, 1464807612, 1464811213, 1464814830, 1464818411,
1464822013, 1464825629, 1464829213, 1464832812, 1464836417, 1464840021,
1464843612, 1464847211, 1464850830, 1464854412, 1464858030, 1464861629,
1464865211, 1464868814, 1464872416, 1464876030, 1464879611, 1464883212,
1464886811, 1464890430, 1464894017, 1464897611, 1464901217, 1464904830,
1464908419, 1464912011, 1464915610, 1464919207, 1464922850, 1464926410,
1464930004, 1464933623, 1464937249, 1464940811, 1464944410, 1464948011,
1464951629, 1464955218, 1464958811, 1464962430, 1464966018, 1464969610,
1464973214, 1464976848, 1464980410, 1464984010, 1464987610, 1464991230,
1464994811, 1464998409, 1465002003, 1465005610, 1465009222, 1465012817,
1465016402, 1465020030, 1465023618, 1465027214, 1465030846, 1465034419,
1465038020, 1465041690, 1465045212, 1465048849, 1465052409, 1465056029,
1465059617, 1465063209, 1465066830, 1465070410, 1465074009, 1465077630,
1465081240, 1465084804, 1465088401, 1465092010, 1465095622, 1465099222,
1465102852, 1465106449, 1465110010, 1465113612, 1465117270, 1465120849,
1465124408, 1465128011, 1465131617, 1465135230, 1465138848, 1465142408,
1465146008, 1465149612, 1465153250, 1465156808, 1465160427, 1465164016,
1465167601, 1465171229, 1465174830, 1465178415, 1465182054, 1465185656,
1465189209, 1465192807, 1465196422, 1465200029, 1465203732, 1465207230,
1465210848, 1465214408, 1465218050, 1465221629, 1465225216, 1465228823,
1465232415, 1465236015, 1465239615, 1465243256, 1465246830, 1465250414,
1465254008, 1465257629, 1465261214, 1465264814, 1465268449, 1465272007,
1465275630, 1465279255, 1465282845, 1465286460, 1465290013, 1465293617,
1465297226, 1465300814, 1465304418, 1465308029, 1465311614, 1465315214,
1465318814, 1465322429, 1465326007, 1465329607, 1465333213, 1465336813,
1465340422, 1465344013, 1465347615, 1465351230, 1465354813, 1465358413,
1465362014, 1465365630, 1465369216, 1465372830, 1465376413, 1465380013,
1465383606, 1465387230, 1465390817, 1465394430, 1465398013, 1465401613,
1465405213, 1465408829, 1465412411, 1465416013, 1465419613, 1465423229,
1465426812, 1465430412, 1465434011, 1465437614, 1465441224, 1465444812,
1465448413, 1465452013, 1465455629, 1465459213, 1465462817, 1465466430,
1465470011, 1465473611, 1465477211, 1465480830, 1465484412, 1465488012,
1465491611, 1465495230, 1465498820, 1465502426, 1465506011, 1465509606,
1465513212, 1465516830, 1465520419, 1465524003, 1465527611, 1465531227,
1465534850, 1465538403, 1465542010, 1465545612, 1465549229, 1465552811,
1465556425, 1465560011, 1465563611, 1465567227, 1465570819, 1465574430,
1465578018, 1465581610, 1465585210, 1465588830, 1465592418, 1465596009,
1465599602, 1465603209, 1465606829, 1465610450, 1465614009, 1465617609,
1465621210, 1465624817, 1465628422, 1465632017, 1465635610, 1465639210,
1465642829, 1465646417, 1465650008, 1465653630, 1465657209, 1465660813,
1465664451, 1465668003, 1465671609, 1465675228, 1465678816, 1465682409,
1465686002, 1465689608, 1465693229, 1465696816, 1465700401, 1465704008,
1465707629, 1465711217, 1465714809, 1465718429, 1465722016, 1465725608,
1465729201, 1465732830, 1465736415, 1465740024, 1465743630, 1465747215,
1465750811, 1465754430, 1465758016, 1465761607, 1465765230, 1465768828,
1465772415, 1465776016, 1465779613, 1465783260, 1465786808, 1465790414,
1465794014, 1465797659, 1465801216, 1465804807, 1465808430, 1465812030,
1465815614, 1465819220, 1465822816, 1465826408, 1465830029, 1465833613,
1465837215, 1465840829, 1465844414, 1465848008, 1465851627, 1465855213,
1465858814, 1465862414, 1465866029, 1465869607, 1465873207, 1465876814,
1465880429, 1465884015, 1465887630, 1465891207, 1465894815, 1465898429,
1465902014, 1465905614, 1465909218, 1465912824, 1465916413, 1465920013,
1465923618, 1465927229, 1465930813, 1465934413, 1465938013, 1465941613,
1465945229, 1465948811, 1465952412, 1465956022, 1465959626, 1465963214,
1465966830, 1465970412, 1465974030, 1465977606, 1465981230, 1465984813,
1465988429, 1465992012, 1465995613, 1465999229, 1466002812, 1466006404,
1466010012, 1466013626, 1466017211, 1466020830, 1466024412, 1466028012,
1466031619, 1466035211, 1466038813, 1466042430, 1466046004, 1466049612,
1466053212, 1466056830, 1466060420, 1466064026, 1466067603, 1466071211,
1466074829, 1466078410, 1466082019, 1466085619, 1466089211, 1466092804,
1466096429, 1466100011, 1466103611, 1466107230, 1466110821, 1466114422,
1466118009, 1466121611, 1466125234, 1466128803, 1466132412, 1466136027,
1466139610, 1466143210, 1466146834, 1466150410, 1466154034, 1466157609,
1466161210, 1466164834, 1466168403, 1466172007, 1466175620, 1466179202,
1466182802, 1466186433, 1466190020, 1466193609, 1466197209, 1466200802,
1466204433, 1466208009, 1466211609, 1466215202, 1466218854, 1466222419,
1466226009, 1466229634, 1466233208, 1466236811, 1466240426, 1466244008,
1466247608, 1466251234, 1466254826, 1466258409, 1466262008, 1466265609,
1466269233, 1466272808, 1466276408, 1466280011, 1466283647, 1466287216,
1466290808, 1466294408, 1466298033, 1466301601, 1466305210, 1466308825,
1466312407, 1466316009, 1466319634, 1466323208, 1466326833, 1466330408,
1466334007, 1466337633, 1466341210, 1466344834, 1466348419, 1466352015,
1466355616, 1466359223, 1466362820, 1466366415, 1466370014, 1466373622,
1466377205, 1466380815, 1466384414, 1466388033, 1466391608, 1466395206,
1466398833, 1466402415, 1466406017, 1466409634, 1466413214, 1466416817,
1466420431, 1466424008, 1466427615, 1466431234, 1466434813, 1466438434,
1466442013, 1466445614, 1466449207, 1466452835, 1466456413, 1466460013,
1466463613, 1466467213, 1466470834, 1466474413, 1466478014, 1466481633,
1466485223, 1466488806, 1466492416, 1466496035, 1466499613, 1466503206,
1466506835, 1466510412, 1466514012, 1466517635, 1466521213, 1466524823,
1466528413, 1466532010, 1466535605, 1466539234, 1466542812, 1466546405,
1466550012, 1466553612, 1466557234, 1466560813, 1466564412, 1466568035,
1466571605, 1466575214, 1466578835, 1466582412, 1466586012, 1466589605,
1466593222, 1466596811, 1466600412, 1466604005, 1466607629, 1466611212,
1466614811, 1466618430, 1466622019, 1466625612, 1466629230, 1466632820,
1466636411, 1466640010, 1466643629, 1466647213, 1466650811, 1466654430,
1466658019, 1466661619, 1466665218, 1466668812, 1466672430, 1466676011,
1466679610, 1466683220, 1466686830, 1466690419, 1466694010, 1466697614,
1466701247, 1466704818, 1466708411, 1466712028, 1466715618, 1466719203,
1466722810, 1466726423, 1466730010, 1466733609, 1466737223, 1466740817,
1466744403, 1466748031, 1466751617, 1466755210, 1466758811, 1466762411,
1466766030, 1466769619, 1466773212, 1466776847, 1466780417, 1466784009,
1466787609, 1466791231, 1466794818, 1466798409, 1466802010, 1466805609,
1466809230, 1466812817, 1466816401, 1466820009, 1466823660, 1466827217,
1466830831, 1466834409, 1466838008, 1466841619, 1466845216, 1466848808,
1466852404, 1466856021, 1466859616, 1466863232, 1466866808, 1466870408,
1466874011, 1466877630, 1466881215, 1466884818, 1466888417, 1466892019,
1466895616, 1466899207, 1466902809, 1466906430, 1466910016, 1466913611,
1466917226, 1466920821, 1466924416, 1466928008, 1466931622, 1466935231,
1466938815, 1466942431, 1466946015, 1466949629, 1466953215, 1466956808,
1466960415, 1466964030, 1466967614, 1466971215, 1466974812, 1466978430,
1466982014, 1466985614, 1466989208, 1466992830, 1466996413, 1467000014,
1467003614, 1467007230, 1467010814, 1467014415, 1467018015, 1467021630,
1467025214, 1467028830, 1467032415, 1467036030, 1467039614, 1467043206,
1467046815, 1467050430, 1467054012, 1467057613, 1467061213, 1467064814,
1467068430, 1467072013, 1467075608, 1467079250, 1467082814, 1467086414,
1467090007, 1467093629, 1467097214, 1467100824, 1467104413, 1467108013,
1467111630, 1467115213, 1467118813, 1467122429, 1467126006, 1467129613,
1467133213, 1467136829, 1467140412, 1467144013, 1467147613, 1467151214,
1467154829, 1467158410, 1467162027, 1467165605, 1467169212, 1467172813,
1467176428, 1467180012, 1467183619, 1467187220, 1467190812, 1467194405,
1467198012, 1467201629, 1467205218, 1467208830, 1467212411, 1467216004,
1467219611, 1467223211, 1467226829, 1467230411, 1467234012, 1467237612,
1467241229, 1467244811, 1467248405), class = c("POSIXct", "POSIXt"
), tzone = ""), meters = c(7.24497992499657, 4.87741163537199,
9.08560044628181, 80.6842320881314, 238.606484922097, 157.204921816723,
625.23872908032, 219.35778781259, 12.6588736944506, 93.8090439559674,
319.445131807673, 67.8036768396769, 804.804836152127, 109.434600933436,
129.949236899749, 105.911149760734, 27.9531918089091, 11.27836453714,
457.093853355937, 26.5240927781247, 19.7015020304213, 14.3532653640863,
1.25853679670009, 0.150718694512225, 1.70366003911483, 2.63870002711148,
127.037462401145, 961.452700995197, 215.04628486518, 48.3476802703997,
56.4299311045402, 71.0567210386123, 53.2157129067539, 80.4040760406296,
236.078682140782, 406.948035573002, 92.6423364709784, 403.797511366086,
323.858212895809, 65.9783289318472, 26.7161400634748, 21.4406886404941,
44.6906704150594, 36.0784092780547, 66.4678272178005, 68.0358199816987,
2.1476323514823, 3.01587341033808, 1.57380761082474, 1.71653324348141,
18.8397076847765, 184.268772826548, 61.2103183204004, 82.9010640232318,
43.7120771884048, 40.4214303580113, 220.354835462908, 77.5844706628055,
10.6522275628958, 64.6401569172547, 170.237028243589, 235.781539666942,
206.150503465281, 25.3213069661311, 36.7436253838348, 9.83110790227874,
23.3459053606757, 3.45271958972457, 1.96114320043511, 20.4049146593214,
15.2372682099889, 20.3543121890185, 42.7350584816069, 12.1313207862892,
1.11708614676525, 191.836648404227, 33.3046462595366, 166.168666618136,
31.1722631768611, 133.717766242875, 12.0334817161546, 62.2359071313657,
16.7484729490856, 109.549479467076, 438.080739581294, 37.0971614841641,
105.391252306762, 122.494788370234, 88.6622245013997, 24.3191344096727,
5.117649955497, 51.9358625225939, 47.9478783281661, 6.96463276369705,
1.75025309899143, 31.176657161161, 10.1169843733554, 26.5346636683759,
15.9584899969855, 337.838831129694, 59.7693703670957, 46.7853809521572,
16.799710673628, 39.1979373391332, 122.408881979713, 266.855999717221,
63.8055787186155, 57.9900269187913, 120.78876572575, 82.1213040340665,
105.298734249817, 161.923229191297, 28.9509612131438, 0.248722765246352,
6.42826019283635, 4.80096922046293, 7.66924438494585, 3.77931970556652,
6.16226345339552, 1.89180927192504, 2.92660299028088, 4.47513027348909,
5.3772236196912, 258.79885256986, 76.3673624568927, 227.248769639605,
119.571707120552, 35.7102849958032, 36.9949248244319, 137.90048603805,
96.6658682838857, 259.080913058415, 105.606050276669, 56.1002922989478,
85.2381765021222, 191.363093870704, 55.7981801107081, 12.9578924739909,
26.3419895578265, 14.4503596334286, 15.6675803413194, 21.9669267962415,
63.1276880372023, 5.54867147176836, 9.0179124542279, 7.13599657582419,
69.6648263961824, 352.989183299746, 263.287397250075, 253.766882591523,
209.967849272818, 73.9692977527144, 98.0159993160327, 170.190795021595,
282.190504225449, 78.6666650047386, 27.0630775295066, 332.829084995611,
194.938072897224, 102.422860453484, 17.7992858642505, 13.1266890679012,
4.58091256610204, 6.40555894626406, 2.66715489350561, 19.248878078399,
14.0807810821772, 4.57816759344819, 4.40196859830686, 10.7329317290172,
32.4528952520776, 138.596548507858, 125.547606032588, 46.3652014291144,
16.3797234392651, 10.249071010749, 248.440266699442, 304.347056271548,
154.412296810916, 46.8081932028809, 226.453483692211, 431.805061221221,
111.437754042661, 217.641929376792, 25.1923986792615, 12.2256823484931,
12.2949586884092, 31.0958526630604, 85.5575841303107, 22.7975660566324,
30.5216893316272, 35.1775681936213, 9.50846937534727, 9.76657486076715,
0.579469646956765, 266.224607309967, 188.727073099707, 292.559096086872,
9.49743703683714, 107.113753739463, 175.681846441223, 19.379648871926,
88.2778253322274, 410.496903513497, 8.97162022276979, 32.4619475881012,
73.646386222969, 75.348171202073, 90.8515841171699, 10.6200903372279,
3.02379387011622, 32.825022046837, 65.550868227495, 24.9833819842075,
77.5493346217115, 2.10544392843427, 2.32222002989636, 3.12605565461408,
1.02518236594501, 341.46710436094, 151.588096225148, 353.933570258634,
124.173972566209, 60.4110080957218, 38.5295043269143, 154.717374816579,
10.642332114307, 112.19511336859, 178.656934678561, 144.883837500965,
193.991868696415, 202.99316836535, 77.6189915466929, 0.871460936515423,
1.63576829944789, 47.5439446635587, 60.241399209101, 92.7059630247652,
1.71653312232677, 3.28998417221502, 16.9888823353554, 1.073227079111,
31.3529682130551, 98.2633746496518, 146.311948071212, 277.215271024987,
30.6004645511119, 49.4907657584358, 17.6377880041836, 517.661457540348,
581.555783536356, 1010.85341607138, 101.36421835411, 101.587448595859,
144.303729077564, 91.3938747436922, 149.518556866971, 36.3308699793953,
7.80121835979054, 23.0312990229266, 13.41048184825, 20.339107047676,
3.08847373655867, 31.7536206163432, 80.5523050297356, 5.57519986215111,
12.7301911126705, 265.400347490029, 96.389278202961, 96.4450196328944,
269.701595926116, 40.1994744716222, 185.194247845766, 132.799182823423,
92.6508846479433, 31.7196753780259, 82.6725380176083, 149.907487149117,
259.995942351777, 136.962271891916, 47.7342981878729, 28.0369643012698,
23.5176540297538, 85.9823208879668, 69.1793641133218, 2.10460736024825,
2.47507980743031, 1.46820616137708, 8.50065507425538, 9.43358037894557,
15.0643556352927, 160.034358372113, 401.192221112903, 208.507212166668,
16.9012427928657, 70.1561153179486, 282.055943502233, 95.7582280566781,
20.4921115795782, 224.297864971227, 248.751359316637, 63.3008262529409,
202.381548954774, 160.240208598145, 89.2596850671307, 2.41266014760612,
2.24662537954621, 92.0812846530431, 64.0782727558696, 102.539355421441,
133.192603215476, 2.19161459176181, 2.71657087565203, 2.60259248593429,
9.95223133490391, 175.102558714441, 102.128569321102, 45.1350310564478,
60.7860248161304, 166.511239966959, 32.3622524770883, 38.1126859517567,
169.914906248272, 165.51479143087, 21.6290787183546, 154.863792200668,
224.650919723327, 172.786068029272, 41.6201741515014, 40.966552071361,
60.9998953906058, 4.00012706993277, 79.9578066806101, 183.917814759389,
103.086558986388, 6.96826209073272, 5.74403906883466, 5.4856515067938,
28.6736690417882, 238.403484773501, 231.70110714268, 126.348996131178,
61.4905557699149, 104.389974082626, 246.69389506543, 79.3069202652704,
24.117595869327, 48.4779179700019, 69.4483313003939, 127.606317513607,
78.8710394107804, 98.1528155665254, 128.061282053331, 3.19606373207204,
1.87066709355931, 30.3658567894746, 67.1163251638405, 32.6323314265454,
0.228514489291685, 2.59419308146204, 6.96463324671082, 31.4857435059086,
32.2152666766542, 219.54173457463, 100.503943180343, 71.9061154834901,
91.2830509779235, 155.03560958018, 98.391102232677, 27.4736388446992,
34.7344995015586, 85.1266347031687, 74.6245520597207, 24.0060703654787,
139.030754853487, 171.244448038257, 112.193936557097, 2.39299360914121,
19.4583131438491, 58.7443921590234, 14.0595780932243, 216.575414578798,
127.459683665046, 26.4468118443774, 15.7904069541748, 3.42889322639363,
18.9364897049987, 137.67183581274, 122.967226389734, 119.894430382828,
104.442282446888, 190.221376118256, 131.941577184786, 353.399746658368,
448.477151206068, 133.358287719838, 261.649707603195, 81.4006720251283,
343.002058701936, 163.91259720329, 197.994167334045, 7.30944634184061,
7.26330302571758, 32.2642168570983, 330.892281390864, 76.9551034586096,
16.5345940654105, 93.4833973060589, 2.44622725450917, 1.07860556492395,
1.93778399725422, 77.9570596409594, 94.0913633507909, 56.7576348472931,
409.330409688539, 51.8605115857434, 101.399915620214, 186.284262562234,
150.206902386026, 73.7756320461831, 29.7407716653824, 148.98703547435,
220.790713913921, 244.515597043242, 18.6165511888937, 9.90234122916165,
33.2562502415159, 156.414444934157, 13.9779124860658, 48.9122633130083,
170.372846084158, 157.591584502432, 41.3319249226387, 14.1513887931616,
6.44142678100409, 228.471931226893, 110.023055666621, 33.5052868559644,
173.194133068492, 32.3931156891464, 44.5888695638585, 57.8480536590698,
130.274156166872, 79.3730952009515, 46.5247317494093, 113.821030825373,
345.300064595988, 152.140595169695, 17.0421460982582, 6.84417297878845,
12.5696620896434, 36.6545290246341, 74.6452657675716, 217.14457420751,
165.496335573275, 22.3871316935213, 6.61421665015, 20.0410769731539,
23.7057126539467, 168.517094664878, 110.986727962072, 83.3281747496762,
7.35167504947444, 76.5528141698817, 20.6384141732761, 87.00310582216,
402.411224410847, 145.210679361704, 55.1206401339897, 446.103457643039,
95.317801637148, 198.306682822754, 88.7652010770343, 4.4779529467687,
55.2357786872407, 118.400174413319, 163.550512253059, 103.789889510405,
70.1485296476271, 44.9031868790507, 5.42285572703366, 23.2710355323781,
7.96212769344129, 37.560076557467, 210.670533114753, 104.544578996089,
438.121243591053, 56.4263114090557, 30.3428605030646, 102.704223497357,
88.0554172082872, 29.8261947342531, 21.3578133423672, 125.139532314134,
199.2412154636, 423.415414756748, 155.583267038193, 2.14393350563694,
2.77638044593597, 25.6375785864043, 176.271312482445, 188.095709294767,
162.049988299195, 28.6407159601821, 341.238744680548, 6.70617205440293,
0.685237342195351, 72.5497248411768, 411.366138460536, 14.570194300977,
35.8331305489166, 336.776755084208, 43.10843602833, 343.590748922672,
306.997839886018, 110.223009494854, 246.067728815614, 229.160642943454,
80.1932202086262, 223.436937319274, 7.33591020042729, 3.91210855110157,
0.301852508403949, 1.9831582085811, 100.093808999808, 136.307291596312,
56.0971664553408, 35.91142300096, 35.9638499452433, 2.71553679684771,
4.6621428025371, 140.593157625054, 274.807479865085, 221.786077846005,
61.483885141769, 20.3881339787884, 30.7758272593722, 96.0458882470437,
246.919746334924, 22.0033859138399, 198.28605047425, 103.814293419658,
46.5348985729046, 29.182847412964, 75.8648063336849, 13.8241139461049,
17.1286557911254, 88.1810161018373, 3.36013813866121, 69.639752829193,
677.723130883346, 41.0286431704323, 73.2159389655071, 2.97198914835809,
2.2955645498568, 77.4338889708046, 165.144080335453, 28.249849842644,
697.335948561217, 26.764915418294, 14.2190768683659, 91.7637857701146,
27.5440244171723, 16.8445374971489, 23.8566496302873, 0.981943140947041,
78.5997636834095, 162.138462101107, 44.1672067123073, 1.57379999898296,
4.52584675687701, 0.97658119252377, 0.477360675618112, 129.151103972441,
70.8307163818214, 275.971859788928, 127.881236082799, 1.170287338146,
0.866454371283992, 434.919703422169, 93.8451377376139, 207.10904118958,
46.8316256828644, 150.387134794503, 278.399451505872, 198.814569340003,
115.184928188408, 36.753170014185, 129.106822541989, 168.482550085438,
92.1323337766019, 250.394018594269, 37.1881210650176, 26.0619948566024,
2.1015758585879, 1.9337619910658, 2.5374401085012, 6.88084629859044,
240.364952743281, 23.3347433113824, 12.8301991435217, 104.664097883855,
11.9543330584122, 299.693093901171, 457.205452556256, 166.486441167246,
479.147896086039, 601.250116553193, 324.328442697521, 329.307886840488,
231.36130846456, 34.9248512789383, 159.724908476382, 310.307623928807,
665.667745992218, 440.34793375254, 47.0987434639045, 3.27176941323539,
9.70137304643561, 10.0607743796965, 3.08631438745061, 85.9070751173181,
114.552594829497, 56.4422079169895, 72.8598000828185, 49.5713482843566,
2.63222698246548, 278.660159682918, 374.155025716734, 614.896477070897,
84.7023024801914, 112.319999024275, 18.5987593461749, 82.9077768700278,
154.845742871174, 125.567795777075, 66.4893506450749, 126.741063662877,
82.2411837719443, 756.335890510717, 73.1151790189073, 3.5922959646701,
8.41573329417288, 4.41473763601453, 2.12491629471561, 57.10162180489,
51.9326111578832, 58.8698849597487, 64.1069702907545, 25.653260586019,
2.25452824419408, 133.78927961757, 341.7548293499, 14.113318950603,
64.7040755111393, 74.5271989167769, 407.725534601351, 309.316524308558,
20.2280966869265, 23.8884632436018, 20.0051667649045, 23.3715363806949,
21.8895053097727, 284.299801015909, 133.058636731235, 9.13435639512076,
8.93531290420054, 6.97575634977357, 38.5847487365879, 322.899303421944,
7.43662008052574, 31.3472739232612, 90.172402886085, 13.1780473878919,
11.8113106256799, 95.4868454357865, 111.151536039587, 62.3434590358668,
25.3913623754508, 152.028407407367, 140.924105429548, 110.376776160796,
21.9269046506022, 282.56001268775, 26.9210719144184, 88.3343050027196,
291.612562587322, 164.906755082596, 116.426543798048, 16.3087551310383,
6.52751999940019, 9.01631759743765, 16.1907026689521, 16.5315572289726,
1.88226712179479, 18.0388366074334, 48.3907627589146, 1.49068315465064,
9.44594654212787, 730.702774263893)), row.names = c(NA, -700L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000002641ef0>)
```
Your meters field might have NA values.
Hence adding na.rm = T to mean and sd functions will solve this.

Conversion from xts to zooreg time series removes date values

I hope you are having a nice day!
First, here's the dput of my data:
StreamsTempAveragextsMonthly <- structure(c(16.44, 15.7230769230769, 16.4484358974359, 16.2202307692308,
15.6025, 16.4114423076923, 16.7097115384615, 16.32125, 16.7625,
16.8855769230769, 17.864358974359, 18.4282692307692, 17.5625,
16.9068269230769, 17.1636730769231, 16.8279230769231, 17.09125,
17.8747916666667, 17.2025, 16.9225, 17, 17.75, 17.85, 17.3663461538462,
17.4355769230769, 16.8797115384615, 17.0658717948718, 17.2979230769231,
17.2128205128205, 17.78225, 17.48, 16.9457051282051, 18.535,
18.5871634615385, 17.7346153846154, 18.214188034188, 16.7875,
16.6706196581197, 17.103125, 17.3691346153846, 17.8264423076923,
16.4920192307692, 16.8905128205128, 16.9484615384615, 17.350641025641,
17.9035096153846, 18.1136363636364, 18.0958, 18.125, 17.3089871794872,
17.3978205128205, 17.8078985507246, 17.6580384615385, 17.7281643356643,
17.3222222222222, 17.6125555555556, 18.4708333333333, 18.9261363636364,
18.3610714285714, 18.2842857142857, 17.5, 18.9776818181818, 18.2313068181818,
18.5352272727273, 18.2314393939394, 16.8462121212121, 17.5330452568202,
16.5851136363636, 18.3415151515152, 19.0620454545455, 17.5744047619048,
16.7176308539945, 16.6407407407407, 16.7227272727273, 16.4184848484848,
17.5290598290598, 17.1817965367965, 16.4547619047619, 15.9484848484848,
15.6835902503294, 16.0388865398168, 17.0166666666667, 17.5905555555556,
16.4290088383838, 16.2997452016069, 16.1557023172906, 16.7221212121212,
16.857196969697, 17.3215277777778, 16.5125, 16.0752525252525,
15.5828743589744, 16.6117845117845, 17.9668930686172, 17.5651666666667,
16.3064442224442, 16.2763888888889, 17.650505050505, 16.8803872053872,
17.3298611111111, 17.3772443181818, 17.0242424242424, 16.8111111111111,
16.8055555555556, 17.232601010101, 17.8261363636364, 17.3753787878788,
16.5777272727273, 15.8893939393939, 16.9522435897436, 16.5363636363636,
16.6034090909091, 17.2667929292929, 18.0511363636364, 17.5534090909091,
16.4551136363636, 17.23125, 17.5136363636364, 17.8482954545455,
16.9869318181818, 16.4244318181818, 17.125, 16.8022727272727,
17.4343006993007, 17.2990909090909, 17.1961538461538, 17.009375,
16.9174747474747, 17.2710227272727, 17.6715909090909, 17.4818181818182,
15.9962121212121, 16.5631818181818, 16.6078349282297, 17.1787878787879,
17.0653409090909, 17.4431818181818, 17.4880681818182, 15.0918803418803,
16.7741339869281, 17.4332792207792, 17.7990981240981, 18.0886363636364,
16.8297619047619, 16.6851973684211, 17.2556818181818, 17.0295454545455,
16.8240909090909, 16.7538770053476, 16.2579545454545, 15.9655757575758,
17.1280753968254, 17.7519191919192, 17.4414335664336, 17.4557954545455,
16.1622115384615, 16.8327651515152, 16.9667613636364, 16.672798573975,
16.9659090909091, 17.0818181818182, 17.1822027972028, 17.8164884135472,
17.4545454545455, 16.7771212121212, 17.8238636363636, 17.7005244755245,
16.7160984848485, 16.341754756871, 16.6440025252525, 16.9829545454545,
16.6630555555556, 19.1123467230444, 19.3261363636364, 17.7603021978022,
18.8059107142857, 18.2065559440559, 18.25, 16.965, 18.0582115800866,
17.2105637254902, 18.6762012987013, 18.4136363636364, 18.2600378787879,
18.0003496503497, 17.9288825757576, 17.959375, 18.5164772727273,
18.4772727272727, 18.2170454545455, 18.90625, 16.7813725490196,
17.2660256410256, 17.0635542929293, 17.2422902097902, 17.1887820512821,
16.1727941176471, 16.0860294117647, 18.3868181818182, 18.0298076923077,
18.7031536293164, 18.3659090909091, 18.625, 16.7256818181818,
17.2232600732601, 18.3319639192886, 17.9238636363636, 17.9799422799423,
16.5837121212121, 16.100952540107, 16.9387019230769, 17.3568181818182,
17.1753146853147, 17.1159090909091, 18.1145833333333, 16.8415170940171,
16.9133597285068, 17.6888034188034, 17.1259793447293, 17.384655448718,
16.7841346153846, 16.2973468660969, 16.1201495726496, 17.5849893162393,
18.2456018518519, 18.0254807692308, 17.4643233618234, 17.1731532356532,
17.5413283475783, 17.7156695156695, 17.5869255189255, 17.6197802197802,
17.1997996794872, 17.6756588319088, 18.048433048433, 18.5746082621083,
19.1991987179487, 18.6432081807082, 16.8430288461538, 17.4325367647059,
17.314707977208, 17.5010576923077, 17.9123219373219, 17.4, 17.5033333333333,
17.8433333333333, 18.9353846153846, 18.6049679487179, 17.1923076923077,
17.4415196078431, 17.6505057932264, 17.2357352941176, 17.9955326484661,
17.5959523809524, 17.5952941176471, 16.9622171945701, 18.2641666666667,
17.3842124183007, 18.8371301247772, 18.2442091503268, 17.8693572984749,
17.4261783559578, 17.8408721670486, 18.0430294117647, 17.5234615384615,
18.7757894736842, 18.0545833333333, 18.3861029411765, 17.2286764705882,
19.1830882352941, 18.9825490196078, 19.399375, 18.8928921568627,
17.379375, 17.8381315789474, 18.9048611111111, 18.32625, 18.5,
17.1804924242424, 17.165, 19.0856617647059, 18.6882066993464,
18.949358974359, 18.2374264705882, 17.9036554621849, 17.7655882352941,
18.0464795008913, 19.0757010582011, 18.0125, 17.0503365384615,
17.4858707264957, 18.0303062678063, 19.0284455128205, 18.0894444444444,
18.4038461538462), .Dim = c(295L, 1L), .Dimnames = list(NULL,
"AverageTemp"), index = structure(c(741398400, 742003200,
746668800, 749347200, 751766400, 754185600, 757209600, 759628800,
762048000, 765072000, 767491200, 770256000, 772243200, 775353600,
778118400, 780710400, 783216000, 785635200, 788054400, 791424000,
793238400, 796521600, 798940800, 801360000, 803865600, 807148800,
809740800, 812246400, 814406400, 817603200, 819244800, 823046400,
824860800, 827884800, 830131200, 833328000, 835315200, 838771200,
841190400, 843609600, 846633600, 849052800, 851904000, 854496000,
857088000, 859420800, 861321600, 864864000, 867110400, 870220800,
872640000, 875491200, 878083200, 880502400, 883353600, 885945600,
888364800, 890784000, 893808000, 895190400, 900115200, 904521600,
907027200, 909532800, 911952000, 913161600, 916790400, 919555200,
921715200, 925257600, 928108800, 930700800, 933292800, 935712000,
938131200, 941155200, 942969600, 945734400, 949017600, 951436800,
953856000, 956016000, 958694400, 962064000, 964483200, 966988800,
970185600, 972604800, 974937600, 977443200, 980208000, 982800000,
985910400, 987984000, 991267200, 993772800, 994723200, 999216000,
1001548800, 1004054400, 1006473600, 1008892800, 1012435200, 1014854400,
1017446400, 1019260800, 1022803200, 1025308800, 1027728000, 1030147200,
1032566400, 1034985600, 1038614400, 1039824000, 1043452800, 1045872000,
1048291200, 1050624000, 1053129600, 1056758400, 1059177600, 1060387200,
1064016000, 1067558400, 1070064000, 1071273600, 1074902400, 1077580800,
1079740800, 1083283200, 1084579200, 1087948800, 1091232000, 1093651200,
1096070400, 1098489600, 1100908800, 1103328000, 1106352000, 1108771200,
1111190400, 1114819200, 1116028800, 1117843200, 1122076800, 1124496000,
1128038400, 1130544000, 1133222400, 1135209600, 1138665600, 1140825600,
1143244800, 1145664000, 1148083200, 1151107200, 1154131200, 1156550400,
1158969600, 1161388800, 1163808000, 1166227200, 1169251200, 1171670400,
1175299200, 1177718400, 1180051200, 1183161600, 1185580800, 1.188e+09,
1190419200, 1191628800, 1196294400, 1197676800, 1200614400, 1203120000,
1206748800, 1209168000, 1210377600, 1214006400, 1216339200, 1220054400,
1222473600, 1224892800, 1227312000, 1229731200, 1232755200, 1235174400,
1237507200, 1240012800, 1242432000, 1248480000, 1250553600, 1253318400,
1256947200, 1259280000, 1260576000, 1262995200, 1266710400, 1269043200,
1272585600, 1275091200, 1275350400, 1280534400, 1282953600, 1285372800,
1287792000, 1290211200, 1292630400, 1295568000, 1298073600, 1299283200,
1304035200, 1306454400, 1307664000, 1311984000, 1314316800, 1316822400,
1319241600, 1322611200, 1324080000, 1327104000, 1330473600, 1333152000,
1335571200, 1338249600, 1340323200, 1342828800, 1346371200, 1348790400,
1351209600, 1353628800, 1356134400, 1359158400, 1360368000, 1363910400,
1366416000, 1369699200, 1375228800, 1377648000, 1379635200, 1382486400,
1384992000, 1386892800, 1391126400, 1393545600, 1395360000, 1398297600,
1401235200, 1406246400, 1408579200, 1412035200, 1414713600, 1417046400,
1418428800, 1421712000, 1424217600, 1426723200, 1430352000, 1432944000,
1435276800, 1438214400, 1440979200, 1443398400, 1444867200, 1447372800,
1449619200, 1453334400, 1456704000, 1459382400, 1461715200, 1463011200,
1466726400, 1469059200, 1472601600, 1475020800, 1477440000, 1479168000,
1481155200, 1484179200, 1487721600, 1489968000, 1491436800, 1495065600,
1498780800, 1501027200, 1503446400, 1508544000, 1510963200, 1513296000,
1516233600, 1519171200, 1520812800, 1523318400, 1525219200), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo"))
It has this structure:
An ‘xts’ object on 1993-06-30/2018-05-02 containing:
Data: num [1:295, 1] 16.4 15.7 16.4 16.2 15.6 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "AverageTemp"
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
Notice how the last date is 2018-05-02? Now, the problem is when I convert my xts object into a zooreg object. I used this code:
StreamsTempzooreg <- zooreg(StreamsTempAveragextsMonthly, start = c(1993,6), end = c(2018,5), frequency = 12)
It has this structure:
‘zooreg’ series from Jun 1993 to Dec 2017
Data: num [1:295, 1] 16.4 15.7 16.4 16.2 15.6 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:295] "1993-06-30" "1993-07-07" "1993-08-30" "1993-09-30" ...
..$ : chr "AverageTemp"
Index: 'yearmon' num [1:295] Jun 1993 Jul 1993 Aug 1993 Sep 1993 ...
Frequency: 12
It changes the last date to December 2017. Why is this happening? How can I fix it so that the original end date is May 2018?
Any help would be greatly appreciated. Thank you.
You want as.zooreg, not zooreg. zooreg is used to construct a zooreg object from its data and index whereas as.zooreg is used to convert other objects to zooreg class. The first argument of zooreg specifies the data portion and the other arguments specify the index portion. The first argument of zooreg should be a numeric vector or matrix but since an xts object was given instead it took the data portion of it ignoring the time index.
as.zooreg can be used to convert from xts class to zooreg class. The first line below does the converstion keeping the Date class index and then the next line converts the index to year/month using the yearmon class.
zr <- as.zooreg(StreamsTempAveragextsMonthly)
zr <- aggregate(zr, as.yearmon, c) ##
range(index(zr))
## [1] "Jun 1993" "May 2018"
The line marked ## could alternatley be written:
index(zr) <- as.yearmon(index(zr))

Error calculating the distance between values from a data.frame

I'm trying to calculate the distance between different points that are a subset from a data.frame.
The method I try to use is distHaversine from library geosphere.
I have my data in var st and try using the function such as:
distHaversine(st[1,c(3,2)],st[2,c(3,2)])
Where each row in st is an entry of a new place, column 3 is longitude and column 2 is latitude.
Using the function in this manner gives me the following error:
Error in .pointsToMatrix(p1) * toRad :
non-numeric argument to binary operator
Reading the error something like:
distHaversine(as.double(st[1,c(3,2)]),as.double(st[2,c(3,2)]))
sounds like it should solve the problem but that just gives med another error:
Error in .pointsToMatrix(p1) : latitude > 90
If I manually add the lat and long in to the function such as:
distHaversine(c(12.6959,60.3097),c(12.6959,60.3097))
It works and returns 0.
The output of st[1,c(3,2)] and st[2,c(3,2)] is the following:
longitude latitude
1 12.6959 60.3097
longitude latitude
2 12.6959 60.3097
Output of dput(st[1:2, 2:3]):
structure(list(latitude = structure(c(422L, 422L), .Label = c(" latitude",
"55.3376", "55.3836", "55.3838", "55.384", "55.3872", "55.4114",
"55.441", "55.47411", "55.4889", "55.4907", "55.5", "55.5231",
"55.5449", "55.597", "55.6048", "55.6049", "55.6093", "55.6333",
"55.6402", "55.65", "55.6646", "55.693", "55.7", "55.7142", "55.7833",
"55.8633", "55.8666", "55.8798", "55.9", "55.9033", "55.9081",
"55.9245", "55.9557", "55.9833", "56.0131", "56.0133", "56.02391",
"56.0304", "56.0419", "56.0431", "56.05", "56.0666", "56.0699",
"56.07", "56.0752", "56.1333", "56.1498", "56.1813", "56.1957",
"56.1977", "56.2", "56.2182", "56.2287", "56.2451", "56.2619",
"56.2799", "56.2979", "56.3012", "56.3666", "56.38", "56.40404",
"56.4495", "56.45", "56.4595", "56.4782", "56.5124", "56.5177",
"56.5256", "56.5376", "56.5546", "56.5684", "56.601", "56.6166",
"56.6333", "56.66694", "56.67", "56.6737", "56.675", "56.6784",
"56.6833", "56.7154", "56.727", "56.7429", "56.7486", "56.7849",
"56.823", "56.8311", "56.8403", "56.8427", "56.8464", "56.8526",
"56.8666", "56.8865", "56.8994", "56.9213", "56.9222", "56.9252",
"56.9303", "56.931", "56.9496", "56.9628", "56.9835", "57.0333",
"57.0666", "57.0735", "57.1084", "57.1088", "57.1136", "57.11667458",
"57.121", "57.1461", "57.1462", "57.1484", "57.1834", "57.1978",
"57.2001", "57.2166", "57.2323", "57.2438", "57.253", "57.2577",
"57.2674", "57.2833", "57.2847", "57.288", "57.2893", "57.3025",
"57.304", "57.3672", "57.3689", "57.3848", "57.3908", "57.3915",
"57.4034", "57.4042", "57.4166", "57.4208", "57.4413", "57.45",
"57.4666", "57.4833", "57.485", "57.4983", "57.4998", "57.54361",
"57.5571", "57.5653", "57.5666", "57.596", "57.6295", "57.6324",
"57.636", "57.642", "57.6431", "57.6468", "57.661", "57.6614",
"57.6678", "57.6687", "57.6898", "57.7071", "57.7084", "57.7166",
"57.7213", "57.722", "57.7242", "57.75", "57.7514", "57.7611",
"57.7666", "57.7786", "57.7848", "57.7865", "57.8052", "57.8232",
"57.8305", "57.8425", "57.8429", "57.85", "57.8701", "57.8748",
"57.8863", "57.8887", "57.8987", "57.9166", "57.9167", "57.93309233",
"57.936", "57.9666", "58.0067", "58.0166", "58.0201", "58.03",
"58.0333", "58.0389", "58.0492", "58.0582", "58.0718", "58.0729",
"58.0788", "58.0794", "58.0908", "58.094", "58.0944", "58.0951",
"58.1425", "58.1584", "58.1589", "58.19969", "58.19981831", "58.2",
"58.2148", "58.2159", "58.2212", "58.2333", "58.2502", "58.2505",
"58.2662", "58.288", "58.2886", "58.3", "58.3141", "58.3221",
"58.3331352", "58.3333", "58.3339", "58.3476", "58.355", "58.3552",
"58.3571", "58.37", "58.38", "58.3833", "58.3928", "58.3935",
"58.3943", "58.3949", "58.4", "58.4004", "58.4072", "58.4166",
"58.4274", "58.4358", "58.4397", "58.4447", "58.4529", "58.4666",
"58.472", "58.5166", "58.5185", "58.5289", "58.5491", "58.55",
"58.5586", "58.5707", "58.5719", "58.5763", "58.5833", "58.5842",
"58.5929", "58.60033", "58.606", "58.6068", "58.6082", "58.6277",
"58.6336", "58.6493", "58.65", "58.66076", "58.6777", "58.6894",
"58.6998", "58.7104", "58.7136", "58.7165", "58.7333", "58.7421",
"58.7433", "58.7588", "58.7833", "58.78468", "58.7904", "58.7906",
"58.7936", "58.8", "58.8055", "58.8063", "58.8333", "58.85",
"58.8509", "58.8772", "58.88", "58.8925", "58.898", "58.9", "58.904",
"58.9333", "58.9334", "58.9502", "58.9671", "58.9733", "58.9817",
"59.0185", "59.0297", "59.05", "59.0542", "59.0561", "59.0666",
"59.0667", "59.0688", "59.08304351", "59.0833", "59.0966", "59.1166",
"59.1412", "59.1469", "59.1547", "59.1789", "59.182", "59.1833",
"59.2032", "59.2184", "59.2217", "59.2289", "59.2333", "59.2334",
"59.2361", "59.2448", "59.2511", "59.2661", "59.2671", "59.2782",
"59.2833", "59.2891", "59.2901", "59.2996", "59.3", "59.3107",
"59.3226", "59.342", "59.3421", "59.35", "59.3537", "59.3582",
"59.361", "59.3619", "59.3658", "59.3832", "59.3833", "59.384",
"59.3867", "59.3868", "59.4", "59.402", "59.4166", "59.4428",
"59.4445", "59.4446", "59.5052", "59.5171", "59.5345", "59.55",
"59.5737", "59.5833", "59.5976", "59.6072", "59.6112", "59.6166",
"59.6269", "59.6396", "59.6557", "59.6575", "59.6616", "59.6658",
"59.6747", "59.6833", "59.7166", "59.7497", "59.75", "59.7506",
"59.7534", "59.8085", "59.8139", "59.8321", "59.8333", "59.85",
"59.8524", "59.8585", "59.8586", "59.8633", "59.8644", "59.8707",
"59.8709", "59.88", "59.8953", "59.9045", "59.9098", "59.9268",
"59.9445", "59.95", "60.0666", "60.0991", "60.1075", "60.1197",
"60.12295", "60.1419", "60.1426", "60.1514", "60.1538", "60.1595",
"60.169", "60.175", "60.2", "60.2026", "60.2357", "60.2363",
"60.2688", "60.2761", "60.2788", "60.3", "60.3097", "60.3328",
"60.3333", "60.3548", "60.3758", "60.3822", "60.4294", "60.4393",
"60.4551", "60.4889", "60.5", "60.502", "60.5074", "60.5262",
"60.5333", "60.542", "60.55", "60.6046", "60.6103", "60.619",
"60.651", "60.6537", "60.6717", "60.6773", "60.7004", "60.7166",
"60.7256", "60.7304", "60.7547", "60.75686", "60.8321", "60.88",
"60.8846", "60.9066", "60.923", "60.9607", "60.962", "60.9645",
"60.9676", "61.0025", "61.0442", "61.0666", "61.1269", "61.15829",
"61.1666", "61.1691", "61.1766", "61.2542", "61.2555", "61.2691",
"61.2797", "61.3198", "61.3613", "61.3784", "61.3882", "61.395",
"61.4446", "61.5", "61.55", "61.6022", "61.6239", "61.6408",
"61.6577", "61.6606", "61.6912", "61.7027", "61.7033", "61.7111",
"61.7167", "61.7854", "61.8271", "61.8333", "61.85", "61.8526",
"61.8547", "61.8691", "61.8892", "62.0166", "62.0291", "62.0424",
"62.0471", "62.0943", "62.0991", "62.1132", "62.1299", "62.1809",
"62.2207", "62.2314", "62.26317", "62.3809", "62.4066", "62.4112",
"62.4172", "62.43064", "62.4502", "62.4943", "62.4961", "62.5",
"62.5165", "62.5166", "62.5246", "62.5335", "62.5493", "62.5772",
"62.6183", "62.628", "62.6431", "62.65", "62.72909", "62.7503",
"62.7512", "62.7557", "62.7594", "62.76291", "62.8016", "62.8108",
"62.8166", "62.8485", "62.9948", "63.0335", "63.044", "63.0447",
"63.05", "63.0502", "63.0509", "63.0796", "63.1251", "63.1333",
"63.1466", "63.1521", "63.1618", "63.16629", "63.1667", "63.1736",
"63.1864", "63.1885", "63.1886", "63.1947", "63.1974", "63.2443",
"63.2631", "63.2831", "63.3028", "63.30543", "63.3158", "63.3166",
"63.3193", "63.3266", "63.3269", "63.37375", "63.3806", "63.4",
"63.4119", "63.4304", "63.4359", "63.4608", "63.5739", "63.5949",
"63.595", "63.6198", "63.6513", "63.6574", "63.677", "63.6849",
"63.6968", "63.7", "63.73", "63.7606", "63.7695", "63.7788",
"63.7947", "63.8082", "63.8129", "63.8302", "63.8395", "63.8548",
"63.8683", "63.9187", "63.925", "63.9515", "63.9687", "63.9812",
"63.9967", "64.0432", "64.0753", "64.0763", "64.0885", "64.1236",
"64.159", "64.1661", "64.1973", "64.2", "64.2182", "64.2684",
"64.2734", "64.3636", "64.4309", "64.43583", "64.4502", "64.4542",
"64.4571", "64.4806", "64.4812", "64.503", "64.5057", "64.5059",
"64.5492", "64.5666", "64.5669", "64.5809", "64.5871", "64.6244",
"64.6389", "64.7166", "64.7306", "64.73431", "64.75", "64.7507",
"64.7584", "64.7627", "64.8666", "64.9093", "64.9112", "64.9263",
"64.95", "64.96782", "65.0074", "65.0106", "65.0251", "65.0333",
"65.035", "65.0464", "65.062", "65.0706", "65.0942", "65.1049",
"65.1089", "65.1317", "65.1522", "65.1808", "65.23", "65.2365",
"65.2391", "65.25", "65.278", "65.2984", "65.3131", "65.3156",
"65.3235", "65.328", "65.33", "65.3373", "65.4166", "65.5085",
"65.53", "65.5354", "65.5374", "65.5434", "65.5592", "65.5762",
"65.5953", "65.62", "65.6702", "65.6772", "65.68", "65.6935",
"65.6982", "65.7", "65.7166", "65.7306", "65.75272", "65.7666",
"65.7832", "65.7968", "65.8", "65.8077", "65.8151", "65.8211",
"65.8249", "65.8333", "65.8367", "65.85", "65.8769", "65.8801",
"65.8849", "65.8952", "65.9166", "65.9446", "65.9744", "65.9867",
"66.0456", "66.0833", "66.1347", "66.263", "66.2801", "66.2833",
"66.3002", "66.3006", "66.32417", "66.3824", "66.3855", "66.3862",
"66.3888", "66.48", "66.48603", "66.499", "66.50502", "66.5333",
"66.5343", "66.5759", "66.6135", "66.6235", "66.6798", "66.6815",
"66.6932", "66.7265", "66.7374", "66.7529", "66.7586", "66.761",
"66.7666", "66.8107", "66.8542", "66.8876", "66.8888", "67.0919",
"67.1261", "67.1358", "67.1421", "67.1429", "67.1498", "67.1705",
"67.2049", "67.2101", "67.2564", "67.3094", "67.3376", "67.3833",
"67.3956", "67.4083", "67.4964", "67.5009", "67.6501", "67.6943",
"67.7261", "67.7262", "67.7315", "67.7615", "67.827", "67.8488",
"67.85", "67.8911", "67.9113", "67.9443", "68.04398", "68.0502",
"68.0745", "68.2201", "68.2833", "68.3555", "68.3557", "68.4217",
"68.4218", "68.4284", "68.4316", "68.4324", "68.4421", "68.4432",
"68.6777", "68.6779"), class = "factor"), longitude = structure(c(81L,
81L), .Label = c(" longitude", "11", "11.0062", "11.0333", "11.0685",
"11.2", "11.2166", "11.2276", "11.3333", "11.3348", "11.41775",
"11.5332", "11.5572", "11.5724", "11.6015", "11.6077", "11.6352",
"11.6528", "11.7833", "11.8075", "11.8824", "11.9038", "11.9064",
"11.92", "11.9321", "11.9333", "11.9541", "11.97", "11.9746",
"11.9858", "11.9939", "12.038", "12.0387", "12.0542", "12.078",
"12.1009", "12.1032", "12.107", "12.1246", "12.1253", "12.1454",
"12.15406", "12.1563", "12.1581", "12.1761", "12.1833", "12.1997",
"12.1999", "12.2223", "12.2689", "12.2741", "12.2775", "12.2963",
"12.3024", "12.3153", "12.3166", "12.3292", "12.3579", "12.3616",
"12.394", "12.4129", "12.41302", "12.454", "12.4798", "12.5438",
"12.547", "12.55", "12.5514", "12.5521", "12.5843", "12.591",
"12.6043", "12.6381", "12.647", "12.6491", "12.6702", "12.6739",
"12.6775", "12.6906", "12.6928", "12.6959", "12.6969", "12.7",
"12.7042", "12.7074", "12.7075", "12.7166", "12.7264", "12.7653",
"12.77226", "12.7758", "12.8129", "12.8166", "12.819", "12.8203",
"12.8226", "12.8332", "12.8333", "12.8441", "12.8538", "12.8542",
"12.8568", "12.8575", "12.8705", "12.8709", "12.9242", "12.9247",
"12.9333", "12.9359", "12.9414", "12.94361", "12.9493", "12.9834",
"12.9841", "12.9843", "12.9908", "13.0164", "13.0166", "13.0338",
"13.0406", "13.0595", "13.0607", "13.0625", "13.0666", "13.0667",
"13.0668", "13.0731", "13.0734", "13.0746", "13.0817", "13.0833",
"13.1", "13.1109", "13.11383", "13.1166", "13.1174", "13.1193",
"13.1238", "13.1247", "13.1296", "13.1554", "13.1589", "13.16067",
"13.1666", "13.1668", "13.18054263", "13.1865", "13.2152", "13.2166",
"13.229", "13.2318", "13.2369", "13.25", "13.2517", "13.2534",
"13.2593", "13.2666", "13.2833", "13.2841", "13.3157", "13.33",
"13.3374", "13.3508", "13.3563", "13.3608", "13.3787", "13.3826",
"13.3957", "13.3995", "13.4001", "13.4083", "13.4407", "13.4455",
"13.45", "13.4666", "13.4728", "13.4833", "13.5037", "13.5168",
"13.52", "13.5254", "13.5324", "13.5335", "13.5397", "13.55",
"13.5609", "13.6115", "13.6191", "13.6333", "13.6488", "13.6671",
"13.66833", "13.6689", "13.6748", "13.7058", "13.71", "13.7166",
"13.7251", "13.75", "13.7529", "13.78495", "13.7975", "13.7995",
"13.8019", "13.823", "13.8278", "13.8436", "13.8456", "13.85",
"13.87671", "13.88040497", "13.8822", "13.8833", "13.8907", "13.8985",
"13.9333", "13.9432", "13.948", "13.98", "13.986", "14.0375",
"14.0383", "14.0476", "14.0733", "14.0842", "14.0921", "14.1",
"14.1019", "14.10272", "14.1166", "14.1265", "14.1273", "14.1297",
"14.1455", "14.1465", "14.1571", "14.1666", "14.17", "14.1831",
"14.1833", "14.1847", "14.1859", "14.1947", "14.2", "14.2028",
"14.2238", "14.2354", "14.25", "14.2595", "14.2666", "14.2833",
"14.2874", "14.2896", "14.3166", "14.3172", "14.3188", "14.3508",
"14.3578", "14.3678", "14.3692", "14.3786", "14.3903", "14.3951",
"14.4", "14.401", "14.4086", "14.41", "14.4277", "14.4317", "14.4323",
"14.45", "14.451", "14.4666", "14.4672", "14.4693", "14.4863",
"14.4898", "14.507", "14.5085", "14.5126", "14.5535", "14.56389527",
"14.5722", "14.5874", "14.6063", "14.6079", "14.6333", "14.6701",
"14.6786", "14.6895", "14.6931", "14.7", "14.7166", "14.7333",
"14.7342", "14.7571", "14.7908", "14.8015", "14.8024", "14.8176",
"14.826", "14.8324", "14.8488", "14.85", "14.8517", "14.8744",
"14.8833", "14.8881", "14.891", "14.8913", "14.8986", "14.8987",
"14.9", "14.9019", "14.95", "14.9524", "14.9585", "14.97", "14.9853",
"14.986", "14.9943", "14.9948", "15.0146", "15.0167", "15.0206",
"15.03", "15.0455", "15.05", "15.0645", "15.0666", "15.0686",
"15.0854", "15.0876", "15.0929", "15.0984", "15.1045", "15.1145",
"15.12262", "15.1285", "15.1404", "15.1574", "15.164", "15.1694",
"15.2", "15.2184", "15.231", "15.2352", "15.2359", "15.2365",
"15.237", "15.25", "15.2544", "15.2553", "15.2686", "15.2742",
"15.2747", "15.2833", "15.2854", "15.3263", "15.331", "15.3666",
"15.3668", "15.3772", "15.37907", "15.3987", "15.418", "15.4324",
"15.4372", "15.45", "15.4606", "15.4655", "15.47", "15.5", "15.5079",
"15.5192", "15.5327", "15.5331", "15.5333", "15.5423", "15.55",
"15.5555", "15.5561", "15.5725", "15.5857", "15.5894", "15.5921",
"15.6235", "15.6332", "15.6333", "15.6603", "15.6666", "15.67093",
"15.6762", "15.6833", "15.6858", "15.7", "15.7077", "15.7175",
"15.7431", "15.7486", "15.7921", "15.8037", "15.8081", "15.8215",
"15.8232", "15.8245", "15.8307", "15.8333", "15.8352", "15.8518",
"15.8652", "15.9166", "15.9333", "15.9666", "15.9667", "15.9681",
"15.98018143", "15.9833", "15.9883", "15.9909", "16.0017", "16.0353",
"16.0581", "16.0833", "16.1088", "16.1137", "16.1166", "16.1212",
"16.1333", "16.15", "16.15439", "16.1667", "16.1689", "16.1733",
"16.1747", "16.1833", "16.1846", "16.2084", "16.2102", "16.2127",
"16.21345182", "16.2149", "16.2166", "16.2283", "16.2383", "16.2388",
"16.2662", "16.2774", "16.283", "16.2838", "16.2922", "16.2944",
"16.3062", "16.3103", "16.3125", "16.3141", "16.35", "16.3511",
"16.3666", "16.3667", "16.3709", "16.39", "16.3975", "16.401",
"16.4036", "16.4142", "16.4145", "16.4149", "16.41734", "16.4199",
"16.4295", "16.4311", "16.4549", "16.4585", "16.4609", "16.4631",
"16.4647", "16.4648", "16.4683", "16.5333", "16.5358", "16.5393",
"16.5418", "16.55", "16.5505", "16.5536", "16.5682", "16.5812",
"16.5828", "16.5833", "16.6326", "16.6607", "16.6799", "16.68",
"16.6833", "16.6875", "16.6919", "16.7114", "16.7337", "16.7387",
"16.7462", "16.7896", "16.8157", "16.8421", "16.8536", "16.8723",
"16.9166", "16.9168", "16.9531", "16.977", "16.9959", "17.0042",
"17.0095", "17.0106", "17.0262", "17.044", "17.05", "17.0605",
"17.0794", "17.086", "17.0879", "17.09442", "17.0967", "17.0983",
"17.0986", "17.1008", "17.1121", "17.1223", "17.1536", "17.1641",
"17.1693", "17.1748", "17.2", "17.2165", "17.2342", "17.2664",
"17.2802", "17.2854", "17.3188", "17.3333", "17.3409", "17.3447",
"17.3494", "17.3574", "17.40337", "17.4333", "17.4401", "17.441",
"17.4498", "17.4666", "17.4704", "17.4711", "17.4718", "17.5166",
"17.5225", "17.5244", "17.525", "17.5612", "17.5666", "17.5935",
"17.6186", "17.6252", "17.6253", "17.6333", "17.6469", "17.65",
"17.65431", "17.6623", "17.6666", "17.6677", "17.7034", "17.7085",
"17.7166", "17.7426", "17.7478", "17.75", "17.7658", "17.86",
"17.8671", "17.8716", "17.8833", "17.8974", "17.904", "17.9049",
"17.9072", "17.9078", "17.9125", "17.9167", "17.93", "17.9462",
"17.9471", "17.9513", "17.9545", "17.9623", "17.9972", "18.0159",
"18.0179", "18.0234", "18.0575", "18.0577", "18.0592", "18.0618",
"18.0929", "18.1", "18.1166", "18.1184", "18.1302", "18.1471",
"18.1541", "18.1666", "18.1698", "18.1719", "18.1794", "18.2222",
"18.2272", "18.2555", "18.2669", "18.2718", "18.2839", "18.2891",
"18.2943", "18.3061", "18.3308", "18.3428", "18.3656", "18.3725",
"18.3766", "18.3835", "18.3911", "18.419", "18.42533", "18.4333",
"18.4493", "18.4639", "18.5297", "18.5318", "18.55", "18.5502",
"18.5974", "18.6068", "18.6281", "18.6348", "18.6587", "18.6833",
"18.7047", "18.7091", "18.7156", "18.7169", "18.7296", "18.7431",
"18.7436", "18.8146", "18.8206", "18.8211", "18.8417", "18.8666",
"18.9011", "18.9174", "18.9179", "18.9227", "18.9236", "18.9505",
"18.9568", "18.9797", "18.9833", "18.9871", "19.0194", "19.0202",
"19.0233", "19.0532", "19.0611", "19.0929", "19.1298", "19.1592",
"19.1833", "19.197", "19.1975", "19.2", "19.2682", "19.3168",
"19.3782", "19.4087", "19.4746", "19.4833", "19.4875", "19.4976",
"19.5", "19.5058", "19.5059", "19.5666", "19.6314", "19.6673",
"19.67495", "19.6768", "19.6983", "19.6986", "19.7123", "19.7243",
"19.8316", "19.8392", "19.9", "20.02", "20.0993", "20.131", "20.1315",
"20.1543", "20.2124", "20.2333", "20.2397", "20.2747", "20.2901",
"20.2918", "20.3387", "20.3605", "20.4055", "20.424", "20.6333",
"20.6455", "20.6586", "20.6686", "20.7565", "20.7652", "20.7891",
"20.8144", "20.8692", "20.9", "20.9026", "20.9281", "20.9478",
"20.9666", "20.97", "20.9858", "21.0537", "21.0558", "21.0662",
"21.0717", "21.0846", "21.133", "21.15", "21.2263", "21.2666",
"21.27997", "21.3152", "21.4907", "21.52", "21.5274", "21.5277",
"21.55", "21.5655", "21.5666", "21.5787", "21.5791", "21.6098",
"21.6167", "21.6317", "21.6335", "21.6396", "21.75496", "21.76498",
"21.85", "21.9814", "22.109", "22.116", "22.1193", "22.1307",
"22.2178", "22.2334", "22.336", "22.3518", "22.3666", "22.3753",
"22.3961", "22.4488", "22.4502", "22.75", "22.8266", "22.8472",
"22.85", "22.9001", "23.0571", "23.0924", "23.1005", "23.2",
"23.3928", "23.3952", "23.4441", "23.4666", "23.468", "23.622",
"24.113", "24.1162"), class = "factor")), row.names = 1:2, class = "data.frame")
As #IceCreamToucan said, the issue is that your latitude and longitude are factor variables. To fix that, you may use
df$latitude <- as.numeric(as.character(df$latitude))
df$longitude <- as.numeric(as.character(df$longitude))
distHaversine(df[1,], df[2,])
# [1] 0
What's also important is why this happened. In particular, we have
levels(df$latitude)
# [1] " latitude" "55.3376" ...
So, there must be a row with " latitude" as a value in the latitude column. As a result, when loading the data this variable became a factor. Similarly,
levels(df$longitude)
# [1] " longitude" "11" ...
I'll take a wild guess that st is a matrix that includes textual data, therefore all data is coerced as char. try rebuilding your data object as data.frame (which allows multiple data types) and see if problem persists.
> st <- data.frame(name=c("A","B"), latitude = 12.6959, longitude = 60.3097)
> st
name latitude longitude
1 A 12.6959 60.3097
2 B 12.6959 60.3097
> geosphere::distHaversine(st[1,c(3,2)],st[2,c(3,2)])
[1] 0
except for that I agree with everything #julius-vainora has written.

trouble adding geom_vline to ggplot2

I have a large time series data set, which I've used xts to summarize in 30 second periods. Not sure how to make this set easily reproducible but it looks like this
> str(taonedf)
'data.frame': 480 obs. of 2 variables:
$ time : POSIXct, format: "2013-01-06 13:00:29" "2013-01-06 13:00:59" "2013-01-06 13:01:29" ...
$ count: int 20763 12030 22188 12183 21112 11628 21543 12609 20095 12992 ...
> head(taonedf)
time count
1 2013-01-06 13:00:29 20763
2 2013-01-06 13:00:59 12030
3 2013-01-06 13:01:29 22188
4 2013-01-06 13:01:59 12183
5 2013-01-06 13:02:29 21112
6 2013-01-06 13:02:59 11628
I've plotted a normal line plot of this and it works fine.
ggplot(data=taonedf, aes(x=time, y=count/30)) + #
geom_line(color="#009E73") +
scale_y_continuous(name="requests per second", labels = format_format(scientific=FALSE, big.mark=",")) +
scale_x_datetime(name="",labels = date_format("%b %d\n%H:%M") ) +
labs(title=paste("Requests per Second - All Requests",count,sep="\n")) +
theme(legend.position = "none")
I want to add some vline annotations. I've created a second dataframe called EV, it looks like this:
> str(ev)
'data.frame': 10 obs. of 2 variables:
$ dt : POSIXct, format: "2013-01-06 13:45:00" "2013-01-06 14:18:00" "2013-01-06 14:49:00" ...
$ event: Factor w/ 9 levels "Event 1",..: 7 8 3 2 5 6 1 4 2 9
> head(ev)
dt event
1 2013-01-06 13:45:00 Event 1
Now, when I add the vline option I get odd results. I'm using the same date time format between the two so the scale should align.
ggplot(data=taonedf, aes(x=time, y=count/30)) +
geom_line(color="#009E73") +
geom_vline(data=ev,aes(xtintercept=dt))+
scale_y_continuous(name="requests per second", labels = format_format(scientific=FALSE, big.mark=",")) +
scale_x_datetime(name="",labels = date_format("%b %d\n%H:%M") ) +
labs(title=paste("Requests per Second - All Requests",count,sep="\n")) +
theme(legend.position = "none")
What am I missing? This doesn't appear to be that hard. All of the documentation and examples show simple numeric X axis so I'm assuming there is some issue with dates in the X axis but I can't pinpoint it. Any help would be appreciated.
> dput(taonedf)
structure(list(time = structure(c(1357506029.996, 1357506059.999,
1357506089.997, 1357506119.998, 1357506149.998, 1357506179.996,
1357506209.996, 1357506239.993, 1357506269.999, 1357506299.996,
1357506329.998, 1357506359.998, 1357506389.999, 1357506419.998,
1357506449.986, 1357506479.996, 1357506509.99, 1357506539.988,
1357506569.996, 1357506599.999, 1357506629.991, 1357506659.998,
1357506689.999, 1357506719.995, 1357506749.996, 1357506779.998,
1357506809.998, 1357506839.997, 1357506869.996, 1357506899.996,
1357506929.997, 1357506959.994, 1357506989.998, 1357507019.999,
1357507049.999, 1357507079.998, 1357507109.998, 1357507139.999,
1357507169.998, 1357507199.99, 1357507229.999, 1357507259.999,
1357507289.999, 1357507319.998, 1357507349.997, 1357507379.997,
1357507409.999, 1357507439.998, 1357507469.994, 1357507499.996,
1357507529.996, 1357507559.996, 1357507589.995, 1357507619.988,
1357507649.999, 1357507679.994, 1357507709.996, 1357507739.996,
1357507769.994, 1357507799.991, 1357507829.999, 1357507859.999,
1357507889.999, 1357507919.999, 1357507949.999, 1357507979.999,
1357508009.999, 1357508039.999, 1357508069.998, 1357508099.999,
1357508129.999, 1357508159.999, 1357508189.999, 1357508219.998,
1357508249.999, 1357508279.999, 1357508309.999, 1357508339.999,
1357508369.999, 1357508399.999, 1357508429.998, 1357508459.999,
1357508489.999, 1357508519.999, 1357508549.999, 1357508579.999,
1357508609.999, 1357508639.999, 1357508669.999, 1357508699.999,
1357508729.999, 1357508759.998, 1357508789.999, 1357508819.998,
1357508849.999, 1357508879.998, 1357508909.999, 1357508939.996,
1357508969.999, 1357508999.999, 1357509029.999, 1357509059.999,
1357509089.999, 1357509119.999, 1357509149.999, 1357509179.999,
1357509209.999, 1357509239.999, 1357509269.999, 1357509299.999,
1357509329.999, 1357509359.999, 1357509389.999, 1357509419.999,
1357509449.999, 1357509479.999, 1357509509.999, 1357509539.999,
1357509569.976, 1357509599.999, 1357509629.999, 1357509659.999,
1357509689.999, 1357509719.999, 1357509749.996, 1357509779.999,
1357509809.999, 1357509839.999, 1357509869.999, 1357509899.999,
1357509929.999, 1357509959.996, 1357509989.999, 1357510019.997,
1357510049.998, 1357510079.997, 1357510109.999, 1357510139.999,
1357510169.999, 1357510199.999, 1357510229.999, 1357510259.999,
1357510289.999, 1357510319.999, 1357510349.999, 1357510379.999,
1357510409.999, 1357510439.999, 1357510469.999, 1357510499.999,
1357510529.999, 1357510559.999, 1357510589.999, 1357510619.999,
1357510649.999, 1357510679.999, 1357510709.999, 1357510739.983,
1357510769.999, 1357510799.999, 1357510829.999, 1357510859.999,
1357510889.999, 1357510919.999, 1357510949.999, 1357510979.999,
1357511009.997, 1357511039.999, 1357511069.999, 1357511099.999,
1357511129.999, 1357511159.999, 1357511189.999, 1357511219.999,
1357511249.999, 1357511279.999, 1357511309.999, 1357511339.999,
1357511369.999, 1357511399.999, 1357511429.999, 1357511459.999,
1357511489.999, 1357511519.999, 1357511549.999, 1357511579.999,
1357511609.999, 1357511639.999, 1357511669.999, 1357511699.999,
1357511729.999, 1357511759.999, 1357511789.996, 1357511819.999,
1357511849.999, 1357511879.999, 1357511909.999, 1357511939.993,
1357511969.999, 1357511999.998, 1357512029.999, 1357512059.999,
1357512089.999, 1357512119.999, 1357512149.999, 1357512179.998,
1357512209.999, 1357512239.999, 1357512269.999, 1357512299.999,
1357512329.997, 1357512359.993, 1357512389.997, 1357512419.999,
1357512449.999, 1357512479.998, 1357512509.999, 1357512539.999,
1357512569.999, 1357512599.999, 1357512629.999, 1357512659.995,
1357512689.999, 1357512719.999, 1357512749.999, 1357512779.995,
1357512809.999, 1357512839.999, 1357512869.999, 1357512899.999,
1357512929.999, 1357512959.999, 1357512989.997, 1357513019.996,
1357513049.999, 1357513079.999, 1357513109.999, 1357513139.999,
1357513169.999, 1357513199.993, 1357513229.999, 1357513259.999,
1357513289.999, 1357513319.999, 1357513349.998, 1357513379.999,
1357513409.999, 1357513439.999, 1357513469.999, 1357513499.999,
1357513529.999, 1357513559.999, 1357513589.999, 1357513619.999,
1357513649.999, 1357513679.999, 1357513709.999, 1357513739.999,
1357513769.999, 1357513799.998, 1357513829.997, 1357513859.999,
1357513889.999, 1357513919.999, 1357513949.999, 1357513979.998,
1357514009.999, 1357514039.996, 1357514069.999, 1357514099.999,
1357514129.999, 1357514159.999, 1357514189.999, 1357514219.999,
1357514249.999, 1357514279.999, 1357514309.999, 1357514339.993,
1357514369.999, 1357514399.999, 1357514429.999, 1357514459.999,
1357514489.999, 1357514519.999, 1357514549.988, 1357514579.997,
1357514609.999, 1357514639.998, 1357514669.984, 1357514699.999,
1357514729.999, 1357514759.999, 1357514789.999, 1357514819.999,
1357514849.999, 1357514879.999, 1357514909.999, 1357514939.996,
1357514969.999, 1357514999.999, 1357515029.999, 1357515059.998,
1357515089.999, 1357515119.97, 1357515149.998, 1357515179.999,
1357515209.999, 1357515239.999, 1357515269.999, 1357515299.999,
1357515329.999, 1357515359.999, 1357515389.999, 1357515419.999,
1357515449.999, 1357515479.999, 1357515509.999, 1357515539.999,
1357515569.999, 1357515599.999, 1357515629.995, 1357515659.999,
1357515689.999, 1357515719.999, 1357515749.999, 1357515779.999,
1357515809.995, 1357515839.999, 1357515869.999, 1357515899.999,
1357515929.999, 1357515959.999, 1357515989.999, 1357516019.999,
1357516049.999, 1357516079.999, 1357516109.999, 1357516139.999,
1357516169.999, 1357516199.999, 1357516229.999, 1357516259.998,
1357516289.998, 1357516319.999, 1357516349.999, 1357516379.999,
1357516409.999, 1357516439.999, 1357516469.999, 1357516499.999,
1357516529.999, 1357516559.999, 1357516589.999, 1357516619.999,
1357516649.999, 1357516679.999, 1357516709.999, 1357516739.999,
1357516769.999, 1357516799.999, 1357516829.999, 1357516859.999,
1357516889.999, 1357516919.999, 1357516949.999, 1357516979.999,
1357517009.999, 1357517039.999, 1357517069.999, 1357517099.999,
1357517129.999, 1357517159.998, 1357517189.999, 1357517219.999,
1357517249.999, 1357517279.999, 1357517309.999, 1357517339.999,
1357517369.999, 1357517399.998, 1357517429.999, 1357517459.999,
1357517489.999, 1357517519.999, 1357517549.999, 1357517579.999,
1357517609.999, 1357517639.999, 1357517669.999, 1357517699.999,
1357517729.999, 1357517759.999, 1357517789.999, 1357517819.999,
1357517849.999, 1357517879.999, 1357517909.999, 1357517939.999,
1357517969.999, 1357517999.999, 1357518029.999, 1357518059.976,
1357518089.999, 1357518119.998, 1357518149.998, 1357518179.999,
1357518209.987, 1357518239.999, 1357518269.998, 1357518299.991,
1357518329.998, 1357518359.999, 1357518389.994, 1357518419.994,
1357518449.995, 1357518479.999, 1357518509.999, 1357518539.998,
1357518569.983, 1357518599.999, 1357518629.998, 1357518659.994,
1357518689.999, 1357518719.988, 1357518749.999, 1357518779.999,
1357518809.999, 1357518839.999, 1357518869.999, 1357518899.999,
1357518929.999, 1357518959.999, 1357518989.999, 1357519019.999,
1357519049.999, 1357519079.998, 1357519109.999, 1357519139.999,
1357519169.999, 1357519199.999, 1357519229.999, 1357519259.999,
1357519289.999, 1357519319.999, 1357519349.999, 1357519379.999,
1357519409.999, 1357519439.999, 1357519469.999, 1357519499.999,
1357519529.999, 1357519559.999, 1357519589.999, 1357519619.999,
1357519649.999, 1357519679.999, 1357519709.999, 1357519739.999,
1357519769.999, 1357519799.999, 1357519829.997, 1357519859.999,
1357519889.999, 1357519919.999, 1357519949.999, 1357519979.999,
1357520009.999, 1357520039.999, 1357520069.999, 1357520099.999,
1357520129.999, 1357520159.999, 1357520189.999, 1357520219.999,
1357520249.999, 1357520279.999, 1357520309.999, 1357520339.999,
1357520369.999, 1357520399.999), tzone = "", tclass = c("POSIXct",
"POSIXt"), class = c("POSIXct", "POSIXt")), count = c(20763L,
12030L, 22188L, 12183L, 21112L, 11628L, 21543L, 12609L, 20095L,
12992L, 21552L, 12447L, 21113L, 12236L, 21705L, 12018L, 21140L,
11820L, 21571L, 12803L, 21146L, 12081L, 21171L, 12440L, 21353L,
11708L, 21476L, 12210L, 21364L, 12041L, 21907L, 11934L, 22207L,
12403L, 21629L, 12676L, 21046L, 12196L, 21673L, 12190L, 21830L,
11652L, 20943L, 12350L, 20848L, 11800L, 21085L, 12367L, 21519L,
12325L, 22217L, 12195L, 22405L, 11869L, 21380L, 12145L, 21842L,
12224L, 21793L, 12856L, 34934L, 24073L, 41005L, 33964L, 46240L,
41287L, 52697L, 62618L, 78594L, 68193L, 76617L, 63747L, 90556L,
75830L, 104609L, 51063L, 67046L, 66977L, 82513L, 87228L, 107474L,
141878L, 127290L, 70953L, 98879L, 87814L, 117309L, 113463L, 150979L,
198271L, 170456L, 108325L, 119583L, 111803L, 117067L, 186768L,
226191L, 235546L, 228039L, 165570L, 159472L, 161707L, 137614L,
180049L, 254616L, 302166L, 336723L, 234902L, 202560L, 210679L,
173053L, 162839L, 262536L, 306859L, 249385L, 300646L, 219594L,
209819L, 166758L, 173716L, 268453L, 310940L, 264778L, 289798L,
202234L, 236882L, 217502L, 181157L, 196976L, 201901L, 228233L,
221241L, 220140L, 122623L, 76699L, 105589L, 381687L, 264571L,
187083L, 175972L, 202483L, 198547L, 196964L, 206402L, 181260L,
189319L, 162374L, 160412L, 186897L, 184529L, 160056L, 177326L,
184240L, 160864L, 156540L, 150392L, 157610L, 138447L, 148423L,
147318L, 148463L, 114389L, 163761L, 126624L, 167519L, 138240L,
133005L, 120187L, 155814L, 132751L, 140000L, 120323L, 124415L,
129450L, 116635L, 125364L, 108176L, 118877L, 143640L, 132457L,
118641L, 114330L, 135960L, 148066L, 130787L, 130230L, 130436L,
107109L, 129405L, 116093L, 135293L, 119048L, 147364L, 127028L,
145576L, 139960L, 139896L, 139433L, 127806L, 124845L, 141319L,
132821L, 129279L, 111905L, 130898L, 133135L, 138201L, 121460L,
143846L, 92964L, 100614L, 85637L, 139594L, 124302L, 106071L,
128247L, 120788L, 176300L, 144378L, 126209L, 117886L, 111001L,
105855L, 122387L, 152357L, 103217L, 134069L, 106021L, 91796L,
103335L, 99422L, 115839L, 147787L, 128868L, 123416L, 109312L,
129782L, 109397L, 130418L, 113709L, 103774L, 133272L, 137311L,
138079L, 132308L, 119744L, 164226L, 149361L, 135044L, 110185L,
151246L, 141811L, 160525L, 128407L, 159161L, 142969L, 150370L,
128705L, 151884L, 171663L, 150428L, 154910L, 165016L, 163729L,
169727L, 144913L, 163476L, 159984L, 155767L, 142334L, 177964L,
169230L, 135086L, 139350L, 174013L, 164427L, 154289L, 143392L,
187156L, 139426L, 159207L, 187435L, 198519L, 132559L, 163582L,
179069L, 150413L, 161463L, 173357L, 162457L, 136248L, 144086L,
151073L, 130237L, 144066L, 179840L, 135843L, 147757L, 206373L,
140734L, 177374L, 176168L, 154999L, 136136L, 187568L, 142357L,
152180L, 168528L, 131228L, 140622L, 145363L, 93070L, 58613L,
82024L, 86640L, 77493L, 71205L, 87641L, 89232L, 99214L, 89311L,
87948L, 90790L, 91326L, 106916L, 97318L, 89452L, 91658L, 82069L,
92559L, 89194L, 81721L, 83490L, 96388L, 90145L, 79861L, 90301L,
77676L, 262966L, 227355L, 256477L, 238905L, 241260L, 206168L,
229477L, 215515L, 245217L, 232026L, 225308L, 223537L, 198524L,
237840L, 233483L, 193081L, 216570L, 212949L, 203150L, 240861L,
209596L, 200673L, 180099L, 187726L, 187642L, 188402L, 176871L,
216090L, 203310L, 184723L, 195702L, 204137L, 276952L, 313717L,
323208L, 308448L, 321638L, 378236L, 352163L, 413678L, 395997L,
354317L, 366915L, 339465L, 346781L, 394895L, 355176L, 349618L,
417590L, 335474L, 405686L, 362581L, 356525L, 354142L, 383487L,
334305L, 327489L, 336201L, 374153L, 341485L, 321473L, 308773L,
15709L, 8870L, 15563L, 8944L, 15941L, 9342L, 16303L, 8951L, 14969L,
9385L, 14537L, 9963L, 15676L, 9011L, 16552L, 9587L, 16802L, 9693L,
15267L, 8946L, 14189L, 9067L, 14359L, 9776L, 167922L, 337364L,
350941L, 362928L, 364922L, 319641L, 348687L, 321356L, 400161L,
334171L, 332829L, 323842L, 397809L, 375694L, 384432L, 356825L,
350846L, 395942L, 359471L, 296926L, 418481L, 322144L, 335658L,
347212L, 334421L, 375769L, 364300L, 317370L, 373192L, 346713L,
356341L, 327225L, 305538L, 347815L, 276914L, 322149L, 303627L,
292363L, 284724L, 305082L, 373363L, 304386L, 438592L, 403579L,
430549L, 450536L, 432445L, 389779L, 434888L, 375010L, 456096L,
577393L, 451122L, 432354L, 425547L, 417729L)), .Names = c("time",
"count"), row.names = c(NA, -480L), class = "data.frame")
> dput(ev)
structure(list(dt = structure(c(1357508700, 1357510680, 1357512540,
1357515360, 1357517220, 1357517700, 1357518000, 1357518000, 1357519140,
1357519140), class = c("POSIXct", "POSIXt"), tzone = ""), event = structure(c(7L,
8L, 3L, 2L, 5L, 6L, 1L, 4L, 2L, 9L), .Label = c("Event 1",
"Event 2", "Event 3",
"Event 4", "Event 5",
"Event 6", "Event 7",
"Event 8", "Event 9"
), class = "factor")), .Names = c("dt", "event"), row.names = c(NA,
-10L), class = "data.frame")
Library Versions:
> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-redhat-linux-gnu (64-bit)
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.2.2 xts_0.9-1 zoo_1.7-9 gdata_2.12.0 data.table_1.8.6 caTools_1.14
[7] scales_0.2.3 ggplot2_0.9.3
Simplied code - this still doesnt work
library(scales)
library(ggplot2)
taonedf<-dget("taonedf") #in this thread
ev<-dget("ev") #in this thread
ggplot(data=taonedf, aes(x=time, y=count/30)) +
geom_line() +
geom_vline(data=ev,aes(xtintercept=as.numeric(dt)))
To get geom_vline() display lines as intended, first, library scales should be loaded. Then use as.numeric() in geom_vline().
library(scales)
+ geom_vline(data=ev,aes(xintercept=as.numeric(dt)))
Two things
You need to wrap the datetimes for the vline in as.numeric
You misspelled xintercept
Fixing those:
library("ggplot2")
library("scales")
ggplot(data=taonedf, aes(x=time, y=count/30)) +
geom_line(color="#009E73") +
geom_vline(data=ev,aes(xintercept=as.numeric(dt)))+
scale_y_continuous(name="requests per second", labels = format_format(scientific=FALSE, big.mark=",")) +
scale_x_datetime(name="",labels = date_format("%b %d\n%H:%M") ) +
labs(title=paste("Requests per Second - All Requests")) +
theme(legend.position = "none")

IBrokers request Historical Futures Contract Data?

I tried to request historical futures data but for a beginner the ibrokers.pdf document is not well enough documented.
example Gold Miny Contract Dec11 NYSELIFFE:
goldminy<-twsFuture("YG","NYSELIFFE","201112",multiplier="33.2")
reqHistoricalData(conn,
Contract= "goldminy",
endDateTime"",
barSize = "1 S",
duration = "1 D",
useRTH = "0",
whatToShow = "TRADES","BID", "ASK", "BID_ASK",
timeFormat = "1",
tzone = "",
verbose = TRUE,
tickerId = "1",
eventHistoricalData,
file)
I also don't know how to specify some of the data parameters correctly ?
whatToShow ? i need Date,Time,BidSize,Bid,Ask,AskSize,Last,LastSize,Volume
tickerID ?
eventHistoricalData ?
file ?
I wrote the twsInstrument package (on RForge) to alleviate these sorts of headaches.
getContract will find the contract for you if you give it anything reasonable. Any of these formats should work:
"YG_Z1", "YG_Z11", "YGZ1", "YGZ11", "YGZ2011", "YGDEC2011", "YG_DEC2011", etc. (also you could use the conId, or give it an instrument object, or the name of an instrument object)
> library(twsInstrument)
> goldminy <- getContract("YG_Z1")
Connected with clientId 100.
Contract details request complete. Disconnected.
> goldminy
List of 16
$ conId : chr "42334455"
$ symbol : chr "YG"
$ sectype : chr "FUT"
$ exch : chr "NYSELIFFE"
$ primary : chr ""
$ expiry : chr "20111228"
$ strike : chr "0"
$ currency : chr "USD"
$ right : chr ""
$ local : chr "YG DEC 11"
$ multiplier : chr "33.2"
$ combo_legs_desc: chr ""
$ comboleg : chr ""
$ include_expired: chr "0"
$ secIdType : chr ""
$ secId : chr ""
I don't have a subscription to market data for NYSELIFFE, so I will use the Dec 2011 e-mini S&P future for the rest of this answer.
You could get historical data like this
tws <- twsConnect()
hist.data <- reqHistoricalData(tws, getContract("ES_Z1"))
This will give you back these columns, and it will all be 'TRADES' data
> colnames(hist.data)
[1] "ESZ1.Open" "ESZ1.High" "ESZ1.Low" "ESZ1.Close" "ESZ1.Volume"
[6] "ESZ1.WAP" "ESZ1.hasGaps" "ESZ1.Count"
whatToShow must be one of 'TRADES', 'BID', 'ASK', or 'BID_ASK'. If your request uses whatToShow='BID' then you will get the OHLC etc. of the BID prices. "BID_ASK" means that the Ask price will be used for the High and the Bid price will be used for the Low.
Since you said the vignette was too advanced, it bears repeating that Interactive Brokers limits historical data requests to 6 every 60 seconds. So you should pause for 10 seconds between each request (or for getting lots of data I usually pause for 30 seconds after I make 3 requests so that if I have BID data for something I am also likely have ASK data for it)
The function getBAT will download the BID, ASK and TRADES data, and merge together only the closing values of those into a single xts object that looks like this:
> getBAT("ES_Z1")
Connected with clientId 120.
waiting for TWS reply on ES ............. done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ES .... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ES .... done.
Pausing 10 seconds between requests ...
Disconnecting ...
[1] "ES_Z1"
> tail(ES_Z1)
ES.Bid.Price ES.Ask.Price ES.Trade.Price ES.Mid.Price
2011-09-27 15:09:00 1170.25 1170.50 1170.50 1170.375
2011-09-27 15:10:00 1170.50 1170.75 1170.50 1170.625
2011-09-27 15:11:00 1171.25 1171.50 1171.25 1171.375
2011-09-27 15:12:00 1171.50 1171.75 1171.50 1171.625
2011-09-27 15:13:00 1171.25 1171.50 1171.25 1171.375
2011-09-27 15:14:00 1169.75 1170.00 1170.00 1169.875
ES.Volume
2011-09-27 15:09:00 6830
2011-09-27 15:10:00 4509
2011-09-27 15:11:00 4902
2011-09-27 15:12:00 6089
2011-09-27 15:13:00 6075
2011-09-27 15:14:00 14380
You asked for both LastSize and Volume. The "Volume" that getBAT returns is the total amount traded over the time of the bar. So, with 1 minute bars, it's the total volume that took place in that 1 minute.
Here's an answer that doesn't use twsInstrument:
I'm almost certain this will work, but as I said, I don't have the required market data subscription, so I can't test.
reqHistoricalData(tws, twsFuture("YG","NYSELIFFE","201112"))
Using the e-mini S&P again:
> mydata <- reqHistoricalData(tws, twsFuture("ES","GLOBEX","201112"), barSize='1 min', duration='5 D', useRTH='0', whatToShow='TRADES')
waiting for TWS reply on ES .... done.
> head(mydata)
ESZ1.Open ESZ1.High ESZ1.Low ESZ1.Close ESZ1.Volume ESZ1.WAP ESZ1.hasGaps ESZ1.Count
2011-09-21 15:30:00 1155.25 1156.25 1155.00 1155.75 3335 1155.50 0 607
2011-09-21 15:31:00 1155.75 1156.25 1155.50 1155.75 917 1155.95 0 164
2011-09-21 15:32:00 1155.75 1156.25 1155.50 1156.00 859 1155.90 0 168
2011-09-21 15:33:00 1156.00 1156.25 1155.50 1155.75 642 1155.83 0 134
2011-09-21 15:34:00 1155.50 1156.00 1155.25 1155.25 1768 1155.65 0 232
2011-09-21 15:35:00 1155.25 1155.75 1155.25 1155.25 479 1155.45 0 94
One of the problems with your attempt is that if you're using a barSize of '1 S', your duration cannot be greater than '60 S' See IB Historical Data Limitations

Resources