Накопленная сумма на основе изменений даты в R

У меня есть зависимая переменная, которая МОЖЕТ изменяться каждый день, кроме воскресенья и понедельника.

И несколько независимых переменных, которые меняются каждый день, кроме субботы и воскресенья.

Я хочу построить корреляцию, возможно, создав коэффициент изменения и накапливая сумму значений независимых переменных, пока у меня не появится изменение зависимого. И используя независимые значения в день + 1 и день + 2 зависимой переменной.

Но выходные - это проблема для меня. Я хочу избежать этих пробелов или создать шаблон для работы с ними, поскольку у зависимых и независимых людей разные дни выходных (воскресенье и понедельник по сравнению с субботой и воскресеньем).

Я хотел бы знать, есть ли способ выбрать дни недели для корреляции, например, моя субботняя независимая переменная d + 1 не будет видеть реальные данные из воскресных данных из зависимой переменной, и мой d + 2 не будет работать с понедельник-зависимая переменная, потому что нет данных, orelse будет теми же данными, что и суббота.

Я также хотел бы знать, есть ли способ подсчета каждых 0 факторов на коэффициент изменения зависимой переменной, например, я хочу знать, сколько 0 (дней без изменений) с момента последнего изменения. И суммируйте сумму независимой переменной по этому результату.

Я приложил и пример, чтобы помочь понять.

Заранее спасибо.

Танаи Гонсалвес.

Date Weekday Dep.value Dep.d.1.change Count.change.days Indep.Value Indep.d.1.change cum.sum.indep.d.1 1 2018-01-01 Mon 2.5889 0 1 66.57 0 0.00 2 2018-01-02 Tue 2.5889 0 2 66.57 0 0.00 3 2018-01-03 Wed 2.5889 0 3 67.84 1.27 0.00 4 2018-01-04 Thu 2.563 -0.0259 0 68.07 0.23 1.27 5 2018-01-05 Fri 2.5488 -0.0142 0 67.62 -0.45 0.23 6 2018-01-06 Sat 2.5299 -0.0189 0 N/A #VALUE! -0.45 7 2018-01-07 Sun N/A #VALUE! 1 N/A #VALUE! -0.45 8 2018-01-08 Mon N/A #VALUE! 2 67.78 0.16 -0.45 9 2018-01-09 Tue 2.5044 -0.0255 0 68.82 1.04 0.16 10 2018-01-10 Wed 2.5156 0.0112 0 69.2 0.38 1.04 11 2018-01-11 Thu 2.5384 0.0228 0 69.26 0.06 0.38 12 2018-01-12 Fri 2.5614 0.023 0 69.87 0.61 0.06 13 2018-01-13 Sat 2.5614 0 1 N/A #VALUE! 0.61 14 2018-01-14 Sun N/A #VALUE! 2 N/A #VALUE! 0.61 15 2018-01-15 Mon N/A #VALUE! 3 70.26 0.39 0.61 16 2018-01-16 Tue 2.5367 -0.0247 0 69.15 -1.11 0.39 17 2018-01-17 Wed 2.5367 0 1 69.38 0.23 -0.72 18 2018-01-18 Thu 2.5187 -0.018 0 69.31 -0.07 0.23 19 2018-01-19 Fri 2.5319 0.0132 0 68.61 -0.7 -0.07 20 2018-01-20 Sat 2.5319 0 1 N/A #VALUE! -0.77 21 2018-01-21 Sun N/A #VALUE! 2 N/A #VALUE! -0.77 22 2018-01-22 Mon N/A #VALUE! 3 69.03 0.42 -0.77 23 2018-01-23 Tue 2.5206 -0.0113 0 69.96 0.93 0.42 24 2018-01-24 Wed 2.5075 -0.0131 0 70.53 0.57 0.93 25 2018-01-25 Thu 2.5299 0.0224 0 70.42 -0.11 0.57 26 2018-01-26 Fri 2.5384 0.0085 0 70.52 0.1 -0.11 27 2018-01-27 Sat 2.5384 0 1 N/A #VALUE! -0.01 28 2018-01-28 Sun N/A #VALUE! 2 N/A #VALUE! -0.01 29 2018-01-29 Mon N/A #VALUE! 3 69.46 -1.06 -0.01 30 2018-01-30 Tue 2.5379 -0.0005 0 69.02 -0.44 -1.06 cum.sum.indep.d.2 1 0.00 2 0.00 3 0.00 4 0.00 5 1.27 6 0.23 7 -0.45 8 -0.45 9 -0.45 10 0.16 11 1.04 12 0.38 13 0.44 14 1.05 15 1.05 16 1.05 17 0.39 18 -0.72 19 0.23 20 0.16 21 0.16 22 0.16 23 0.16 24 0.42 25 0.93 26 0.57 27 0.46 28 0.46 29 0.46 30 0.45

dput(head(Dset, 30))

структура (список (Дата = структура) (c(17532, 17533, 17534, 17535, 17536, 17537, 17538, 17539, 17540, 17541, 17542, 17543, 17544, 17545, 17546, 17547, 17548, 17549, 17550, 17551, 17552, 17553, 17554, 17555, 17556, 17557, 17558, 17559, 17560, 17561), класс = "Дата"), день недели = структура (c(2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L), .Label = c("Пт", "Пн", "Сб", "Солнце", "Чт", "Вт", "Среда"), класс = "фактор"), Dep.value = структура (c(54L, 54L, 54L, 50л, 42л, 36л, 130л, 130л, 25л, 31л, 41л, 49л, 49л, 130л, 130л, 39л, 39л, 33л, 37л, 37л, 130л, 34л, 29л, 29л, 36л, 41л, 41л, 130л, 130L, 40L), .Label = c("2,3749", "2,3966", "2,4237", "2,4348", "2,4463", "2,4521", "2,4525", "2,4568", "2,4647", "2,4662", "2.4696", "2.4712", "2.4718", "2.4758", "2.4785", "2.4786", "2.4821", "2.4896", "2.4908", "2.4952", "2.4986", "2.4989", "2.5028 "," 2.5033 "," 2.5044 "," 2.5058 "," 2.5062 "," 2.507 "," 2.5075 "," 2.5133 "," 2.5156 "," 2.5158 "," 2.5187 "," 2.5206 "," 2.5215 "," 2.5299 "," 2.5319 "," 2.5363 "," 2.5367 "," 2.5379 "," 2.5384 "," 2.5488 "," 2.5514 "," 2.5541 "," 2.5554 "," 2.5558 "," 2.5562 "," 2.5583 "," 2.5614 "," 2.563 "," 2.5667 "," 2.5691 "," 2.5753 "," 2.5889 "," 2.5893 "," 2.5943 "," 2.6078 ", "2.6172", "2.622", "2.6299", "2.6329", "2.6414", "2.6491", "2.6827", "2.6993", "2.7077", "2.7172", "2.7277", "2.7504", "2.7623" "," 2.7722 "," 2.7858 "," 2.7929 "," 2.8008 "," 2.8029 "," 2.8102 "," 2.8136 "," 2.8266 "," 2.8307 "," 2.8337 "," 2.843 "," 2.8439 ", "2.8465", "2.8554", "2.856", "2.8561", "2.8599", "2.8609", "2.8636", "2.872", "2.8748", "2.8752", "2.8786", "2.8816", "2.8845" ", "2.8896", "2.8929", "2.8944", "2.8961", "2.8965", "2.9074", "2.9108", "2.9127", "2.9146", "2.924057067", "2.9253", "2.9312", "2.9406", "2.9432", "2.9483", "2.9507", "2.9533", "2.959", "2.9678", "2.9749", "2.978", "2.9963", "2.9967", "3.0213", "3.0509", "3.0624", "3.0825", "3.0838", "3.1023", "3.1203", "3.1293", "3.1428", "3.1515", "3.1648", "N/A"), класс =" фактор "), Dep.d.1.change = структура (c(54L, 54L, 54L, 40L, 18L, 25L, 53L, 53L, 53L, 39L, 67L, 96L, 97L, 54L, 53L, 53L, 38L, 54L, 23L, 72L, 54L, 53L, 53L, 11L, 15L, 93L, 63L, 54L, 53L, 53L, 1L), .Label = c("-0.0005", "-0.0008", "-0.0021", "-0.0048", "-0.0071", "-0,0088 "," -0,0105 "," -0.0105 "," -0.0106 "," -0.0107 "," -0.0113 "," -0.0115 "," -0.0122 "," -0.0127 "," -0.0131 "," - 0.0133 "," -0.0134 "," -0.0142 "," -0.0157 "," -0.016542933 "," -0.0173 "," -0.0179 "," -0.018 "," -0.0188 "," -0.0189 "," - 0,0198 "," -0,0201 "," -0,0202 "," -0,0207 "," -0,0209 "," -0,0217 "," -0,0218 "," -0,0222 "," -0,0224 "," -0,0237", "-0.0241", "-0.0243", "-0.0247", "-0.0255", "-0.0259", "-0.027", "-0.0313", "-0.0329", "-0.0351", "-0.0356", "-0.0379", "-0.0434", "-0.0473", "-0.0599", "-0.063157067", "-0.0784", "-0.0875", "#VALUE!", "0", "0.0016", "0.0025", "0,0027", "0,0034", "0,0076", "0,0078", "0,0081", "0,0082", "0,0085", "0,0086", "0,0095", "0,0099", "0,0112", "0,0119", " 0,012 "," 0,0123 "," 0,0126 "," 0,0132 "," 0,0135 "," 0,0136 "," 0,014 "," 0,0141 "," 0,012 42 "," 0,0151 "," 0,0157 "," 0,0158 "," 0,0169 "," 0,0171 "," 0,0176 "," 0,0178 "," 0,0179 "," 0,018 "," 0,0184 "," 0,0187 "," 0,0192 ", "0,0204", "0,0217", "0,022", "0,0224", "0,0225", "0,0227", "0,0228", "0,023", "0,0231", "0,0233", "0,0235", "0,0237", " 0,0244 "," 0,0247 "," 0,025 "," 0,0255 "," 0,0271 "," 0,0273 "," 0,0284 "," 0,0285 "," 0,0291 "," 0,0293 "," 0,0296 "," 0,0329 "," 0,0336 ", "0,0352", "0,0356", "0,036", "0,0361", "0,0365", "0,0368", "0,0385", "0,0389", "0,0422", "0,0425"), class = "factor"), Count.change.days = c(1L, 2L, 3L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 1L, 2L, 3L, 0L, 1L, 0L, 0L, 1L, 2L, 3L, 0L, 0L, 0L, 0L, 1L, 2L, 3L, 0L), Indep.Value = структура (c(29L, 29L, 43L, 45L, 40L, 220L, 220L, 42L, 51L, 57L, 58L, 65L, 220L, 220L, 70L, 56L, 60L, 59L, 49L, 220L, 220L, 54L, 66L, 75L, 72L, 74L, 220L, 220L, 61L, 53L), .Label = c("62,59", "62,72", "62,79", "63,61", "63,83", "64,33", "64,34", "64,36", "64,37", "64,64", "64,81", "64,84", "64,89", "64,95", "65,12 "," 65,25 "," 65,42 "," 65,47 "," 65,49 ", "65,51", "65,54", "65,6", "65,67", "65,78", "65,79", "66,05", "66,21", "66,39", "66,57", "66,62", "66,63", "66,76 "," 66.77 "," 66.86 "," 67 "," 67.11 "," 67.31 "," 67.42 "," 67.5 "," 67.62 "," 67.64 "," 67.78 "," 67.84 "," 68.02 ", "68,07", "68,12", "68,33", "68,58", "68,61", "68,65", "68,82", "68,91", "69,02", "69,03", "69,05", "69,15", "69,2" "," 69,26 "," 69,31 "," 69,38 "," 69,46 "," 69,47 "," 69,53 "," 69,65 "," 69,87 "," 69,96 "," 70,11 "," 70,12 "," 70,18 ", "70,26", "70,27", "70,42", "70,45", "70,52", "70,53", "70,65", "70,76", "71,04", "71,42", "71,43", "71,58", "71,83 "," 71,84 "," 72,02 "," 72,06 "," 72,07 "," 72,13 "," 72,16 "," 72,21 "," 72,28 "," 72,39 "," 72,46 "," 72,58 "," 72,61 ", "72,63", "72,81", "72,83", "72,89", "72,9", "73,05", "73,06", "73,07", "73,13", "73,17", "73,21", "73,36", "73,4 "," 73.44 "," 73.45 "," 73.48 "," 73.62 "," 73.75 "," 73.78 "," 73.86 "," 73.93 "," 74 "," 74.06 "," 74.25 "," 74.29 ", "74,45", "74,54", "74,64", "74,65", "74,71", "74,73", "74,74", "74,78", "74,85", "74,87", "74,97", "75,08", "75.17 "," 75.29 "," 75.3 "," 75.33 "," 75.34 "," 75.36 "," 75.38 "," 75.39 "," 75.47 "," 75.55 "," 75.82 "," 75.88 "," 75.91 "," 75,94 "," 75,95 "," 76,17 "," 76,21 "," 76,31 "," 76,44 "," 76,46 "," 76,5 "," 76,74 "," 76,79 "," 76,83 "," 76,89 ", "77.11", "77.12", "77.14", "77.21", "77.27", "77.3", "77.32", "77.34", "77.37", "77.39", "77.42", "77.47", "77.5 "," 77.59 "," 77.62 "," 77.76 "," 77.77 "," 77.85 "," 78.05 "," 78.07 "," 78.09 "," 78.15 "," 78.17 "," 78.18 "," 78.23 ", "78,24", "78,43", "78,51", "78,7", "78,79", "78,8", "78,86", "79,03", "79,06", "79,22", "79,28", "79,29", "79,3 "," 79,4 "," 79,44 "," 79,57 "," 79,74 "," 79,78 "," 79,8 "," 79,83 "," 80,05 "," 80,26 "," 80,43 "," 80,78 "," 81,2 ", "81,34", "81,41", "81,72", "81,87", "82,72", "83,09", "83,91", "84,16", "84,58", "84,8", "84,98", "85", "86,29", "N/A"), class = "factor"), Indep.d.1.change = структура (c(84L, 84L, 152L, 97L, 29L, 83L, 83L, 93L, 144L, 109L, 88L, 122L, 83L, 83L, 110L, 52L, 97L, 7L, 35L, 83L, 83L, 112L, 138L, 120L, 10L, 90L, 83L, 83L, 50L, 28L), .Label = c("-0,01", "-0,02", "-0,03", "-0,04", "-0,05", "-0,06", "-0,07", "-0,09", "-0,1", "-0,11" "," -0.15 "," -0.16 "," -0.18 "," -0.2 "," -0.21 "," -0.23 "," -0.24 "," -0.25 "," -0.26 "," -0.27 "," -0,28 "," -0,31 "," -0,32 "," -0,33 "," -0,34 "," -0,35 "," -0,42 "," -0,44 "," -0,45 "," -0,47 "," -0,53 "," -0,54 "," -0,56 "," -0,58 "," -0,7 "," -0,72 "," -0,73 "," -0,76 "," -0,77 "," -0,79 "," -0.8 "," -0.82 "," -0.85 "," -0.86 "," -0.87 "," -0.9 "," -0.96 "," -1.01 "," -1.04 "," -1.06 "," -1.07 "," -1.11 "," -1.14 "," -1.16 "," -1.22 "," -1.32 "," -1.35 "," -1.36 "," -1.42 "," -1.43 "," -1,45 "," -1,5 "," -1,56 "," -1,69 "," -1,7 "," -1,71 "," -1,86 "," -1,91 "," -1,95 "," -2,02 "," -2.04 "," -2.14 "," -2.35 "," -2.37 "," -2.5 "," -2.58 "," -2.63 "," -2.83 "," -3.39 "," -3.49 "," -4,65 "," -5,46", "#VALUE!", "0", "0,02 "," 0,03 "," 0,05 "," 0,06 "," 0,09 "," 0,1 "," 0,13 ", "0,14", "0,16", "0,17", "0,19", "0,2", "0,23", "0,24", "0,25", "0,26", "0,28", "0,3", "0,31", "0,32 "," 0,33 "," 0,34 "," 0,35 "," 0,37 "," 0,38 "," 0,39 "," 0,4 "," 0,42 "," 0,46 "," 0,48 "," 0,49 "," 0,51 ", "0,53", "0,54", "0,56", "0,57", "0,6", "0,61", "0,63", "0,65", "0,67", "0,68", "0,71", "0,72", "0,73 "," 0,74 "," 0,79 "," 0,83 "," 0,85 "," 0,86 "," 0,88 "," 0,9 "," 0,92 "," 0,93 "," 0,96 "," 0,97 "," 0,98 ", "1", "1,02", "1,04", "1,05", "1,06", "1,09", "1,11", "1,17", "1,19", "1,25", "1,27", "1,3", "1,31 "," 1.37 "," 1.49 "," 1.54 "," 1.58 "," 1.59 "," 1.64 "," 1.69 "," 1.88 "," 1.9 "," 1.96 "," 2.05 "," 2.11 ", "2,15", "2,26", "2,36", "2,39", "2,4", "2,5"), класс = "фактор"), cum.sum.indep.d.1 = c(0, 0, 0, 1,27, 0,23, -0,45, -0,45, -0,45, 0,16, 1,04, 0,38, 0,06, 0,61, 0,61, 0,61, 0,39, -0,72, 0,23, -0,07, -0,77, -0,77, -0,77, 0,42, 0,93, 0,57, -0,11, -0,01, -0,01, -0,01, -1,06, cum.indep.d.2 = c(0, 0, 0, 0, 1,27, 0,23, -0,45, -0,45, -0,45, 0,16, 1,04, 0,38, 0,44, 1,05, 1,05, 1,05, 0,39, -0,72, 0,23, 0,16, 0,16, 0,16, 0,16, 0,16, 0,42, 0,93, 0,57, 0,46, 0,46, 0,46, 0,45)), row.names = c(NA, 30L), класс = "data.frame")

пример

0 ответов

Другие вопросы по тегам