Applying function to specific rows using for loop R - 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)
}

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.

format dates as quarters in ggplot2 x axis

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"))

why is the result of eigen_centrality function in igraph different each time it is executed

I want to compute eigen centrality measure on the network of my transaction data. However, each time the eigen_centrality function in igraph is run, it creates different results for the nodes.
Here is the code:
relations <- data.frame(from = data$from,
to = data$to)
network <- graph_from_data_frame(relations, directed = T)
E(network)$weight <- log(data$amount + 1.1) # weight of the edges
V(network)$eigenCentrality <- eigen_centrality(network, scale = TRUE)$vector
network_data <- igraph::as_data_frame(network, what = "vertices")
head(network_data)
the result of this code above creates a different result each time it is run.
the result of a run:
name eigenCentrality
<chr> <dbl>
5695 5695 0.00000000000000004061352
9379 9379 0.00000000000000001507577
8170 8170 0.00000000000000002865109
9845 9845 0.00000000000000000000000
9640 9640 0.00000000000000000000000
5815 5815 0.00000000000000000000000
the result of another run:
name eigenCentrality
<chr> <dbl>
5695 5695 0.0000000000000000000000
9379 9379 0.0000000000000000000000
8170 8170 0.0000000000000000000000
9845 9845 0.0000000000000002674064
9640 9640 0.0000000000000000000000
5815 5815 0.0000000000000011516567
What might possibly causes this?
Reproducible example:
structure(list(id = 0:499, from = c("5695", "9379", "8170", "9845",
"9640", "5815", "8583", "8314", "8865", "6530", "9155", "7536",
"7085", "5829", "9606", "6795", "5573", "7874", "9409", "5454",
"9318", "8763", "9030", "9280", "5517", "9387", "9810", "9056",
"7300", "7313", "7518", "5601", "7549", "7841", "8888", "5808",
"9545", "9460", "8053", "6615", "8846", "7280", "8265", "6934",
"8930", "6917", "7889", "7653", "6262", "7222", "7907", "6203",
"9098", "7278", "6312", "8844", "8822", "9742", "8687", "9174",
"5939", "7424", "6797", "7564", "8671", "6415", "5610", "6052",
"9152", "7568", "5503", "7869", "7148", "6686", "6223", "7343",
"8990", "8027", "5673", "6718", "5858", "7001", "9529", "8708",
"7603", "7468", "7601", "9868", "5755", "9655", "8321", "9110",
"5726", "6393", "7425", "5803", "6268", "9723", "7682", "6839",
"7063", "8046", "9558", "8628", "7719", "7754", "6292", "6306",
"9069", "6329", "8238", "9793", "8178", "5538", "8305", "7227",
"9207", "9694", "6194", "7559", "5373", "8235", "8280", "6252",
"6646", "5781", "5568", "6234", "5588", "6015", "6253", "5400",
"5954", "8518", "6829", "6660", "7210", "6351", "6147", "5907",
"6504", "7344", "7584", "7989", "7265", "7207", "9620", "6022",
"6733", "6210", "8631", "6222", "7070", "7313", "9524", "5640",
"5649", "9291", "7617", "8727", "9152", "6794", "6502", "7259",
"9723", "5621", "7055", "7151", "8594", "8980", "7264", "6526",
"8781", "7984", "6114", "9408", "9115", "6140", "8537", "7370",
"7744", "8551", "5562", "6354", "7783", "5443", "9161", "5668",
"9249", "8422", "5409", "9216", "7253", "5921", "8689", "7869",
"6329", "5836", "9669", "8708", "6359", "8239", "9021", "8469",
"9135", "6043", "5776", "6770", "7725", "5769", "7502", "6284",
"9811", "6830", "7047", "5608", "8282", "9047", "8708", "8768",
"9398", "8532", "9048", "7195", "7812", "7568", "7528", "5957",
"8233", "6312", "7039", "6874", "7047", "8390", "9843", "8906",
"5952", "6772", "5774", "5773", "5816", "7664", "6600", "7093",
"6454", "6487", "6934", "5585", "6358", "5876", "8266", "6794",
"6361", "6799", "7839", "8047", "6390", "8423", "7247", "7534",
"8870", "8756", "8880", "9772", "6989", "6248", "9161", "6374",
"7893", "8228", "6514", "9135", "7974", "8395", "8361", "9027",
"8456", "6616", "7035", "9047", "8532", "7488", "7382", "6600",
"6784", "9597", "9075", "6228", "8708", "8736", "6442", "9137",
"5573", "8168", "9803", "7650", "7290", "6410", "5409", "9450",
"9201", "7728", "7049", "6986", "9084", "7955", "8336", "9457",
"9676", "8078", "6540", "8757", "7084", "9811", "9161", "7492",
"5566", "8872", "5931", "9161", "5722", "8502", "6836", "6648",
"9696", "9498", "7247", "8317", "8389", "6375", "7109", "8304",
"6463", "8894", "8741", "9719", "8963", "5915", "7336", "7802",
"8374", "5922", "7680", "8598", "6634", "6765", "7880", "9855",
"7653", "8683", "8640", "8853", "9277", "8105", "9819", "8472",
"8713", "6509", "7503", "7069", "8787", "9251", "9744", "8912",
"6502", "7614", "6669", "7603", "6749", "7860", "9589", "5715",
"8130", "6143", "6129", "8937", "9075", "8660", "6883", "8599",
"6634", "9723", "7494", "6417", "5650", "9613", "7021", "7996",
"5784", "5885", "7390", "7247", "6230", "9398", "7495", "8512",
"8456", "9684", "9802", "5418", "8668", "5605", "9803", "9813",
"8206", "9398", "6022", "9251", "8783", "8723", "8713", "7321",
"9610", "6334", "5866", "7792", "8422", "5562", "5512", "7711",
"9632", "5896", "8472", "8945", "8982", "7047", "7057", "8809",
"9630", "6479", "5433", "8933", "9839", "8735", "6153", "6042",
"7899", "9111", "7332", "5473", "8869", "5418", "7770", "6883",
"9803", "7598", "5831", "9750", "6099", "8982", "5433", "7049",
"7293", "8945", "6551", "8763", "9228", "8739", "9315", "5644",
"7718", "8008", "8239", "6794", "8499", "7854", "8041", "9617",
"9525", "8423", "5576", "9545", "6997", "6600", "6138", "9657",
"8266", "7631", "8785", "8945", "6375", "8615", "7271", "7326",
"9652", "5999", "5976", "9392", "8118", "9580", "9046", "7517",
"6407", "8934", "7577", "6510", "5727", "9840", "9350", "9369"
), to = c("7468", "6213", "7517", "6143", "6254", "6669", "6934",
"9240", "6374", "6143", "8105", "9131", "9770", "6217", "6454",
"6375", "9565", "9723", "8336", "8078", "9202", "9333", "7518",
"6503", "9839", "8472", "6673", "8671", "6795", "6540", "9845",
"9240", "8323", "6616", "6671", "6669", "8657", "7471", "5517",
"9744", "8408", "5605", "6671", "7154", "9664", "6053", "8368",
"9664", "7483", "6502", "7772", "7471", "7243", "6143", "7253",
"6153", "8820", "6951", "6771", "9750", "9161", "9667", "8876",
"7885", "6155", "9161", "5648", "6404", "9573", "8846", "6359",
"5997", "9744", "9682", "8708", "8665", "8678", "6947", "8168",
"6912", "9012", "7345", "7564", "9793", "5483", "8278", "8606",
"9493", "5635", "7604", "6794", "8945", "6510", "6299", "7192",
"9741", "9664", "8323", "7222", "5732", "6273", "9202", "8757",
"7568", "9602", "8763", "8168", "7468", "7109", "8611", "5643",
"9111", "5925", "7406", "6331", "6093", "7990", "8790", "9468",
"6243", "9631", "9676", "8945", "7057", "7672", "6429", "9277",
"9628", "5837", "8551", "9251", "7624", "7143", "9602", "6673",
"7155", "8248", "6566", "6669", "7047", "9457", "8741", "8739",
"7483", "6934", "9042", "5885", "7525", "8906", "6442", "8008",
"6795", "8594", "7604", "8239", "6600", "6883", "9228", "6299",
"8769", "6634", "5570", "8180", "8368", "8782", "7714", "6986",
"9135", "8128", "9488", "9202", "8567", "6460", "5468", "6192",
"8763", "6526", "6011", "7825", "9811", "9065", "5411", "8044",
"5873", "7785", "6876", "7406", "8790", "6042", "5921", "8783",
"6025", "7710", "8483", "6837", "7317", "7675", "7214", "9837",
"6523", "5503", "9815", "8726", "5506", "6422", "9137", "5582",
"8952", "9827", "6164", "8558", "9202", "9690", "6153", "7790",
"8368", "9573", "9501", "7707", "7598", "9648", "5614", "8220",
"6646", "6874", "7869", "9744", "7345", "8787", "5605", "7518",
"6411", "6560", "6613", "5408", "5968", "8967", "7929", "5604",
"6340", "6334", "8078", "8939", "7411", "6142", "9845", "9499",
"7929", "6883", "6114", "7447", "9552", "9555", "8483", "8345",
"8982", "5925", "6215", "9313", "7518", "6851", "6530", "6422",
"7915", "8870", "7153", "6530", "9469", "6874", "7555", "9202",
"9372", "8757", "6560", "9369", "5468", "9717", "9348", "8947",
"7920", "7718", "7155", "6997", "9667", "9811", "6400", "9012",
"5849", "8674", "7895", "7406", "8429", "6014", "9037", "7017",
"7990", "8095", "6938", "5755", "5468", "9802", "8941", "9084",
"7718", "9573", "7878", "7941", "5732", "9777", "7564", "8667",
"8975", "7304", "9828", "5548", "6831", "5468", "6442", "9137",
"6560", "9676", "9111", "9802", "5837", "6025", "8422", "7449",
"9251", "8323", "7170", "9862", "6131", "6305", "7122", "8611",
"9558", "6634", "8130", "8967", "6774", "5641", "7879", "6517",
"8105", "8129", "6771", "6866", "6994", "9085", "9398", "8912",
"9729", "8772", "5695", "5605", "8551", "6056", "7316", "6333",
"8326", "6108", "6651", "9027", "7532", "8985", "8412", "9667",
"6417", "5605", "9747", "5582", "7635", "5842", "8282", "9861",
"6392", "9746", "8770", "6689", "8134", "9244", "7585", "9623",
"9364", "5692", "9558", "5794", "7192", "5741", "5610", "9741",
"8038", "5614", "7772", "6634", "6020", "7397", "8420", "5374",
"6581", "5874", "5827", "9291", "5751", "8405", "7096", "9102",
"7565", "7735", "7214", "7171", "8409", "7990", "5753", "9832",
"9602", "7928", "7496", "8790", "6168", "8178", "7293", "7506",
"8648", "9777", "7191", "8310", "9450", "9570", "8743", "9454",
"8461", "7599", "8312", "5728", "7255", "8523", "6186", "7534",
"8053", "9522", "8982", "8522", "9387", "7208", "6325", "5610",
"9493", "5837", "6843", "7122", "8870", "7034", "5789", "6798",
"6525", "9381", "6700", "6146", "8830", "6338", "9793", "7785",
"8309", "6359", "7214", "8447", "8017", "9772", "9701", "9366",
"9847", "5905", "6045", "5517", "8416", "8456", "6600", "8280",
"9221", "5495", "7668", "8329", "6114", "7328", "6311", "7151",
"8906", "9723", "9135", "9481", "6951", "8967", "6972", "6807",
"6735", "8310", "5692", "8142", "9493"), date = structure(c(12787,
12791, 12793, 12795, 12797, 12803, 12807, 12809, 12813, 12814,
12814, 12816, 12817, 12819, 12820, 12821, 12822, 12822, 12823,
12824, 12825, 12826, 12827, 12828, 12830, 12832, 12834, 12837,
12839, 12840, 12842, 12842, 12842, 12842, 12842, 12842, 12842,
12842, 12845, 12846, 12847, 12847, 12848, 12849, 12849, 12850,
12850, 12851, 12851, 12852, 12852, 12853, 12854, 12854, 12855,
12855, 12856, 12856, 12857, 12859, 12861, 12862, 12864, 12865,
12867, 12868, 12869, 12869, 12870, 12872, 12873, 12873, 12873,
12873, 12873, 12873, 12873, 12873, 12873, 12873, 12873, 12873,
12873, 12875, 12876, 12877, 12878, 12878, 12879, 12879, 12880,
12880, 12880, 12880, 12881, 12882, 12882, 12882, 12883, 12883,
12884, 12884, 12885, 12885, 12885, 12886, 12886, 12886, 12887,
12887, 12888, 12889, 12890, 12891, 12892, 12893, 12894, 12895,
12896, 12897, 12898, 12899, 12900, 12901, 12902, 12903, 12903,
12903, 12903, 12903, 12903, 12903, 12903, 12903, 12903, 12903,
12903, 12903, 12903, 12903, 12903, 12903, 12903, 12903, 12905,
12905, 12906, 12907, 12908, 12908, 12908, 12909, 12909, 12909,
12909, 12910, 12910, 12910, 12910, 12911, 12911, 12912, 12912,
12912, 12912, 12913, 12913, 12913, 12914, 12914, 12914, 12915,
12915, 12915, 12915, 12916, 12916, 12916, 12916, 12917, 12917,
12917, 12918, 12918, 12919, 12920, 12921, 12921, 12922, 12923,
12924, 12924, 12925, 12926, 12927, 12927, 12928, 12929, 12929,
12930, 12931, 12931, 12932, 12934, 12934, 12934, 12934, 12934,
12934, 12934, 12934, 12934, 12934, 12934, 12934, 12934, 12934,
12934, 12934, 12934, 12934, 12934, 12934, 12934, 12934, 12934,
12934, 12934, 12934, 12934, 12934, 12935, 12936, 12936, 12937,
12938, 12938, 12939, 12939, 12939, 12939, 12939, 12940, 12940,
12940, 12940, 12941, 12941, 12941, 12941, 12941, 12942, 12942,
12942, 12942, 12942, 12942, 12943, 12943, 12943, 12943, 12943,
12944, 12944, 12944, 12944, 12945, 12945, 12945, 12945, 12945,
12946, 12946, 12946, 12946, 12946, 12946, 12947, 12947, 12947,
12947, 12947, 12947, 12948, 12948, 12948, 12948, 12949, 12949,
12950, 12950, 12951, 12951, 12951, 12952, 12952, 12952, 12953,
12954, 12954, 12955, 12955, 12956, 12956, 12957, 12957, 12958,
12959, 12959, 12959, 12960, 12961, 12961, 12962, 12963, 12963,
12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964,
12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964,
12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964,
12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964, 12964,
12964, 12964, 12964, 12964, 12965, 12966, 12966, 12967, 12967,
12967, 12968, 12969, 12969, 12969, 12969, 12969, 12969, 12970,
12970, 12970, 12970, 12970, 12970, 12970, 12971, 12971, 12971,
12971, 12971, 12971, 12971, 12972, 12972, 12972, 12972, 12972,
12972, 12972, 12973, 12973, 12973, 12973, 12973, 12973, 12973,
12974, 12974, 12974, 12974, 12974, 12975, 12975, 12975, 12975,
12975, 12975, 12976, 12976, 12976, 12976, 12976, 12976, 12976,
12976, 12977, 12977, 12977, 12977, 12977, 12977, 12977, 12978,
12978, 12978, 12978, 12978, 12978, 12979, 12979, 12980, 12980,
12981, 12981, 12982, 12982, 12982, 12983, 12983, 12984, 12984,
12984, 12985, 12986, 12986, 12987, 12987, 12988, 12988, 12989,
12989, 12989, 12990, 12990, 12991, 12991, 12992, 12992, 12993,
12993, 12994, 12994, 12995, 12995, 12995, 12995, 12995, 12995,
12995, 12995, 12995, 12995, 12995, 12995, 12995, 12995, 12995,
12995, 12995, 12995, 12995, 12995, 12995, 12995, 12995, 12995,
12995, 12995, 12995, 12995, 12995, 12995, 12995, 12995, 12995,
12995, 12995, 12995, 12995), class = "Date"), amount = c(700,
11832, 1000, 4276, 200, 200, 49752, 19961, 1000, 13.4, 42.7,
200, 800, 5224, 5859, 3242, 200, 800, 6242, 1200, 22349, 17477,
9000, 20567, 1000, 6600, 500, 3100, 300, 1100, 18.7, 102, 2.9,
3, 39285, 52.1, 34.1, 18.5, 1000, 37305, 8900, 7618, 7597, 9612,
10700, 200, 1000, 7041, 300, 12200, 5922, 5810, 2901, 4276, 15263,
5718, 3058, 10737, 500, 16549, 200, 900, 3100, 4300, 7700, 23851,
700, 400, 600, 13764, 6.1, 21.7, 157.9, 54, 110.9, 70.4, 140.2,
14500, 39.3, 30.1, 259.3, 154.9, 87.6, 35400, 12300, 5100, 4807,
3401, 3375, 6934, 11584, 5404, 5298, 6932, 6559, 18500, 7041,
2400, 12015, 900, 24863, 22349, 6614, 5582, 37346, 18729, 20203,
6959, 6148, 22800, 800, 800, 6700, 300, 8500, 6700, 45670, 800,
12100, 9400, 25700, 600, 400, 6700, 9300, 37.6, 152.2, 182.1,
225.2, 165.7, 187.8, 115.8, 157.6, 96.1, 112.9, 167, 24.7, 161.8,
243.6, 196.7, 331.1, 28.7, 190.3, 121, 42538, 13500, 32800, 500,
400, 3191, 10000, 43493, 22054, 6934, 23638, 7600, 3395, 12368,
6932, 46096, 1300, 6400, 3300, 4939, 10200, 8100, 8905, 12200,
9700, 300, 22349, 21971, 4507, 25317, 4327, 18729, 3662, 9222,
16646, 4470, 1600, 20400, 35000, 29414, 1000, 9900, 5900, 1500,
3700, 15300, 20700, 2600, 13200, 200, 20600, 12200, 5700, 400,
3200, 6900, 3600, 16100, 4700, 69.9, 48.7, 211.2, 14.6, 56400,
197.7, 25.8, 181.2, 254.5, 14.6, 29, 14.6, 105.8, 17.4, 52600,
14.6, 70.4, 14.6, 277.7, 83.4, 25391, 43.5, 306.9, 195.8, 347.8,
150.4, 121.6, 131.9, 27900, 13300, 1100, 600, 6000, 26365, 3600,
6280, 169, 5029, 29902.5, 30100, 53100, 8300, 6825, 9612, 22998,
3395, 4042, 500, 15200, 12300, 3390, 800, 500, 5842, 2400, 3978,
15876, 33132, 600, 3756, 3008, 28365, 13900, 17362, 2800, 33790,
43352, 33524, 5400, 6614, 2552, 9100, 37976, 1474, 1500, 32329.5,
4600, 5355, 22444, 3334, 52500, 4470, 4197, 67124, 700, 2400,
8900, 990, 4800, 11900, 20800, 19200, 48015, 3700, 13000, 18400,
30600, 46443, 8356, 53100, 9600, 19100, 2600, 12050, 3000, 23500,
21440, 22400, 30146, 9900, 2400, 4600, 23700, 198, 39.4, 234.4,
30, 262, 182.3, 235.6, 123.1, 109, 174.5, 14.6, 192.1, 150.2,
14.6, 14.6, 24, 296.8, 268.2, 148.2, 202.5, 349.1, 131.9, 327.2,
74.8, 14.6, 70.8, 2.2, 179.1, 30, 218.9, 101.8, 31.5, 270.1,
285.5, 184.9, 22851, 279.2, 14.6, 95.3, 14.6, 17600, 500, 2800,
12000, 15700, 4680, 7400, 463, 9541, 22798, 13841, 997, 7618,
8827, 38098, 1727, 6300, 2850, 20673, 15308, 6842, 6991, 601,
3399, 33000, 7936, 44267, 300, 23381, 37435, 3913, 6559, 10800,
18347, 18500, 3978, 48098, 2800, 29416, 1505, 3596, 23842, 4146,
2300, 2962, 506, 2140, 21400, 11900, 2700, 3100, 7591, 19594,
24266, 8740, 6136, 25282, 2552, 310, 37346, 4407, 36700, 1400,
6226, 500, 1300, 262, 12245, 32928, 599, 8414, 24009, 10562,
1900, 1200, 9500, 12600, 12800, 3280, 2200, 700, 4909, 10609,
6600, 31100, 13534, 2600, 2800, 8769, 1600, 21800, 9900, 33733,
56300, 20800, 59200, 1800, 3600, 19642, 11800, 11200, 4892, 900,
1100, 1600, 19300, 1700, 14.6, 130.9, 188.7, 29.7, 14.6, 246.1,
9.8, 155.4, 172.3, 132.4, 14.6, 177.8, 191.8, 150.4, 190.9, 94.5,
14.6, 14.6, 189.3, 14.6, 60.5, 14.6, 112.6, 285.9, 354.7, 90,
132.5, 22500, 14.6, 396, 17.3, 91.7, 14.6, 23, 268.7, 31.8, 65.6
)), row.names = c(NA, -500L), class = "data.frame")

How to establish if the dates in a column are unique?

I have a simple question to ask. I couldn't find a solution on SO.
I have a column vector of dates and I would like to see whether months are unique or not. I tried to use unique but I may have used it in the wrong way.
An example:
Date = structure(c(11690, 11725, 11753, 11781, 11809, 11844, 11872,
11900, 11942, 11970, 11998, 12026, 12061, 12089, 12117, 12145,
12180, 12208, 12243, 12264, 12265, 12299, 12327, 12362, 12390,
12425, 12453, 12481, 12509, 12544, 12572, 12600, 12635, 12663,
12698, 12726, 12754, 12796, 12817, 12845, 12880, 12907, 12936,
12971, 12999, 13027, 13062, 13090, 13118, 13160, 13181, 13209,
13244, 13272, 13307, 13335, 13363, 13392, 13426, 13454, 13489,
13524, 13552, 13580, 13615, 13643, 13670, 13699, 13727, 13762,
13790, 13825, 13853, 13888, 13916, 13944, 13979, 14007, 14035,
14063, 14098, 14126, 14154, 14160, 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")
In this column vector I would like to see whether there are two observations in the same month (there are 2 for "2003-07" and "2008-10"). I can I check it with one line of command?
Thanks!
In base R, we can format the Date to get only year and month, use table to count their occurrences, Filter to select only those month-year which occur more than once.
names(Filter(function(x) x > 1, table(format(Date, "%Y-%m"))))
#[1] "2003-07" "2008-10"
Same logic using zoo::as.yearmon.
names(Filter(function(x) x > 1, table(zoo::as.yearmon(Date))))
#[1] "Jul 2003" "Oct 2008"
library('lubridate')
library('tidyverse')
Date %>%
enframe() %>%
count(year(value), month(value)) %>%
filter(n > 1)
One line with as.yearmon from zoo
library(zoo)
Date[which(duplicated(as.yearmon(Date))==TRUE)]
[1] "2003-07-31" "2008-10-08"
Use str_sub to get year-month and use table to count frequency of occurence:
library(tidyverse)
year_month <- str_sub(Date, 1, 7) #this will extract characters 1-7 (year-mo)
result <- as_tibble(table(year_month))
#or pipe it all
year_month2 <- str_sub(Date, 1, 7) %>%
table() %>%
as_tibble()
#or filter to get only those with > 1 occurence
year_month3 <- str_sub(Date, 1, 7) %>%
table() %>%
as_tibble() %>%
filter(n > 1)

"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])
}

Resources