format dates as quarters in ggplot2 x axis - r

I have the following chart generated using ggplot2. Is there a way in which I can format my dates on the x axis as quarters (for example 2022-Q1) instead of the current date format which I have?
data <- structure(list(Date = structure(c(19083, 19086, 19087, 19088,
19089, 19090, 19093, 19094, 19095, 19096, 19101, 19102, 19103,
19104, 19107, 19108, 19109, 19110, 19111, 19114, 19115, 19116,
19117, 19118, 19121, 19122, 19123, 19124, 19125, 19128, 19129,
19130, 19131, 19132, 19135, 19136, 19137, 19138, 19139, 19143,
19144, 19145, 19146, 19149, 19150, 19151, 19152, 19153, 19156,
19157, 19158, 19159, 19160, 19164, 19165, 19166, 19167, 19170,
19171, 19172, 19173, 19174, 19178, 19179, 19180, 19181, 19184,
19185, 19186, 19187, 19188, 19191, 19192, 19193, 19194, 19195,
19198, 19199, 19200, 19201, 19202, 19205, 19206, 19207, 19208,
19209, 19212, 19213, 19214, 19215, 19216, 19219, 19220, 19221,
19222, 19223, 19226, 19227, 19228, 19229, 19230, 19233, 19234,
19235, 19236, 19237, 19241, 19242, 19243, 19244, 19247), class = "Date"),
US = c(-0.099, -0.082, -0.102, -0.276, -0.265, -0.214, -0.254,
-0.321, -0.263, -0.195, -0.189, -0.077, -0.025, -0.278, -0.543,
-0.595, -0.638, -0.587, -0.571, -0.754, -0.681, -0.597, -0.68,
-0.738, -0.942, -0.882, -0.657, -0.673, -0.468, -0.394, -0.323,
-0.357, -0.604, -0.533, -0.409, -0.529, -0.577, -0.496, -0.344,
-0.362, -0.326, -0.367, -0.365, -0.318, -0.43, -0.405, -0.515,
-0.658, -0.77, -1.033, -1.058, -1.217, -1.112, -1.096, -1.114,
-1.158, -1.013, -0.915, -0.863, -1.099, -1.192, -1.236, -1.395,
-1.144, -1.09, -1.167, -1.325, -1.378, -1.343, -1.492, -1.591,
-1.56, -1.254, -1.505, -1.327, -1.505, -1.542, -1.528, -1.526,
-1.591, -1.416, -1.513, -1.577, -1.389, -1.429, -1.486, -1.547,
-1.649, -1.61, -1.585, -1.475, -1.393, -1.307, -1.376, -1.571,
-1.744, -2.067, -2.294, -2.401, -2.423, -2.449, -2.69, -2.633,
-2.654, -2.812, -2.909, -3.037, -2.912, -2.456, -2.523, -2.51
)), row.names = c(NA, -111L), class = c("tbl_df", "tbl",
"data.frame"))
library(tidyverse)
ggplot(data, aes(Date, US)) +
geom_line(size=2) +
scale_x_date(breaks = '3 months')

You could try making use of the zoo package:
ggplot(data, aes(Date, US)) +
geom_line(size=2) +
scale_x_date(breaks = "3 months",
labels = function(x) zoo::format.yearqtr(x, "%Y-Q%q"))

Related

Impact of events in one dataframe have on the values of another dataframe

I have two very large dataframes of the following structure(s):
df1:
structure(list(Date = structure(c(18871, 18873, 18883, 18928,
18950, 18961, 18962, 18963, 18967, 18969, 18970, 18971, 18974,
18975, 18976, 18977, 18978, 18981, 18982, 18983, 18984, 18985,
18990, 18991, 18992, 18996, 18997, 18998, 18999, 19002, 19003,
19004, 19005, 19006, 19009, 19010, 19011, 19012, 19013, 19016,
19017, 19018, 19019, 19020, 19023, 19025, 19026, 19027, 19030,
19031, 19033, 19034, 19037, 19038, 19039, 19040, 19041, 19044,
19045, 19046, 19047, 19048, 19051, 19052, 19053, 19054, 19055,
19058, 19059, 19066, 19082, 19104, 19107, 19108, 19109, 19110,
19111, 19115, 19143, 19166, 19167, 19170, 19171, 19172, 19173,
19174, 19177, 19178, 19179, 19180, 19181, 19184, 19185, 19186,
19187, 19188, 19191, 19192, 19193, 19194, 19195, 19198, 19199,
19200, 19201, 19202, 19205, 19206, 19207, 19208, 19209, 19212,
19213, 19214, 19215, 19216, 19219, 19220, 19221, 19222, 19223,
19226, 19227, 19228, 19229, 19230, 19234, 19235, 19236, 19237,
19240, 19241, 19242, 19243, 19244, 19247, 19248, 19249, 19250,
19251, 19255, 19256, 19257, 19258, 19261, 19262, 19263, 19269,
19270, 19271, 19272, 19275, 19276, 19291, 19292, 19293, 19296,
19297, 19298, 19299, 19300, 19303, 19304, 19305, 19306, 19307,
19310, 19312, 19313, 19314, 19317, 19318, 19319, 19320, 19321,
19324, 19325, 19326, 19327, 19328, 19333, 19334, 19335, 19338,
19339, 19340, 19341, 19342, 19345, 19346, 19347, 19348, 19349,
19354, 19355, 19356, 19360, 19361, 19362, 19363, 19366, 19367,
19368, 19369, 19370, 19373, 19374, 19375, 19376, 19377, 19380,
19381, 19382, 19383, 19384, 19387, 19388, 19389, 19390, 19391,
19394, 19395, 19396, 19397, 19398, 19401, 19402, 19403, 19404,
18964, 18968, 19024, 19032, 19103, 19264, 19268, 18884, 18935,
19060, 19061, 19068, 19102, 19165, 19278, 18907, 18919, 18942,
18914, 18872, 18876, 18877, 18878, 18879, 18880, 18885, 18886,
18887, 18890, 18891, 18892, 18893, 18894, 18897, 18898, 18899,
18900, 18901, 18904, 18905, 18906, 18908, 18911, 18912, 18913,
18915, 18918, 18920, 18921, 18922, 18925, 18926, 18927, 18929,
18932, 18933, 18934, 18936, 18939, 18940, 18941, 18943, 18946,
18947, 18948, 18949, 18953, 18954, 18955, 18956, 18957, 18960,
19062, 19065, 19067, 19069, 19072, 19073, 19074, 19075, 19076,
19079, 19080, 19081, 19083, 19086, 19087, 19088, 19089, 19090,
19093, 19094, 19095, 19096, 19101, 19116, 19117, 19118, 19121,
19122, 19123, 19124, 19125, 19128, 19129, 19130, 19131, 19132,
19135, 19136, 19137, 19138, 19139, 19142, 19144, 19149, 19150,
19151, 19152, 19153, 19156, 19157, 19158, 19159, 19160, 19163,
19164, 19265, 19277, 19279, 19282, 19283, 19284, 19285, 19286,
19289, 19290, 19311, 19331, 19332), class = "Date"), Value = c(-5.33417292743301,
-2.52617494564308, -1.49324009324009, -17.0425444455863, -9.35793961841595,
-11.8841517857143, -9.69152125451611, -10.5028364323984, -8.5025565123789,
-7.38025700934579, -7.78238256870689, -8.17978487280178, -7.7131012583794,
-6.41295139213209, -8.91708282295298, -8.71221652160492, -10.0680747922438,
-11.9437278705109, -16.4053740896049, -20.3138418538824, -23.3452022125799,
-28.1797446210017, -28.9915196362348, -22.7089976941569, -53.3396956436279,
-12.4708793300343, -11.1787848605578, -11.2914868901427, -9.00403312503746,
-5.26940458505923, -3.2678042007321, -1.80023400936037, -1.00480716704905,
-2.57038505839299, -6.25052713783678, -4.13951632213265, -4.66916949663517,
-5.80691219642381, -9.01403811889207, -8.76200989722116, -5.14415894039735,
-4.49265067482651, -6.81002817489304, -5.00625312656328, -12.9697844076655,
-16.4525949550594, 1.35574468085106, -11.14101743721, -10.3890230312036,
-9.95399610136452, -10.7999731146659, -10.0551036897388, -8.00489963647858,
-6.98869824910699, -5.52771977448319, -5.8565313387104, -10.6561639051647,
-10.1591881404835, -10.5755809770487, -12.7858263854831, -12.4515269244669,
-9.75300293620429, -10.6174887030093, -10.9297709205513, -8.62649503888442,
-5.67639625979277, -7.76497565794115, -5.11864292912328, -4.15677111515569,
-1.24773160858034, -13.3209387381896, -4.61524571133755, -2.7064391500322,
-2.54590337369225, -4.7534422125529, -5.77741708660495, -9.54039920679418,
-3.49263873159683, -6.37633384146341, -4.8018775807754, -5.92095982827354,
-6.7982259326898, -8.14429721160882, -14.0318602941176, -15.7693341697285,
-6.33099406742874, -6.02467779730522, -6.67451006984472, -5.78277734678045,
-6.76536805011747, -5.30100480559197, -5.48448933319592, -3.07906668141104,
-1.19141872046993, -0.986825656313108, -2.78210279591495, -1.85680436798187,
1.15368964707724, 0.592389680247037, 2.09444444444445, 1.47565073474096,
-3.77455441789913, -4.99841919285848, -4.54737286347331, -6.52210666135604,
-12.4586229788041, -9.76655410805627, -10.1672813163265, -10.1071078030427,
12.1093036008042, -6.27654856354408, -11.7451651977779, -9.69657142857144,
-14.7645443406988, -13.8044752609797, -12.8212930202637, -10.7131114789657,
-9.7502835651603, -9.12285518188058, -7.82418739307804, -7.37290867229471,
-5.42488374865864, -7.16548962504551, -7.22640747577296, -5.67880989576978,
-2.38449197860963, -4.98403249527278, -8.01237481293888, -8.92721143345521,
-11.0038498048729, -7.91574019894676, -7.24256979885921, -13.0468491640639,
-12.4842416971359, -13.1832300362112, -9.69594285190952, -11.8236710963455,
-15.4970313957103, -16.001590401224, -23.0116940912636, -20.0316726652775,
-15.7387646961417, 14.3248459700742, -33.9856294639016, -29.5890893667004,
-24.5815039111784, 0.351626092151443, -12.4957158872518, -13.5265822044065,
-15.8057934508816, -18.5712850985479, -19.0062351207346, -17.8759978712081,
-30.248495829345, -33.496764540864, -32.9090642540002, -35.9095838866755,
-39.0622059592264, -37.4053056372396, 30.4444623180246, -17.2843857072932,
-10.2894505770196, -9.46059792738388, -10.9101340816963, -10.6768441621886,
-15.8564689156004, -25.0377375363291, -23.491809908999, -16.0848675710594,
-9.52203025543524, -9.57329945269742, -8.26118487113689, -8.77146105741898,
-7.52932569974554, -7.51696981061316, -6.34114162627894, -4.169791026429,
-8.65363440517035, -7.07429566797939, -6.04901960784313, -3.95286523637039,
-4.57831931564948, -4.70401930472815, -4.58048473762432, -4.19802716367601,
-4.8712225795747, 33.7199676963457, -8.38811297695784, -8.41675133350266,
-7.95239554602104, -7.61540762007296, -7.36910990686483, -5.19474884165624,
-6.40677640427848, -6.6880699933269, -2.50748947332091, -5.01177083333332,
-4.96342420082369, -3.74379953975965, -4.39864864864864, -3.96323864499468,
-3.64570892339191, -4.00421168284196, -4.32506635700066, -3.84347776249426,
-3.98014059753953, -4.93978444946085, -2.00666607412675, -4.40675836944916,
-4.67217558943196, -4.69624595469255, -4.93134138588684, -4.59980852082335,
-3.79959344732751, -3.56417422281594, -3.72577039757304, -6.8438477254722,
-6.90215521144616, 17.0278089071515, -8.69234350531171, -8.31160081053697,
-6.48055612912106, -2.82488289917003, -1.97938846776255, -2.76711193952573,
-3.57596324527513, -2.66680277664353, -2.76065192083818, -3.07607539874335,
-9.19352066115703, -7.40534903692798, -20.9290200655884, -9.4949337142347,
-1.67593031748771, -4.00032731786333, -12.9233908365795, -3.12797485406376,
-4.58038461538462, -4.92624181954726, -1.64177861663151, 14.5233533882204,
-3.43662848605578, -7.00163549013596, -22.4328399502006, -5.68106442090641,
-10.4667593755607, -8.32068944589428, -7.5574384739845, -1.18996298205332,
0.685336364575798, 0.683983218163869, -0.190581124472707, 0.40016858076072,
-0.685425265972909, -3.23485727546995, -2.17232406175909, -4.03732694666745,
-3.43151815181518, -4.09670641680863, -6.31686046511628, -8.351512347464,
-8.85095265741279, -7.33867558133818, -8.51522800856061, -8.98040313549832,
-21.688225290116, -7.97696621402419, -7.54824182322704, -6.15927894799952,
-4.38810945273632, -4.66345711216818, -6.63357687936353, -6.46356517733763,
-7.69505643096672, -8.85590341722938, -9.65988090292203, -7.41890982503365,
-8.4019243496411, -9.20245522237875, -11.7157621602604, -15.8009129904372,
-16.7188609056313, -29.5936953565184, -16.1899352493468, -14.6818673311081,
-9.23683475995534, -7.2676923950379, -5.58227412415489, -6.1622267560478,
-7.530241504595, -8.17124428752656, -6.35655475028349, -8.35745036021789,
-8.91222072419106, -8.32131192161132, -8.17337013669821, -10.877467450651,
-9.77258672063867, -11.2631774313289, -10.1360580092287, -7.70075269546349,
-3.16698645907571, -2.38109087441594, -0.022514417531718, -1.08499335989376,
-3.3854513350099, -13.1874976421323, -12.0737487121089, -6.16346335921227,
-6.0325834936609, -8.26060968320382, -6.76706578585191, -9.52737443465841,
-3.80397824702367, -4.72200198216056, -5.16157624343703, -7.23900280010839,
-6.18115990990991, -2.1293152465275, -3.7406576456566, -3.66946114241044,
-2.04129052515896, -0.222846441947564, -0.318394134689292, -2.16259885464958,
14.1518376302798, -1.73382942186606, 0.100049115913557, -0.378155479059093,
-0.588078533746669, -0.121356930514267, -2.06994937873907, -2.84085812981032,
-4.51622825625796, -3.44699324686462, -6.08916034821679, -10.3705139175517,
-7.18844523191799, -7.9430781129157, -5.99308878256247, -7.00972211589987,
-3.268970347887, -0.549423136888057, -6.08099664736361, -2.04115579182029,
-0.832810782197972, -0.916704322940202, -0.2527559897864, -1.11381138113811,
-1.81908592321755, -2.61384042630849, -2.35869084475896, 11.992052157715,
-2.80402835408022, -1.61877788005381, -2.68177518524358, -11.6975791772843,
-20.9404339865207, -27.5328187051002, -29.4899791627202, -28.9357382364862,
-30.3360181743081, -30.4065839909808, -32.100806252164, -34.963228894691,
-36.3754188653083, -24.3521969489358, -7.1991154066672, -4.39962753162101
)), row.names = c(NA, -369L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x00000214068b1ef0>)
df2:
structure(list(Date = structure(c(19402, 19396, 19395,
19389, 19376, 19374, 19367, 19362, 19346, 19339, 19333, 19332,
19332, 19327, 19319, 19311, 19311, 19306, 19305, 19291, 19290,
19284, 19283, 19277, 19276, 19270, 19269, 19262, 19242, 19241,
19236, 19227, 19220, 19213, 19206, 19200, 19194, 19192, 19185,
19178, 19171, 19151, 19150, 19136, 19129, 19123, 19122, 19102,
19094, 19087, 19066, 19059, 19053, 19038, 19018, 19010, 19003,
18968, 18962, 18947, 18941, 18934, 18933, 18933, 18926, 18920,
18913, 18912, 18906, 18905, 18905, 18898, 18892, 18885, 18884,
18878, 18877, 18877, 18871, 18863, 18856, 18850, 18849, 18842,
18835, 18828, 18815, 18814, 18814, 18809, 18801, 18794, 18793,
18793, 18787, 18786, 18786, 18780, 18780, 18766, 18765, 18765,
18758, 18758, 18752, 18752, 18745, 18738, 18737, 18731, 18730,
18725, 18724, 18724, 18710, 18709, 18703, 18702, 18702, 18696,
18695, 18690, 18688, 18688, 18681, 18675, 18674, 18674, 18668,
18661, 18660, 18660, 18654, 18653, 18653, 18648, 18640, 18639,
18639, 18633, 18605, 18604, 18604, 18598, 18598, 18597, 18597,
18590, 18590, 18584, 18583, 18583, 18578, 18578, 18577, 18570,
18569, 18569, 18563, 18562, 18562, 18557, 18557, 18555, 18549,
18548, 18548, 18542, 18542, 18541, 18541, 18536, 18536, 18534,
18529, 18529, 18521, 18520, 18520, 18515, 18515, 18508, 18508,
18507, 18500, 18499, 18499, 18494, 18493, 18492, 18492, 18486,
18485, 18485, 18479, 18479, 18478, 18478, 18472, 18472, 18471,
18471, 18465, 18465, 18464, 18464, 18458, 18458, 18457, 18457,
18452, 18450, 18450, 18445, 18445, 18444, 18444, 18438, 18437,
18437, 18436, 18436, 18430, 18430, 18429, 18429, 18424, 18424,
18423, 18416, 18416, 18415, 18415, 18410, 18410, 18409, 18409,
18403, 18403, 18402, 18396, 18396, 18395, 18388, 18388, 18387,
18387, 18381, 18381, 18380, 18380, 18374, 18374, 18373, 18373,
18368, 18368, 18367, 18367, 18360, 18360, 18359, 18359, 18354,
18340, 18338, 18331, 18326, 18325, 18317, 18312, 18296, 18289,
18282, 18275, 18270, 18268, 18247, 18241, 18235, 18233, 18226,
18214, 18205, 18198, 18191, 18184, 18177, 18170, 18163, 18144,
18142, 18128, 18121, 18114, 18100, 18093, 18079, 18072, 18065,
18059, 18051, 18039, 18025, 18002, 17995, 17988, 17981, 17969,
17961, 17953, 17948, 17941, 17918, 17913, 17904, 17877, 17871,
17869, 17855, 17850, 17841, 17827, 17808, 17799, 17794, 17785,
17780, 17764, 17751, 17736, 17731, 17715, 17708, 17702, 17688,
17675, 17660, 17654, 17645, 17640, 17631, 17625, 17617, 17605,
17596, 17591, 17584, 17577, 17554, 17549, 17542, 17540, 17513,
17507, 17505, 17491, 17486, 17479, 17463, 17458, 17449, 17444,
17436, 17428, 17422, 17401, 17386, 17379, 17372, 17366, 17353,
17344, 17339, 17323, 17318, 17309, 17304, 17290, 17282, 17276,
17268, 17260, 17253, 17247, 17239, 17234, 17227, 17220, 17212,
17206, 17204, 17197, 17184, 17178, 17176, 17171), class = "Date"),
Amount = c(3000, 2000, 3500, 3000, 1195.925, 4440.75, 3702.5,
3500, 3619.25, 3749.999, 744.65, 4062.498, 2812.5, 2812.499,
3559.5, 3250, 2250, 4374.998, 2750, 3500, 898.85, 4062.5,
3125, 4365.25, 1106.299, 3749.999, 2500, 1200, 3437.5, 4183.157,
2499.999, 750, 2803.5, 1760, 2771.875, 700, 4366.249, 2500,
3437.5, 2378.75, 1011.975, 3125, 3075, 847.7, 2187.499, 2500,
2250, 2499.998, 3124.998, 3250, 1239.4, 1874.999, 3250, 2250,
600, 3124.998, 3000, 1875, 2250, 2170.375, 900, 2500, 3000,
1533.75, 2750, 2812.5, 567.4, 1562.5, 3125, 3000, 2288.125,
2000, 350, 2857.916, 3000, 1010.725, 3250, 1250, 2859, 3000,
2274.25, 705, 3437.499, 2000, 3000, 1562.5, 701.05, 3437.499,
1866.25, 4232.497, 500, 3118.75, 3055.281, 2000, 1172.438,
3742.497, 1562.5, 3437.499, 2500, 3093.749, 4062.495, 2812.499,
3283.75, 1500, 2750, 2000, 1113.65, 3124.999, 4062.498, 713.825,
1000, 3437.5, 3394, 2000, 357.25, 2250, 2500, 4375, 1633,
878.975, 2812.5, 3437.5, 3147.351, 1562.499, 2499.998, 2500,
3250, 1250, 2000, 3143, 3000, 1071.25, 1036.3, 2936.25, 1750,
3250, 375, 3000, 1450.624, 3749.999, 3011.102, 4029.372,
2500, 1250, 2750, 3669.355, 2250, 3002.75, 1562.499, 2500,
4062.499, 2500, 3749.999, 1562.499, 861.4, 3000, 3405.625,
2499.999, 3084.999, 3510.742, 1249.999, 2632, 1986.875, 697.049,
3125, 3000, 1562.499, 2500, 884.149, 3518.452, 2000, 3250,
2000, 3437.499, 3749.999, 1310.749, 3124.999, 2867.5, 2187.5,
3250, 2421.749, 3412.749, 2500, 459.05, 2750, 3000, 1342.375,
1374.999, 3437.5, 4062.497, 2477.25, 3437.499, 3250, 1250,
2750, 562.25, 4062.499, 2812.499, 3500, 3008.875, 3437.499,
1874.999, 3000, 500, 3749.999, 2711, 3250, 1500, 3834.095,
3750, 1062.85, 3750, 2812.5, 4261.195, 2329.25, 3000, 2785.999,
1229.576, 3250, 1768.25, 3250, 3628.75, 4260.749, 2812.5,
3716.25, 2000, 3250, 4036.624, 900, 3941.249, 1500, 3328.75,
3749.999, 2927.5, 2357.75, 3750, 1785.499, 1562.499, 2500,
3869.624, 3250, 2250, 745.85, 3897.958, 1750, 4062.499, 3000,
3616.375, 2152.5, 1499.999, 3749.999, 3671.476, 3141, 4062.499,
1874.999, 3648.75, 2499.998, 3676.249, 2313.749, 2750, 2092.5,
4062.5, 1562.499, 2299.997, 3250, 2299.999, 2587.488, 1244.238,
3500, 3441.975, 3269.875, 800, 2750, 2545.872, 3250, 1056.037,
3162.497, 3162.496, 500, 2082.45, 3162.498, 3449.922, 2356.675,
2820.25, 1100, 3449.997, 3162.499, 919.998, 2250, 574.995,
2750, 3000, 1264.997, 2299.997, 3000, 3162.497, 2250, 3373.097,
2250, 3162.499, 700, 3000, 1437.496, 3000, 2250, 3135.1,
3449.998, 325, 1724.998, 3414.749, 1254.571, 1688.5, 2587.497,
2012.498, 2799.403, 2563.87, 500, 2012.499, 2500, 555.065,
2299.997, 2250, 1100, 3000, 872.72, 2750, 2500, 3000, 1100,
2500, 2750, 2299.997, 2874.995, 2587.495, 1381.4, 2750, 1146.874,
2810.081, 3427.034, 750, 2500, 2185.16, 2750, 675.24, 2864.731,
2250, 2815.66, 950, 2562.68, 1000, 2500, 2250, 2587.497,
800, 2276.5, 2750, 1000, 2722.32, 2750, 650, 2599.24, 2500,
3162.5, 950, 2500, 2500, 3158.628, 1000, 2250, 2500, 3162.498,
2867.806, 1000, 2587.498, 2874.999, 2500, 882.3, 2962.15,
2293.788, 857.793, 3162.497, 1724.999, 2867.739, 2874.999,
2299.997, 2557.917, 833.737, 2500, 2299.997, 1250, 2624.29,
3004.215, 2444.046, 2881.949, 2250, 837.798, 2329.914)), row.names = c(NA,
397L), class = "data.frame")
I'm trying to work out the percentage impact the events listed in the second dataframe (df2) have on the values from the previous day entry listed in the first dataframe (df1). Does anyone know how I can achieve this? APologies if unclear, English is not my first language.

Different errors in using left_join in r

I am trying to interpolate two series but I get different errors when it comes to use left_join. These are the two series:
df1 = structure(list(Date = structure(c(11690, 11725, 11753, 11781,
11809, 11844, 11872, 11900, 11942, 11970, 11998, 12026, 12061,
12089, 12117, 12145, 12180, 12208, 12243, 12265, 12299, 12327,
12362, 12390, 12425, 12453, 12481, 12509, 12544, 12572, 12600,
12631, 12663, 12698, 12726, 12754, 12796, 12817, 12845, 12880,
12907, 12936, 12971, 12996, 13027, 13062, 13090, 13118, 13160,
13181, 13209, 13244, 13272, 13307, 13335, 13363, 13392, 13426,
13454, 13489, 13524, 13552, 13580, 13615, 13643, 13670, 13699,
13726, 13762, 13790, 13825, 13853, 13888, 13916, 13944, 13979,
14007, 14035, 14063, 14098, 14126, 14154, 14189, 14217, 14259,
14280, 14308, 14336, 14371, 14399, 14427, 14462, 14490, 14525,
14553, 14581, 14623, 14644, 14672, 14707, 14735, 14770, 14798,
14826, 14854, 14889, 14917, 14945, 14987, 15008, 15036, 15071,
15099, 15134, 15162, 15190, 15225, 15253, 15281, 15316, 15351,
15379, 15407, 15434, 15463, 15497, 15526, 15554, 15589, 15617,
15652, 15680, 15715, 15743, 15771, 15799, 15827, 15862, 15890,
15918, 15953, 15980, 16016, 16044, 16079, 16107, 16135, 16163,
16198, 16226, 16254, 16289, 16317, 16345, 16380, 16408, 16457,
16467, 16499, 16540, 16556, 16589, 16632, 16648, 16681, 16730,
16740, 16772, 16821, 16832, 16870, 16912, 16922, 16954, 17003,
17014, 17052, 17094, 17106, 17143, 17185, 17198, 17234, 17283,
17287, 17325, 17367, 17379, 17416, 17465, 17471, 17514, 17556,
17563, 17598, 17647, 17652, 17696, 17738, 17744, 17787, 17829,
17836, 17878, 17920, 17928, 17962, 17996, 18017, 18053, 18102,
18109, 18151, 18193, 18201, 18242), class = "Date"), Fit = c(-1.68038051095608,
-2.12317945962401, -2.71086209338424, -3.22489682411764, -2.51769032592554,
-1.33242532610804, -2.13564807610995, -2.13564807610995, -2.03415137348661,
-1.58909921518124, -0.68844714029518, -1.94691881575563, -1.16714425518695,
-2.15153420569546, -2.04779261960842, -0.867515299774483, -1.47986823637587,
-0.650513604798111, -1.61361732632524, -1.61361732632524, -1.48596960028163,
-2.20004804407501, -2.64689217553021, -2.67436545120372, -3.48049123019991,
-2.28510809912552, -2.32172665536549, -1.76823348887895, -2.763353378483,
-2.09381469041352, -2.08400217235893, -2.08400217235893, -1.59720187270177,
-2.06034560841579, -2.56317571167687, -1.32635640217861, -1.48729782102413,
-2.00732693090646, -3.40272319833461, -2.49810610074565, -3.32810591309226,
-1.92402348117091, -1.35397391665409, -1.35397391665409, -1.76640461987233,
-2.51735528772741, -2.35332514240503, -2.35272634525907, -2.68468172493552,
-2.92171051216825, -3.16925509035157, -2.23093489115309, -3.06530983495044,
-2.30445613039677, -3.30583207178147, -2.82188405397887, -2.82188405397887,
-2.51962207875813, -2.66982767393931, -2.98041595989062, -2.55306093500464,
-3.36871970472208, -2.6201699311654, -2.49494986723432, -2.78910296607766,
-2.57178346785915, -3.28445145020619, -3.28445145020619, -2.42213276484722,
-2.38709926337358, -2.99568739417641, -2.65138790450656, -1.58074660422518,
-1.91906056315361, -2.90337379582732, -2.20117735700684, -1.29303194740331,
-2.2494711668963, -1.53224235243069, -2.04481468859739, -0.929377541149286,
-0.0656900034556943, -2.20508088335906, -0.578502434372448, -0.858925093713048,
-2.96970181427872, -2.97862851965814, -3.31779605911778, -2.08207766441392,
-2.4848907725881, -1.99650762208841, -2.17076028347941, -1.27061305909,
-1.92537835567221, -1.49409886851971, -2.32667673830125, -1.90852299188928,
-2.88390697795999, -2.9172411130509, -3.5788915130035, -2.92151370364156,
-2.71478221054495, -3.40505963822921, -3.21129210245385, -2.66481268506047,
-3.27494238949828, -3.37230078139583, -3.53346393522174, -3.99626066800013,
-3.41713548837371, -3.67409843863548, -3.50182175058264, -3.37467436298585,
-3.40580625489191, -2.99003362011982, -3.66653724632765, -3.61588309823573,
-3.18167532125541, -3.35619362672467, -2.8806023910338, -2.23185392156432,
-0.626338062660262, -2.00482507082523, -1.7359344838577, -0.800609837957656,
-1.93997314433068, -0.93482911672218, -2.63652739226028, -1.93546315176661,
-1.17524044037369, -2.274143231604, -2.69679235052359, -2.23731851002543,
-1.86716852238077, -0.716926538642468, -1.30258784685856, -1.08194224250233,
-0.930472602419788, 1.13081617308243, 2.57874064965174, -0.388461792958877,
-0.340421132850094, 0.638071432169484, -1.57318833539501, 4.40961161388978,
1.62060735764472, -0.674369921428344, 1.76679629890753, -0.748857461264583,
0.520407646064921, -0.575555044309392, 4.34511452569889, 3.34194433067617,
3.21304624521961, 1.97147139745885, 3.45052500620869, -0.399783726365629,
-0.399783726365629, -0.519126753266423, -0.924795366280377, -0.924795366280377,
4.49023560078066, 2.40210157814194, 2.40210157814194, 3.48634734098189,
2.72330542145941, 2.72330542145941, 2.44175433903678, 1.83754103018167,
1.83754103018167, 2.00744190890702, 2.84495436947268, 2.84495436947268,
3.68678762972081, 3.16657087164265, 3.16657087164265, 2.54382298188246,
3.23525009614645, 3.23525009614645, 2.25680930181096, 1.44116917115332,
1.44116917115332, -0.510916286085479, -0.212859586548733, -0.212859586548733,
-0.740774827758169, -0.740774827758169, -0.740774827758169, -0.740774827758169,
-0.41701391913469, -0.41701391913469, -1.22601225946438, -0.632288989161026,
-0.632288989161026, -1.04309509051099, -1.17291896442253, -1.17291896442253,
-1.17291896442253, -1.17291896442253, -1.17291896442253, -1.11302828507398,
-2.78220449305609, -2.78220449305609, -2.86224137302201, -2.86224137302201,
-2.86224137302201, 1.34122649704384, 1.33515381941954, 1.33515381941954,
2.56866171677324, 0.963485086246604, 0.963485086246604, 4.22653525972732,
5.35095831479335, 5.35095831479335, 3.93448842430935)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -216L))
df2 = structure(list(Fit = c(-3.36981025066699, -2.83074686834444,
-3.4112766646918, -2.45754914212114, -2.16728424394746, -3.61860873481586,
-3.61860873481586, -2.2916834860219, -1.37942237747605, -3.95034004701435,
-1.46235520552567, -2.91367969639407, -3.95034004701435, -3.57714232079105,
-2.16728424394746, -2.20875065797227, -2.6648812122452, -2.74781404029482,
-2.00141858784822, -3.07954535249331, -3.07954535249331, -3.24541100859256,
-3.36981025066699, -2.49901555614595, -2.6648812122452, -3.95034004701435,
-3.0380789384685, -3.24541100859256, -3.12101176651812, -2.58194838419558,
-2.83074686834444, -3.4112766646918, -2.6648812122452, -2.99661252444369,
-3.61860873481586, -2.99661252444369, -3.57714232079105, -2.74781404029482,
-3.20394459456775, -2.25021707199708, -2.37461631407152, -3.61860873481586,
-3.36981025066699, -2.95514611041888, -3.61860873481586, -3.32834383664218,
-2.74781404029482, -3.20394459456775, -2.00141858784822, -3.12101176651812,
-3.66007514884067, -3.47347628572902, -3.95034004701435, -3.45274307871661,
-3.45274307871661, -2.83074686834444, -3.95034004701435, -2.99661252444369,
-3.61860873481586, -2.16728424394746, -3.70154156286548, -2.37461631407152,
-2.83074686834444, -2.12581782992265, -3.32834383664218, -2.6648812122452,
-3.0380789384685, -3.20394459456775, -3.4112766646918, -2.83074686834444,
-2.91367969639407, -3.95034004701435, -2.6648812122452, -3.36981025066699,
-2.2916834860219, -3.45274307871661, -2.37461631407152, -1.54528803357529,
-2.91367969639407, -3.49420949274142, -2.95514611041888, -3.32834383664218,
-2.45754914212114, -3.57714232079105, -3.36981025066699, -3.45274307871661,
-3.95034004701435, -3.16247818054293, -2.83074686834444, -3.36981025066699,
-2.6648812122452, -2.6648812122452, -2.45754914212114, -3.32834383664218,
-3.66007514884067, -3.36981025066699, -3.95034004701435, -2.83074686834444,
-2.91367969639407, -3.45274307871661, -2.20875065797227, -2.6648812122452,
-3.95034004701435, -3.95034004701435, -2.74781404029482, -2.83074686834444,
-2.99661252444369, -3.57714232079105, -3.95034004701435, -1.54528803357529,
-2.45754914212114, -2.91367969639407, -3.20394459456775, -2.83074686834444,
-3.70154156286548, -1.29648954942642, -3.36981025066699, -2.20875065797227,
-2.45754914212114, -3.24541100859256, -2.6648812122452, -1.87701934577378,
-2.83074686834444, -2.37461631407152, -3.95034004701435, -3.07954535249331,
-2.6648812122452, -2.99661252444369, -2.91367969639407, -3.49420949274142,
-3.61860873481586, -3.20394459456775, 0.44509983961565, 0.776831151814141,
-2.37461631407152, -3.61860873481586, -0.259829198806142, -2.37461631407152,
3.72094654757574, -1.37942237747605, -3.12101176651812, 0.196301355466782,
-2.20875065797227, -0.923291823203123, -2.58194838419558, 0.569499081690084,
0.486566253640461, 1.56469301828556, -1.62822086162492, 3.59654730550131,
-0.923291823203123, -2.2916834860219, -0.633026925029444, 0.486566253640461,
2.14522281463291, 1.15002887803744, 2.60135336890584, 2.97455109512914,
1.02562963596301, 1.89642433048405, 2.97455109512914, 3.38921523537725,
3.22334957927801, 3.47214806342688, 1.77202508840961, 4.05267785977424,
3.30628240732763, -0.0939635427068968, -0.342762026855765, -0.0939635427068968,
1.23296170608707, -0.342762026855765, -0.425694854905387, -3.36981025066699,
-0.674493339054255, -1.08915747930237, -1.37942237747605, -2.08435141589784,
-1.46235520552567, -1.46235520552567, -2.83074686834444, -2.25021707199708,
-3.0380789384685, 3.05748392317876, 1.15002887803744, 2.68428619695546,
0.196301355466782, 2.26962205670735, 2.85015185305471, 2.85015185305471
), Date = c("2002-01-03", "2002-02-07", "2002-03-07", "2002-04-04",
"2002-05-02", "2002-06-06", "2002-07-04", "2002-09-12", "2002-10-10",
"2002-11-07", "2002-12-05", "2003-01-09", "2003-02-06", "2003-03-06",
"2003-04-03", "2003-05-08", "2003-06-05", "2003-07-10", "2003-09-04",
"2003-10-02", "2003-11-06", "2003-12-04", "2004-01-08", "2004-02-05",
"2004-03-04", "2004-04-01", "2004-05-06", "2004-06-03", "2004-07-01",
"2004-09-02", "2004-10-07", "2004-11-04", "2004-12-02", "2005-01-13",
"2005-02-03", "2005-03-03", "2005-04-07", "2005-05-04", "2005-06-02",
"2005-07-07", "2005-09-01", "2005-10-06", "2005-11-03", "2005-12-01",
"2006-01-12", "2006-02-02", "2006-03-02", "2006-04-06", "2006-05-04",
"2006-06-08", "2006-07-06", "2006-08-03", "2006-10-05", "2006-11-02",
"2006-12-07", "2007-01-11", "2007-02-08", "2007-03-08", "2007-04-12",
"2007-05-10", "2007-06-06", "2007-07-05", "2007-09-06", "2007-10-04",
"2007-11-08", "2007-12-06", "2008-01-10", "2008-02-07", "2008-03-06",
"2008-04-10", "2008-05-08", "2008-06-05", "2008-07-03", "2008-08-07",
"2008-09-04", "2008-10-02", "2008-11-06", "2008-12-04", "2009-01-15",
"2009-02-05", "2009-03-05", "2009-04-02", "2009-05-07", "2009-06-04",
"2009-07-02", "2009-08-06", "2009-09-03", "2009-10-08", "2009-11-05",
"2009-12-03", "2010-01-14", "2010-02-04", "2010-03-04", "2010-04-08",
"2010-05-06", "2010-06-10", "2010-07-08", "2010-08-05", "2010-09-02",
"2010-10-07", "2010-11-04", "2010-12-02", "2011-01-13", "2011-02-03",
"2011-03-03", "2011-04-07", "2011-05-05", "2011-06-09", "2011-07-07",
"2011-08-04", "2011-09-08", "2011-10-06", "2011-11-03", "2011-12-08",
"2012-01-12", "2012-02-09", "2012-03-08", "2012-04-04", "2012-05-03",
"2012-06-06", "2012-07-05", "2012-08-02", "2012-09-06", "2012-10-04",
"2012-11-08", "2012-12-06", "2013-01-10", "2013-02-07", "2013-03-07",
"2013-04-04", "2013-05-02", "2013-06-06", "2013-07-04", "2013-08-01",
"2013-09-05", "2013-10-02", "2013-11-07", "2013-12-05", "2014-01-09",
"2014-02-06", "2014-03-06", "2014-04-03", "2014-05-08", "2014-06-05",
"2014-07-03", "2014-08-07", "2014-09-04", "2014-10-02", "2014-11-06",
"2014-12-04", "2015-01-22", "2015-03-05", "2015-04-15", "2015-06-03",
"2015-07-16", "2015-09-03", "2015-10-22", "2015-12-03", "2016-01-21",
"2016-03-10", "2016-04-21", "2016-06-02", "2016-07-21", "2016-09-08",
"2016-10-20", "2016-12-08", "2017-01-19", "2017-03-09", "2017-04-27",
"2017-06-08", "2017-07-20", "2017-09-07", "2017-10-26", "2017-12-14",
"2018-01-25", "2018-03-08", "2018-04-26", "2018-06-14", "2018-07-26",
"2018-09-13", "2018-10-25", "2018-12-13", "2019-01-24", "2019-03-07",
"2019-04-10", "2019-06-06", "2019-07-25", "2019-09-12", "2019-10-24",
"2019-12-12")), class = "data.frame", row.names = c(NA, -190L
))
I then used these codes to interpolate the series:
# first series
monthss <- data.frame(Date = seq(as.Date("2002-01-03"), as.Date("2019-12-12"), by = "month"), stringsAsFactors = F)
df1 <- left_join(x = monthss, y = df1, by = "Date")
df1 <- na.approx(object = df1$'as it comes from the previous step', rule = 2)
# second series
df2 <- left_join(x = monthss, y = df2, by = "Date")
df2 <- na.approx(object = df2$'as it comes from the previous step', rule = 2)
However, in the first case, I get a weird result when using left_join (I get too many NAs that don't correspond to the missing dates), while in the second case, when using left_join, I get Error: cannot join a Date object with an object that is not a Date object. For this error, I tried to look for some solutions online, but what I found doesn't help.
Can anyone help me fix this?
Thanks!
You get the left_join error in the second case because df2$Date is not of class Date. This fixes the second join:
library(lubridate)
# second series
df2$Date <- lubridate::date(df2$Date) #convert Date variable to date
df2 <- left_join(x = monthss, y = df2, by = "Date")
df2 <- na.approx(object = df2$'as it comes from the previous step', rule = 2)
Changing this will fix the first join:
# first series
monthss <- data.frame(Date = seq(as.Date("2002-01-03"), as.Date("2019-12-12"), by = "day"), stringsAsFactors = F)
You did a sequence by month and thus only got the dates for each month where the day is "3" e.g., 2002-01-03, 2002-02-03 etc. Thus you only got joins for Dates ending on "03" but not for other dates that you have Fit values for in df1.

"Breakpoints" don't display dates but a continuous measure

I'm trying to detect some structural breaks in my series. The problem is that it displays continuous numbers rather than dates, despite my series being a ts object.
I found this solution but it doesn't work in my case.
This is my dataset and code:
df = structure(list(Date = structure(c(11690, 11725, 11753, 11781,
11809, 11844, 11872, 11900, 11942, 11970, 11998, 12026, 12061,
12089, 12117, 12145, 12180, 12208, 12243, 12265, 12299, 12327,
12362, 12390, 12425, 12453, 12481, 12509, 12544, 12572, 12600,
12631, 12663, 12698, 12726, 12754, 12796, 12817, 12845, 12880,
12907, 12936, 12971, 12996, 13027, 13062, 13090, 13118, 13160,
13181, 13209, 13244, 13272, 13307, 13335, 13363, 13392, 13426,
13454, 13489, 13524, 13552, 13580, 13615, 13643, 13670, 13699,
13726, 13762, 13790, 13825, 13853, 13888, 13916, 13944, 13979,
14007, 14035, 14063, 14098, 14126, 14154, 14189, 14217, 14259,
14280, 14308, 14336, 14371, 14399, 14427, 14462, 14490, 14525,
14553, 14581, 14623, 14644, 14672, 14707, 14735, 14770, 14798,
14826, 14854, 14889, 14917, 14945, 14987, 15008, 15036, 15071,
15099, 15134, 15162, 15190, 15225, 15253, 15281, 15316, 15351,
15379, 15407, 15434, 15463, 15497, 15526, 15554, 15589, 15617,
15652, 15680, 15715, 15743, 15771, 15799, 15827, 15862, 15890,
15918, 15953, 15980, 16016, 16044, 16079, 16107, 16135, 16163,
16198, 16226, 16254, 16289, 16317, 16345, 16380, 16408, 16457,
16467, 16499, 16540, 16556, 16589, 16632, 16648, 16681, 16730,
16740, 16772, 16821, 16832, 16870, 16912, 16922, 16954, 17003,
17014, 17052, 17094, 17106, 17143, 17185, 17198, 17234, 17283,
17287, 17325, 17367, 17379, 17416, 17465, 17471, 17514, 17556,
17563, 17598, 17647, 17652, 17696, 17738, 17744, 17787, 17829,
17836, 17878, 17920, 17928, 17962, 17996, 18017, 18053, 18102,
18109, 18151, 18193, 18201, 18242), class = "Date"), Fit = c(-2.01864866574525,
-2.51081772611801, -3.07896216512166, -3.02724722640642, -0.764567739958455,
-1.81459657078637, -2.13093106123547, -2.13093106123547, -1.91543051022373,
-1.31418467170089, -1.86573850139921, -2.42539556395029, -1.26414303389104,
-2.5433900359616, -1.99767537794132, -1.34728409808229, -1.64315561542246,
-0.687106946387411, -2.48041219070826, -2.48041219070826, -1.78680159845671,
-2.13687301896279, -2.6123923387608, -2.84563515334999, -3.41506073833104,
-2.74565641471061, -2.3682788731863, -1.77410755661286, -2.46191758167165,
-2.34829604543204, -2.37030627525843, -2.37030627525843, -1.75944822651175,
-2.21875944722698, -2.60249841953241, -2.6758310533823, -1.99157259723667,
-2.34860918772813, -3.24977356678388, -3.1998805120359, -3.64471855523435,
-2.80762315792921, -1.46910836105049, -1.46910836105049, -2.24153954648439,
-2.64718944648088, -2.61088260257325, -2.45889016663966, -2.59732356608009,
-3.49037732690643, -2.75284369990193, -2.56284320115193, -3.01470163344929,
-2.24267403694233, -3.36759206183078, -2.65899770326269, -2.65899770326269,
-3.83487166356133, -2.30405890853423, -3.83487166356133, -2.91420930066836,
-2.92649062542454, -2.45288174087111, -2.59203353843301, -2.37211828478634,
-2.35485833573613, -3.28807932670479, -3.28807932670479, -2.69856893402308,
-2.4482421908289, -3.42965769805337, -2.4002640291758, -1.72498017056001,
-2.10246950134994, -2.75989530409431, -2.04609226712013, -1.50354129352453,
-1.721866774994, -1.42652131446034, -1.99149928941641, -0.924508173463412,
-0.34424720787331, -1.47956887747857, -0.699260660882747, -0.705970004477605,
-2.89615299118885, -2.87168709242964, -3.49698896688496, -1.80133944039088,
-2.3066390154612, -2.16578274820764, -1.62064416630292, -1.50034889686538,
-1.64551702528081, -1.54888542275039, -2.36526073757675, -2.17980843362752,
-2.61987658921009, -2.99580131757171, -3.27224528690084, -2.90968038360951,
-2.43786428440244, -3.53447897261775, -2.94164730632451, -2.67914051197011,
-3.08963971104142, -3.30489291781406, -3.16112222668117, -3.78875309229899,
-3.27799815735179, -3.27546357519604, -3.28715323339141, -3.277230212033,
-2.73537305926061, -2.63360778909794, -3.42285993586989, -3.02592822360864,
-2.80491835054881, -3.1610709896381, -2.69912996631718, -2.48975331263934,
-0.134524884114962, -2.3485759078928, -1.67019370390805, -1.30630530826772,
-2.2627030307026, -1.19967822767006, -2.18902328617136, -2.32822018421121,
-1.05335780233708, -2.32765305050142, -3.70136681094428, -2.47624061269887,
-2.2395891355029, -0.873612387550348, -2.52750186765166, -1.58254587448088,
-1.3519682697086, -2.67716755653968, -2.09120993997918, -2.83947106437091,
-1.59227436938979, -2.70393468772428, -3.07475393381032, -1.72535933812472,
-2.62864985613023, -2.1788856069182, -1.66072722296379, -2.02593106477748,
-0.236862069023111, -2.20046381510765, 2.67747589830398, 2.03103654671807,
-0.411843127888723, 0.15392859458, 3.15264600488878, -0.115883494946465,
-0.115883494946465, -2.48408112888983, -2.13179786204659, -2.13179786204659,
-0.421916196665926, -1.81454302259545, -1.81454302259545, -0.719344207794365,
2.30623888786222, 2.30623888786222, 0.233349485130917, 0.807655736612547,
0.807655736612547, 0.00810498434400109, 1.73561337499853, 1.73561337499853,
2.05294933680988, 2.52332617911213, 2.52332617911213, 1.6590362509139,
2.44897469036036, 2.44897469036036, 1.48162277916561, 0.109012820753664,
0.109012820753664, -0.552382527186447, 0.342735574558364, 0.342735574558364,
0.352860787128766, 0.352860787128766, 0.352860787128766, 0.352860787128766,
0.726520452040748, 0.726520452040748, 0.176144461112964, 2.28171712015304,
2.28171712015304, 0.256037205994603, 0.10686264754173, 0.10686264754173,
0.10686264754173, 0.10686264754173, 0.10686264754173, -0.871047910469186,
-1.61892724112359, -1.61892724112359, -2.04847571973674, -2.04847571973674,
-2.04847571973674, 1.22730660297267, 1.94291846403141, 1.94291846403141,
2.64766715573213, 1.66439852581802, 1.66439852581802, 3.92242045719081,
2.92445832371034, 2.92445832371034, 4.09796304281725)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -216L))
word = ts(df$Fit, start = c(2002, 1), end = c(2019, 12), frequency = 12)
breaks = breakpoints(df$Fit ~ 1, h = 0.1)
plot(breaks)
coef(breaks, breaks = 2)
word[breaks$breakpoints] # July 2014
plot(df[,2], type = "b")
lines(fitted(breaks), col = 4, lwd = 2)
abline(v = df[breaks$breakpoints], lty = 2)
Can anyone help me sort this out?
Thanks a lot!
You created a timeseries object but you did not use it:
plot(word)
breaks <- breakpoints(word ~ 1, h = 0.1)
lines(fitted(breaks), col="blue", lwd=2)
abline(v = time(word)[breaks$breakpoints], lty = 2)
If I remember correctly, for irregularly spaced intervals you use zoo, you can try something like below:
library(strucchange)
library(zoo)
breaks = breakpoints(df$Fit ~ 1, h = 0.1)
zoo_df = as.zoo(df[,2])
time(zoo_df) = df[,1]
plot(zoo_df, type = "b")
abline(v = time(zoo_df)[breaks$breakpoints], lty = 2)
This gets you the plot with date on x-axis. Now to get the x-axis values, you do time(zoo_df) and it's a matter of subsetting this according to the index. For the fitted line, if you don't want the lines to join, you need to split them into breakpoints + 1 groups, and draw each one separately:
#group your breaks
grps = cut(1:length(zoo_df),
breaks=c(0,breaks$breakpoints,+Inf),
labels=1:(length(breaks$breakpoints)+1))
for(i in unique(grps)){
lines(time(zoo_df)[grps==i],fitted(breaks)[grps==i])
}

ggplot and two different geom_line(): the legend does not appear

I have the following code (dput data for the data sets is here):
ruz <- structure(list(date = structure(c(16617, 16618, 16619, 16622,
16623, 16624, 16625, 16626, 16629, 16630, 16631, 16632, 16633,
16636, 16637, 16638, 16639, 16640, 16643, 16644, 16645, 16646,
16647, 16650, 16651, 16652, 16653, 16654, 16657, 16658, 16659,
16660, 16661, 16664, 16665, 16666, 16667, 16668, 16671, 16672,
16673, 16674, 16675, 16678, 16679, 16680, 16681, 16682, 16685,
16686, 16687, 16688, 16689, 16692, 16693, 16694, 16695, 16696,
16699, 16700, 16701, 16702, 16703, 16706, 16707, 16708, 16709,
16710, 16713, 16714, 16715, 16716, 16717, 16720, 16721, 16722,
16723, 16724, 16727, 16728, 16729, 16730, 16731, 16734, 16735,
16736, 16737, 16738, 16741, 16742, 16743, 16744, 16745, 16748,
16749, 16750, 16751, 16752, 16755, 16756, 16757, 16758, 16759,
16762, 16763, 16764, 16765, 16766, 16769), class = "Date"), val1 = c(61.8954,
61.6297, 61.7859, 62.2135, 62.692, 63.026, 63.1511, 63.008, 62.7991,
62.5304, 62.3971, 62.1703, 61.9535, 61.7927, 61.8367, 62.1856,
62.7663, 63.5846, 64.859, 66.0745, 65.9327, 65.1387, 65.8362,
67.9171, 68.8917, 68.7714, 69.295, 69.9932, 70.0878, 70.0563,
71.0985, 71.7451, 71.9923, 72.3836, 72.6186, 72.7895, 74.1316,
76.3577, 79.6818, 80.4601, 79.637, 77.1905, 74.7982, 74.0868,
73.6844, 74.7815, 75.1829, 75.0874, 76.0362, 76.5334, 76.1729,
76.2661, 76.521, 76.5815, 76.1411, 74.7473, 74.229, 74.8073,
74.8083, 74.2189, 73.7976, 74.0765, 73.7323, 73.5319, 73.8853,
73.7351, 73.2462, 73.7254, 73.4657, 72.5227, 70.9683, 70.1357,
69.7459, 69.7823, 70.714, 71.5863, 71.3391, 70.2717, 70.1001,
70.3965, 70.964, 70.901, 69.6083, 69.0542, 70.325, 71.2619, 70.6912,
70.5258, 70.6195, 69.9786, 68.9845, 68.7403, 69.5909, 69.6324,
69.2801, 69.3884, 70.4129, 71.6024, 70.7705, 69.6673, 69.2706,
69.2517, 69.2788, 69.3983, 69.7819, 69.8404, 69.8002, 69.9816,
70.1287)), .Names = c("date", "val1"), row.names = c("2015-07-01",
"2015-07-02", "2015-07-03", "2015-07-06", "2015-07-07", "2015-07-08",
"2015-07-09", "2015-07-10", "2015-07-13", "2015-07-14", "2015-07-15",
"2015-07-16", "2015-07-17", "2015-07-20", "2015-07-21", "2015-07-22",
"2015-07-23", "2015-07-24", "2015-07-27", "2015-07-28", "2015-07-29",
"2015-07-30", "2015-07-31", "2015-08-03", "2015-08-04", "2015-08-05",
"2015-08-06", "2015-08-07", "2015-08-10", "2015-08-11", "2015-08-12",
"2015-08-13", "2015-08-14", "2015-08-17", "2015-08-18", "2015-08-19",
"2015-08-20", "2015-08-21", "2015-08-24", "2015-08-25", "2015-08-26",
"2015-08-27", "2015-08-28", "2015-08-31", "2015-09-01", "2015-09-02",
"2015-09-03", "2015-09-04", "2015-09-07", "2015-09-08", "2015-09-09",
"2015-09-10", "2015-09-11", "2015-09-14", "2015-09-15", "2015-09-16",
"2015-09-17", "2015-09-18", "2015-09-21", "2015-09-22", "2015-09-23",
"2015-09-24", "2015-09-25", "2015-09-28", "2015-09-29", "2015-09-30",
"2015-10-01", "2015-10-02", "2015-10-05", "2015-10-06", "2015-10-07",
"2015-10-08", "2015-10-09", "2015-10-12", "2015-10-13", "2015-10-14",
"2015-10-15", "2015-10-16", "2015-10-19", "2015-10-20", "2015-10-21",
"2015-10-22", "2015-10-23", "2015-10-26", "2015-10-27", "2015-10-28",
"2015-10-29", "2015-10-30", "2015-11-02", "2015-11-03", "2015-11-04",
"2015-11-05", "2015-11-06", "2015-11-09", "2015-11-10", "2015-11-11",
"2015-11-12", "2015-11-13", "2015-11-16", "2015-11-17", "2015-11-18",
"2015-11-19", "2015-11-20", "2015-11-23", "2015-11-24", "2015-11-25",
"2015-11-26", "2015-11-27", "2015-11-30"), class = "data.frame")
dfr <- structure(list(date = structure(c(16616, 16646, 16677, 16708,
16738, 16769), class = "Date"), val2 = c(0, 0.0920000000000001,
0.120392, 0.136077488, 0.0917704659680001, 0.0874033841041282
)), .Names = c("date", "val2"), row.names = c("2015-06", "2015-07",
"2015-08", "2015-09", "2015-10", "2015-11"), class = "data.frame")
ggplot:
ggplot() +
geom_line(data = ruz, aes(date, val1), size = 1.5, color = "blue") +
geom_line(data = dfr, aes(date, val2 * 100), size = 1.5, color = "red") +
scale_fill_manual(values = c("blue", "red"))
which produces the following graph:
My question is, how to modify it to show a legend after all?
If you want to avoid combining the data.frames, you can do this:
ggplot() +
geom_line(data = ruz, aes(date, val1, color = "a"), size = 1.5) +
geom_line(data = dfr, aes(date, val2 * 100, color = "b"), size = 1.5) +
scale_color_manual(name = "Colors",
values = c("a" = "blue", "b" = "red"))
In order to get a legend, you have to map something to color within aes. You can then use scale_color_manual to define the colors for the mapped character values. There are situations where this trick is easier and results in more readable code then reshaping/combining data.
You could rbind them and use color
ruz$type <- "ruz"
dfr$val2 <- dfr$val2 * 100
dfr$type <- "dfr"
names(ruz) <- names(dfr)
df <- rbind(ruz, dfr)
ggplot(df, aes(date, val2, color = type), size = 1.5) + geom_line()

Applying function to specific rows using for loop R

I am having trouble to set correctly the rows that I need to pass to a function. I have "calculating functions" and "row selecting functions". The functions that I use to calculate are doing what I want.
The function must subset first estado=3 (that part goes fine) then subset the correct row numbers and pass them to "the calculating functions".
As I said, my code to select should pick specific row numbers (in this case 1:11, 23:23, 46:56 and 68:78). The thing is I'm trying to make this automatic for any data frame's length. I should get this pattern: rep (11 , 12) where I mean I have drop the first 11 between the first two dfs I pass to the calculating function and drop 12 between the first two and the next block and loop till the data frame is over (see photo to get better undesrstanding).
The row selecting is based on time (you can see that there's a tiempo column so there's a little bit of calculating rows based on times (t_muestreo, t_pasaje, t__entre). I'm trying to set the row numbers based on this times just in case we have to change them in our experiment.
pendientes_ensayo <- function(df){
pendientes = data.frame()
t_muestreo = 0.2
t_pasaje = 2
t_entre = 2.5
nro_filas_salto = (t_pasaje / t_muestreo)
nro_filas_entre = (t_entre/t_muestreo)
df_a_medir_completo = df[df$estado == 3,] # Me quedo solo con la parte que mido
tamanodf = dim(df_a_medir_completo)[1]
df$tiempo = as.numeric( df$tiempo)
cant_animales = length(names(df)) - 2 # tiempo y estado
pos_en_filas = 1
for(nro_ensayo in 1:(tamanodf/ (nro_filas_salto + nro_filas_entre) )) {
print( pos_en_filas )
print( pos_en_filas + nro_filas_salto )
df_a_medir = df_a_medir_completo[pos_en_filas:(pos_en_filas+nro_filas_salto),]
pendientes = rbind(pendientes,obtener_pendientes(df_a_medir))
pos_en_filas = pos_en_filas + nro_filas_salto + nro_filas_entre - 0.5
}
return(pendientes)
}
The return I get (printed rows) is this
[1] 1
[1] 11
[1] 23
[1] 33
[1] 45
[1] 55
[1] 67
[1] 77
Note that the first 2 subsets (1:11 and 23:33 are correct) but 45:55 and 67:77 are one row out of phase.
In order to run the pendientes_ensayo function you will need
iti = 153
t_adaptacion = 600
t_pre_estimulo = 18
t_muestreo = 0.2
t_pasaje = 2
t_entre = 2.5
defasaje = 1
nro_ensayos = 2
regresion <- function(formula){
regresion = lm( formula )
ss = summary(regresion)
return(c( coef(regresion)[2] , ss$adj.r.squared))
}
sacar_columnas <- function (dataframe,columnas){
return(dataframe[,!(names(dataframe) %in% columnas)])
}
desplazar<- function(tiempo,dataframe){
matriz = as.matrix(dataframe)
matriz_dif = abs(diff(matriz )) # derivar
matriz_dif_inv = diffinv(matriz_dif) # integrar
return(data.frame(cbind(tiempo, matriz_dif_inv )))
}
obtener_pendientes <- function(df_a_medir){
df_a_medir2 =sacar_columnas(df_a_medir, c("estado","tiempo"))
mat_desplazamiento = desplazar(df_a_medir$tiempo,df_a_medir2)
pend_muestreo = apply(mat_desplazamiento,2,function(x) regresion(x~df_a_medir$tiempo))
return(pend_muestreo)
}
Here's an example of a data frame that has been already subseted by df$estado=3
dput(d)
structure(list(estado = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), tiempo = c(618.2,
618.4, 618.6, 618.8, 619, 619.2, 619.4, 619.6, 619.8, 620, 620.2,
620.4, 620.6, 620.8, 621, 621.2, 621.4, 621.6, 621.8, 622, 622.2,
622.4, 622.6, 622.8, 623, 623.2, 623.4, 623.6, 623.8, 624, 624.2,
624.4, 624.6, 624.8, 625, 625.2, 625.4, 625.6, 625.8, 626, 626.2,
626.4, 626.6, 626.8, 627, 3138.2, 3138.4, 3138.6, 3138.8, 3139,
3139.2, 3139.4, 3139.6, 3139.8, 3140, 3140.2, 3140.4, 3140.6,
3140.8, 3141, 3141.2, 3141.4, 3141.6, 3141.8, 3142, 3142.2, 3142.4,
3142.6, 3142.8, 3143, 3143.2, 3143.4, 3143.6, 3143.8, 3144, 3144.2,
3144.4, 3144.6, 3144.8, 3145, 3145.2, 3145.4, 3145.6, 3145.8,
3146, 3146.2, 3146.4, 3146.6, 3146.8, 3147), UT2x45 = c(14663,
14662, 14661, 14659, 14657, 14654, 14652, 14650, 14651, 14651,
14651, 14650, 14648, 14647, 14645, 14644, 14641, 14640, 14640,
14640, 14640, 14640, 14639, 14639, 14638, 14638, 14637, 14637,
14637, 14636, 14636, 14635, 14634, 14632, 14631, 14629, 14628,
14628, 14626, 14625, 14625, 14625, 14624, 14624, 14624, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888), TR2x45 = c(14663,
14663, 14655, 14634, 14632, 14634, 14634, 14632, 14633, 14605,
14573, 14555, 14548, 14540, 14525, 14523, 14517, 14510, 14506,
14499, 14497, 14494, 14492, 14487, 14453, 14426, 14414, 14416,
14413, 14413, 14416, 14409, 14387, 14380, 14367, 14364, 14356,
14347, 14347, 14343, 14337, 14332, 14330, 14329, 14327, 13432,
13432, 13427, 13424, 13426, 13427, 13427, 13427, 13428, 13428,
13426, 13424, 13423, 13423, 13423, 13423, 13423, 13422, 13421,
13421, 13420, 13419, 13418, 13417, 13410, 13410, 13410, 13411,
13411, 13411, 13411, 13411, 13410, 13410, 13410, 13410, 13410,
13411, 13411, 13411, 13411, 13411, 13411, 13411, 13411), UT2x45.2 = c(15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282), TR2x45.2 = c(17295,
17295, 17293, 17266, 17248, 17250, 17268, 17277, 17252, 17195,
17141, 17115, 17108, 17105, 17102, 17099, 17098, 17097, 17093,
17089, 17086, 17084, 17084, 17083, 17080, 17036, 16975, 16919,
16884, 16869, 16836, 16788, 16738, 16700, 16675, 16661, 16653,
16648, 16643, 16638, 16634, 16633, 16631, 16629, 16626, 24047,
24047, 24047, 24047, 24047, 24062, 24085, 24119, 24163, 24191,
24181, 24182, 24181, 24183, 24183, 24183, 24183, 24183, 24183,
24183, 24183, 24184, 24184, 24184, 24182, 24137, 24070, 24011,
23978, 23965, 23920, 23868, 23826, 23806, 23799, 23796, 23788,
23782, 23776, 23774, 23772, 23769, 23766, 23764, 23761), TR2x45.3 = c(14702,
14702, 14702, 14702, 14702, 14702, 14702, 14704, 14720, 14733,
14739, 14741, 14743, 14744, 14746, 14750, 14753, 14754, 14755,
14756, 14756, 14758, 14759, 14760, 14760, 14760, 14763, 14767,
14770, 14774, 14773, 14774, 14775, 14775, 14776, 14776, 14776,
14776, 14776, 14776, 14776, 14776, 14776, 14776, 14776, 14818,
14818, 14818, 14818, 14818, 14820, 14819, 14818, 14819, 14819,
14819, 14819, 14819, 14819, 14819, 14819, 14819, 14819, 14819,
14819, 14819, 14819, 14819, 14819, 14817, 14816, 14816, 14816,
14816, 14816, 14817, 14817, 14817, 14817, 14817, 14817, 14817,
14817, 14817, 14817, 14817, 14817, 14817, 14817, 14817), UT2x45.3 = c(18521,
18518, 18517, 18518, 18517, 18515, 18513, 18512, 18510, 18507,
18505, 18503, 18501, 18499, 18499, 18497, 18496, 18495, 18493,
18492, 18490, 18487, 18483, 18481, 18479, 18476, 18474, 18472,
18471, 18470, 18467, 18464, 18461, 18457, 18455, 18453, 18451,
18449, 18447, 18445, 18442, 18438, 18435, 18432, 18430, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204), TR2x45.1 = c(14052,
14049, 14013, 13961, 13907, 13874, 13861, 13844, 13801, 13742,
13680, 13627, 13585, 13563, 13547, 13536, 13524, 13511, 13497,
13487, 13477, 13466, 13457, 13446, 13406, 13352, 13295, 13255,
13248, 13237, 13211, 13163, 13110, 13066, 13034, 13015, 13000,
12980, 12969, 12958, 12944, 12931, 12927, 12919, 12910, 11396,
11394, 11355, 11301, 11247, 11210, 11198, 11195, 11164, 11130,
11081, 11038, 11011, 11003, 10991, 10982, 10974, 10966, 10960,
10954, 10949, 10944, 10941, 10934, 10892, 10839, 10789, 10748,
10729, 10722, 10697, 10656, 10611, 10574, 10547, 10534, 10525,
10516, 10509, 10502, 10494, 10489, 10485, 10482, 10476), UT2x45.1 = c(17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17077,
17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077,
17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077,
17077, 17077, 17077, 17078, 17078, 17080, 17083, 17084, 17084,
17084, 17085, 17087, 17089, 17090, 17093, 17095, 17095, 17096,
17098, 17100, 17101, 17103, 17106, 17107, 17109, 17111), UT2x45.4 = c(16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16823,
16824, 16825, 16825, 16825, 16825, 16825, 16826, 16826, 16826,
16826, 16826, 16826, 16826, 16824, 16824, 16824, 16824, 16826,
16827, 16828, 16828, 16828, 16828, 16829, 16829, 16830, 16831,
16832, 16832, 16832, 16832, 16833, 16833, 16834, 16835, 16834,
16833, 16833, 16833, 16833, 16834, 16834, 16835, 16836), TR2x45.4 = c(12488,
12488, 12486, 12471, 12448, 12435, 12430, 12423, 12418, 12370,
12307, 12247, 12190, 12136, 12095, 12069, 12045, 12025, 12015,
12001, 11994, 11985, 11978, 11967, 11931, 11885, 11817, 11761,
11714, 11667, 11624, 11574, 11519, 11475, 11438, 11405, 11375,
11354, 11330, 11313, 11303, 11284, 11272, 11265, 11255, 9056,
9056, 9056, 9056, 9056, 9056, 9056, 9056, 9054, 9039, 9006, 8994,
8983, 8969, 8968, 8964, 8957, 8950, 8951, 8949, 8943, 8936, 8935,
8934, 8920, 8885, 8841, 8808, 8778, 8752, 8715, 8654, 8595, 8549,
8524, 8500, 8488, 8477, 8461, 8458, 8447, 8441, 8436, 8426, 8421
), UT2x45.1.1 = c(12004, 12004, 12004, 12004, 12004, 12004, 12004,
12004, 12003, 12003, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015), TR2x45.1.1 = c(20832, 20832, 20828, 20818, 20822,
20855, 20873, 20895, 20930, 20915, 20904, 20904, 20904, 20904,
20904, 20904, 20904, 20904, 20905, 20906, 20907, 20908, 20911,
20916, 20918, 20887, 20857, 20861, 20879, 20892, 20910, 20897,
20893, 20894, 20893, 20893, 20893, 20893, 20893, 20893, 20893,
20893, 20893, 20893, 20893, 22371, 22371, 22370, 22369, 22383,
22410, 22440, 22459, 22470, 22426, 22414, 22406, 22404, 22404,
22404, 22403, 22402, 22402, 22402, 22402, 22402, 22402, 22402,
22402, 22405, 22387, 22371, 22373, 22387, 22390, 22364, 22327,
22303, 22295, 22283, 22272, 22264, 22253, 22250, 22240, 22234,
22232, 22230, 22223, 22214), TR2x45.5 = c(11994, 11992, 11991,
11992, 11994, 11994, 11992, 11989, 11991, 11988, 11981, 11978,
11978, 11977, 11974, 11971, 11971, 11969, 11962, 11955, 11952,
11948, 11945, 11943, 11942, 11942, 11942, 11942, 11941, 11939,
11938, 11938, 11931, 11928, 11927, 11925, 11920, 11917, 11915,
11914, 11909, 11906, 11904, 11901, 11897, 3997, 3997, 3997, 3997,
3997, 3997, 3997, 3997, 3997, 3997, 3998, 3998, 3998, 3998, 3998,
3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998,
3998, 3998, 3998, 3999, 3999, 3999, 3999, 3999, 3999, 3999, 3999,
3999, 3999, 3999, 3999, 3999, 3999, 3999, 3999), TR2x45.1.2 = c(12363,
12363, 12352, 12332, 12333, 12330, 12329, 12329, 12329, 12318,
12298, 12296, 12289, 12286, 12277, 12264, 12264, 12261, 12254,
12252, 12249, 12248, 12246, 12244, 12244, 12240, 12227, 12224,
12220, 12219, 12218, 12215, 12206, 12200, 12197, 12197, 12196,
12196, 12195, 12191, 12187, 12183, 12182, 12179, 12177, 11923,
11922, 11921, 11914, 11913, 11913, 11913, 11913, 11913, 11905,
11904, 11903, 11903, 11903, 11902, 11902, 11901, 11901, 11901,
11901, 11901, 11900, 11900, 11899, 11898, 11891, 11880, 11878,
11875, 11873, 11871, 11870, 11866, 11864, 11861, 11859, 11857,
11856, 11855, 11854, 11853, 11852, 11851, 11849, 11848), UT2x45.5 = c(12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462), UT2x45.1.2 = c(12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580
), UT2x45.6 = c(15564, 15563, 15562, 15562, 15563, 15563, 15563,
15563, 15562, 15562, 15560, 15554, 15544, 15542, 15541, 15540,
15539, 15538, 15538, 15537, 15536, 15535, 15533, 15529, 15528,
15525, 15524, 15521, 15519, 15517, 15516, 15516, 15515, 15515,
15514, 15514, 15513, 15510, 15508, 15505, 15502, 15501, 15500,
15498, 15496, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438), UT2x45.1.3 = c(18966, 18966, 18966, 18966, 18966,
18966, 18966, 18966, 18966, 18966, 18966, 18966, 18966, 18966,
18966, 18966, 18966, 18966, 18966, 18966, 18967, 18968, 18968,
18969, 18970, 18970, 18971, 18971, 18971, 18972, 18973, 18974,
18974, 18975, 18976, 18977, 18977, 18978, 18978, 18978, 18978,
18979, 18980, 18980, 18981, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189), TR2x45.6 = c(15834, 15834, 15834,
15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834,
15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834,
15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834,
15834, 15834, 15834, 15835, 15835, 15835, 15835, 15835, 15835,
15835, 15835, 15835, 15835, 15835, 15835, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775), TR2x45.1.3 = c(19716,
19716, 19716, 19716, 19716, 19718, 19718, 19718, 19720, 19722,
19723, 19723, 19723, 19723, 19723, 19723, 19722, 19722, 19722,
19723, 19723, 19723, 19724, 19724, 19725, 19726, 19728, 19730,
19732, 19733, 19736, 19737, 19737, 19737, 19737, 19738, 19738,
19738, 19738, 19738, 19738, 19738, 19738, 19738, 19738, 27720,
27720, 27720, 27720, 27720, 27720, 27720, 27720, 27720, 27720,
27720, 27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719,
27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719,
27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719,
27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719)), .Names = c("estado",
"tiempo", "UT2x45", "TR2x45", "UT2x45.2", "TR2x45.2", "TR2x45.3",
"UT2x45.3", "TR2x45.1", "UT2x45.1", "UT2x45.4", "TR2x45.4", "UT2x45.1.1",
"TR2x45.1.1", "TR2x45.5", "TR2x45.1.2", "UT2x45.5", "UT2x45.1.2",
"UT2x45.6", "UT2x45.1.3", "TR2x45.6", "TR2x45.1.3"), row.names = c("3087",
"3088", "3089", "3090", "3091", "3092", "3093", "3094", "3095",
"3096", "3097", "3098", "3099", "3100", "3101", "3102", "3103",
"3104", "3105", "3106", "3107", "3108", "3109", "3110", "3111",
"3112", "3113", "3114", "3115", "3116", "3117", "3118", "3119",
"3120", "3121", "3122", "3123", "3124", "3125", "3126", "3127",
"3128", "3129", "3130", "3131", "15687", "15688", "15689", "15690",
"15691", "15692", "15693", "15694", "15695", "15696", "15697",
"15698", "15699", "15700", "15701", "15702", "15703", "15704",
"15705", "15706", "15707", "15708", "15709", "15710", "15711",
"15712", "15713", "15714", "15715", "15716", "15717", "15718",
"15719", "15720", "15721", "15722", "15723", "15724", "15725",
"15726", "15727", "15728", "15729", "15730", "15731"), class = "data.frame")
I wish I've made myself clear I think there must be a simpler way to do this. Thank you.
I think the for loop is just subsetting and applying the function ibtener_pendientes. If that is the case you can use this:
## create list of dfs based on row idx
idx <- list(1:11, 23:33, 45:56, 67:78)
## or write a function to automate the rows
# idx <- lapply(seq(1, nrow(d), by = 10 + 12), function(x) seq(x, x + 11))
l <- lapply(idx, function(x) d[x, ])
do.call('rbind', Map('obtener_pendientes', l))
# tiempo V2 V3 V4 V5 V6
# df_a_medir$tiempo 1 8.2272727 37.9090909 0 95.5909091 16.6818182
# 1 0.9495273 0.7936207 NaN 0.8618870 0.5851504
# df_a_medir$tiempo 1 2.3181818 50.3181818 0 184.8636364 10.6363636
# 1 0.9436334 0.8228418 NaN 0.9712757 0.9353285
# df_a_medir$tiempo 1 0.0000000 4.4220779 0 51.0649351 1.4675325
# 1 NaN 0.8718034 NaN 0.7925802 0.6962431
# df_a_medir$tiempo 1 0.0000000 4.0000000 0 194.3636364 1.9090909
# 1 NaN 0.5621631 NaN 0.9699970 0.7561455
# V7 V8 V9 V10 V11
# df_a_medir$tiempo 8.4090909 182.0000000 0.0000000 0.0000000 77.0454545
# 0.9803865 0.9748662 NaN NaN 0.8338448
# df_a_medir$tiempo 10.4090909 169.5000000 0.0000000 0.0000000 242.1363636
# 0.9862105 0.9684383 NaN NaN 0.9918088
# df_a_medir$tiempo 0.0000000 120.2272727 0.0000000 1.6818182 36.2532468
# NaN 0.9357959 NaN 0.8381040 0.8932908
# df_a_medir$tiempo 0.0000000 165.5000000 5.8636364 2.6363636 171.0909091
# NaN 0.9730404 0.9418502 0.9271605 0.9658712
# V12 V13 V14 V15 V16 V17 V18
# df_a_medir$tiempo 0.7727273 86.1363636 9.8636364 27.8181818 0 0 2.2727273
# 0.5311111 0.9337161 0.8997295 0.8450071 NaN NaN 0.8739165
# df_a_medir$tiempo 0.0000000 77.2727273 4.9545455 20.1363636 0 0 9.0454545
# NaN 0.9727717 0.7449921 0.9362205 NaN NaN 0.9558411
# df_a_medir$tiempo 0.0000000 52.7532468 0.3571429 5.7337662 0 0 0.0000000
# NaN 0.8280456 0.7368421 0.8605300 NaN NaN NaN
# df_a_medir$tiempo 0.0000000 68.8181818 0.6363636 18.5454545 0 0 0.0000000
# NaN 0.9035839 0.6666667 0.9262417 NaN NaN NaN
# V19 V20 V21
# df_a_medir$tiempo 0.0000000 0 3.5000000
# NaN NaN 0.8115468
# df_a_medir$tiempo 2.8636364 0 7.6363636
# 0.9541063 NaN 0.9702682
# df_a_medir$tiempo 0.0000000 0 0.3571429
# NaN NaN 0.7368421
# df_a_medir$tiempo 0.0000000 0 0.0000000
# NaN NaN NaN
which looks similar to the output I got from your loop function
helper functions:
(didnt have enough room to add the data d here)
regresion <- function(formula){
regresion = lm( formula )
ss = summary(regresion)
return(c( coef(regresion)[2] , ss$adj.r.squared))
}
sacar_columnas <- function (dataframe,columnas){
return(dataframe[,!(names(dataframe) %in% columnas)])
}
desplazar<- function(tiempo,dataframe){
matriz = as.matrix(dataframe)
matriz_dif = abs(diff(matriz )) # derivar
matriz_dif_inv = diffinv(matriz_dif) # integrar
return(data.frame(cbind(tiempo, matriz_dif_inv )))
}
obtener_pendientes <- function(df_a_medir){
df_a_medir2 =sacar_columnas(df_a_medir, c("estado","tiempo"))
mat_desplazamiento = desplazar(df_a_medir$tiempo,df_a_medir2)
pend_muestreo = apply(mat_desplazamiento,2,function(x) regresion(x~df_a_medir$tiempo))
return(pend_muestreo)
}

Resources