Scaling and centering a circular distribution with conversion.circular function - r

Using the circular package, I have a circular data object:
x <- structure(c(44, 95, 88, NA, 317, 16, NA, NA, 85, NA, 31, 14, 323, 336, 345, 338, 10, 3, 342, 325, 30, 311, 359, 335, 331, 4, 347, 9, 9, 33, 335, 345, 355, 346, 18, 9, 341, 24, 352, 343, 3, 22, NA, 356, 14, 5, 8, NA, 352, 36, NA, 312, 352, 8, 351, 338, 341, 316, 357, 331, 326, 8, 347, 342, 315, 336, 9, 355, 0, 227, 14, 292, NA, 342, 352, 351, 1, 339, 358, 341, 1, 22, 20, 1, 285, 357, 279, 13, 25, 16, 353, 348, 2, 354, 357, 32, 7, 351, 349, 24, 354, 352, 352, 357, 356, 355, 332, 348, 30, 351, 128, 348, 358, 337, 177, 11, 314, 282, 342, 285, 168, 14, 26, 32, 21, 13, 11, 19, 17, 29, 285, 22, 0, 35, 8, 7, 25, 357, 354, 17, 46, 14, 6, 350, 355, 324, NA, 298, 356, 6), class = c("circular", "numeric"), circularp = structure(list(type = "angles", units = "degrees", template = "none", modulo = "2pi", zero = 1.5707963267949, rotation = "clock"), .Names = c("type", "units", "template", "modulo", "zero", "rotation")))
hist(as.numeric(x)) looks like this:
How can I scale and re-center this data so that the range is [-pi:pi] and the centre is at 0? I have tried conversion.circular but can't get the parameters right.

Related

Replace a single value in data.table column if it doesn't meet a specific criteria

I have the following data.table:
> head(sample)
WeekEndingDate Totals_1 Totals_2
1: 2021-06-05 0 0
2: 2021-06-12 0 0
3: 2021-06-19 0 0
4: 2021-06-26 0 0
5: 2021-07-03 0 0
6: 2021-07-10 0 0
> dput(sample)
structure(list(WeekEndingDate = structure(c(18783, 18790, 18797,
18804, 18811, 18818, 18825, 18832, 18839, 18846, 18853, 18860,
18867, 18874, 18881, 18888, 18895, 18902, 18909, 18916, 18923,
18930, 18937, 18944, 18951, 18958, 18965, 18972, 18979, 18986,
18993, 19000, 19007, 19014, 19021, 19028, 19035, 19042, 19049,
19056, 19063, 19070, 19077, 19084, 19091, 19098, 19105, 19112,
19119, 19126, 19133, 19140, 19147, 19154, 19161, 19168, 19175,
19182, 19189, 19196, 19203, 19210, 19217, 19224, 19231, 19238,
19245, 19252, 19259, 19266, 19273, 19280, 19287, 19294, 19301,
19308, 19315, 19322, 19329, 19336, 19343, 19350, 19357, 19364,
19371, 19378, 19385, 19392, 19399, 19406, 19413, 19420, 19427,
19434, 19441, 19448, 19455, 19462, 19469, 19476, 19483, 19490,
19497, 19504, 19511, 19518, 19525), class = "Date"), Totals_1 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 3, 5, 6, 8,
10, 12, 15, 18, 21, 24, 27, 27, 28, 32, 37, 42, 48, 54, 61, 68,
74, 81, 90, 98, 106, 116, 123, 123, 128, 136, 145, 155, 164,
173, 181, 191, 200, 208, 216, 225, 236, 243, 247, 251, 254, 259,
266, 271, 278, 288, 296, 304, 313, 326, 333, 341, 351, 360, 369,
376, 384, 392, 400, 409, 409, 412, 423, 428, 432, 436, 440, 442,
443, 443, 444, 445, 446, 446, 446, 446, 446, 446, 446, 447, 447,
447, 447, 447, 447), Totals_2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 5, 6, 8, 10, 13, 15, 18,
18, 19, 22, 26, 30, 35, 40, 45, 51, 57, 63, 69, 76, 83, 91, 98,
98, 100, 108, 117, 124, 131, 140, 146, 155, 163, 170, 178, 185,
192, 199, 207, 210, 213, 217, 221, 229, 236, 244, 253, 260, 268,
276, 284, 292, 300, 308, 316, 323, 330, 339, 347, 355, 355, 357,
364, 372, 381, 389, 398, 407, 413, 418, 422, 424, 426, 427, 427,
428, 429, 429, 429, 429, 429, 429, 429, 429, 429)), row.names = c(NA,
-107L), class = c("data.table", "data.frame"))
Within the same environment I have defined a variable x_date which has a specific date. For this example the value is 2023-09-30. Now I have been able to identify the row with the max value using the statement below:
> class(x_date)
[1] "Date"
> x_date
[1] "2023-09-30"
> sample[sample[,.I[which.max(WeekEndingDate)]]]
WeekEndingDate Totals_1 Totals_2
1: 2023-06-17 447 429
However, what I'm trying to do is if the maximum WeekEndingDate is before x_date
than I want to update that value to x_date otherwise the maximum value is fine. I was thinking something like this would work, but this replaces all values in the column instead of just the max:
max(sample$WeekEndingDate) <- ifelse(max(sample$WeekEndingDate) < x_date, x_date, max(sample$WeekEndingDate))
library(data.table)
x_date <- as.Date('2023-09-30')
sample[WeekEndingDate==max(WeekEndingDate),WeekEndingDate:=ifelse(WeekEndingDate<x_date,x_date,WeekEndingDate)]

How to make spiked rather than rectangular columns with ggplot

I'm using ggplot to display some data. The machine that generates it produces a plot that looks like this. You'll notice each individual blue peak is a spike, with a sharp top.
I've recreated this with ggplot, but is there a way to get geom_col, or another function, to produce a spike for each peak?
data <- data.frame(rpt = c(100.803333333333, 101.783333333333, 102.733333333333, 103.653333333333,
104.64, 105.566666666667, 106.526666666667, 107.46, 108.36, 109.33,
110.303333333333, 111.276666666667, 112.223333333333, 113.136666666667,
114.186666666667, 115.21, 116.2, 117.153333333333, 118.18, 119.17,
120.16, 121.153333333333, 122.163333333333, 123.086666666667,
124.046666666667, 125.003333333333, 125.956666666667, 126.946666666667,
127.866666666667, 128.82, 129.736666666667, 130.7, 131.633333333333,
132.6, 133.573333333333, 134.51, 135.453333333333, 136.43, 137.376666666667,
138.356666666667, 139.306666666667, 140.256666666667, 141.213333333333,
142.166666666667, 143.16, 144.086666666667, 145.046666666667,
146.01, 147.013333333333, 147.946666666667, 148.913333333333,
149.853333333333, 150.86, 151.803333333333, 152.746666666667,
153.693333333333, 154.673333333333, 155.66, 156.576666666667,
157.53, 158.486666666667, 159.443333333333, 160.403333333333,
161.403333333333, 162.33, 164.223333333333, 165.226666666667,
166.123333333333, 167.023333333333, 168.103333333333, 168.936666666667,
170.026666666667, 170.9, 171.85, 172.62, 173.61, 174.716666666667,
175.713333333333, 176.64, 177.57, 178.466666666667, 180.913333333333,
181.256666666667, 182.286666666667, 183.32, 184.283333333333,
185.246666666667, 186.136666666667, 187.146666666667, 188.16,
188.823333333333, 190.036666666667, 190.983333333333, 191.613333333333,
191.93, 192.323333333333, 192.84, 193.91, 194.866666666667, 195.706666666667,
199.523333333333, 200.563333333333, 201.326666666667, 202.293333333333,
203.346666666667, 204.893333333333, 206.943333333333, 208.223333333333,
208.846666666667, 209.68, 210.723333333333, 211.476666666667,
212.023333333333, 212.363333333333, 214.71, 216.576666666667,
216.97, 219.883333333333, 221.806666666667, 222.62, 223.526666666667,
224.436666666667, 225.35, 226.273333333333, 227.056666666667,
228.22, 229.013333333333, 232.076666666667, 238.52, 240.806666666667,
245.48, 248.49, 251.136666666667, 256.523333333333, 258.646666666667,
260.856666666667, 265.93, 268.88, 270.963333333333, 283.38, 285.2,
286.223333333333, 288.49, 294.926666666667),
height = c(119, 127, 132, 139, 136, 136, 140, 161, 162, 194, 239, 278,
370, 288, 434, 361, 286, 232, 213, 221, 238, 244, 266, 295, 306,
325, 358, 420, 497, 670, 838, 1104, 1451, 1743, 2018, 2170, 2226,
2058, 1777, 1464, 1158, 916, 702, 604, 540, 535, 554, 543, 517,
490, 434, 365, 322, 315, 312, 293, 272, 281, 279, 293, 297, 286,
253, 222, 170, 111, 69, 49, 29, 39, 33, 25, 24, 23, 16, 19, 18,
17, 24, 16, 19, 12, 20, 16, 19, 17, 19, 13, 16, 16, 17, 17, 16,
11, 19, 11, 18, 19, 17, 16, 14, 18, 14, 14, 11, 15, 10, 10, 11,
15, 11, 10, 10, 10, 11, 10, 10, 12, 10, 15, 14, 24, 27, 30, 22,
11, 12, 10, 10, 10, 10, 11, 10, 11, 10, 10, 12, 12, 15, 11, 10,
11, 10, 12))
ggplot(data, aes(x=rpt, y=height)) +
geom_col(position="identity")
Here's an approach where I add a zero value halfway in between each original value.
library(tidyverse)
data %>%
uncount(2, .id = "id") %>% # repeat each row
mutate(rpt = if_else(id == 2, (rpt + lead(rpt))/2, rpt), # space in between
height = if_else(id == 2, 0, height)) %>% # zero out every other row
ggplot(aes(x=rpt, y=height)) +
geom_line()

How can I check and add missing rows in a data frame based on an index vector?

I need to add missing rows from "count" based on the "numberclass" that is missing. "numberclass" is the column of the "count" dataset that should go from 1 to 652, but misses some numbers and ends at 645.
To achieve that, I made an index vector that goes from 1 to 652 called c1.
How can I use rbind to add the missing rows that are missing in "count"?
Those missing rows should contain the appropriate number in "numberclass" that is missing and a 0 on the column "sum" in the "count" data frame.
visual example
count
numberclass sum
1 1 3.45
2 2 32.45
3 3 23.11
4 5 21.33
5 6 1.54
c1
V1
1 1
2 2
3 3
4 4
5 5
6 6
finalcount
numberclass sum
1 1 3.45
2 2 32.45
3 3 23.11
4 4 0
5 5 21.33
6 6 1.54
dput(c1)
1:652
> dput(count)
structure(list(numberclass = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170,
171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183,
184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222,
223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235,
236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248,
249, 251, 252, 253, 255, 256, 257, 258, 259, 260, 261, 262, 263,
264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276,
277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302,
303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315,
316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328,
329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341,
342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354,
355, 356, 357, 358, 360, 361, 362, 363, 364, 365, 366, 367, 368,
369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381,
382, 383, 384, 385, 386, 387, 388, 389, 391, 392, 393, 394, 395,
396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408,
409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421,
422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434,
435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447,
448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460,
461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473,
474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486,
487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499,
500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512,
513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525,
526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538,
539, 540, 541, 542, 543, 545, 546, 547, 548, 549, 550, 551, 552,
554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566,
567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579,
580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592,
593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605,
606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618,
619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631,
632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644,
645, 646, 647, 648, 649, 650, 651, 652), sum = c(237.750666386555,
189.540342857143, 351.867604761905, 195.005685714286, 308.574686424686,
18.2691666666667, 85.6063492063492, 330.872041913642, 12.5832666666667,
81.3559523809524, 940.085002447968, 38.9222222222222, 67.6095238095238,
52.4340924675325, 48.9761904761905, 190.221922510823, 67.2384948051948,
106.311044372294, 50.4888222222222, 40.4883365079365, 146.992341452991,
43.6190142857143, 133.421034293119, 234.662733903319, 41.3940476190476,
27.5869769119769, 4.77619047619048, 1.14404345238095, 33.7083333333333,
44.2833333333333, 22.9526315789474, 21.5833333333333, 10.65,
2.75, 73.0113858363858, 9.41666666666667, 10.9, 30.3830128205128,
58.9269230769231, 1.39285714285714, 267.691666666667, 58.0575757575758,
48.1547008547009, 82.8479908979909, 57.6404761904762, 0.333333333333333,
15.0952380952381, 62.5674603174603, 155.280158730159, 39.9, 82.6307359307359,
24.6282467532468, 301.294040989729, 336.528306349206, 19.0833333333333,
110.152380952381, 151.278584609835, 27.3151515151515, 326.42688974359,
148.124206349206, 250.934674989716, 791.586193783953, 284.357225111163,
26.3166666666667, 689.571152020736, 211.649312496276, 143.23373015873,
104.389479365079, 1977.09488512611, 278.063024283429, 635.353051458803,
255.639689709121, 182.388611918596, 121.218055555556, 53.5880285714286,
29.8071514529915, 289.396377133977, 261.427877777778, 13.0333333333333,
120.082323232323, 26.4499333333333, 118.030555555556, 3.16666666666667,
3.5, 1.27692307692308, 1327.43098544718, 359.099526103064, 886.03077133796,
77.9476163059163, 3.7, 204.405522222222, 42.3193805944056, 83.1319512987013,
32.0430735930736, 100.999933333333, 41.4505205838606, 359.286551817072,
134.815597663857, 120.851665339892, 68.6170634920635, 120.464757456432,
98.7313991341991, 138.937179487179, 18.4913941580642, 8.9984237984238,
238.521621356421, 123.083044733045, 363.372644000444, 39.2380952380952,
3.16666666666667, 19.6226551226551, 53.5838383838384, 34.581746031746,
4.95, 131.300949206349, 445.728384935065, 109.100990092656, 364.408721825397,
61.5416666666667, 222.299498645799, 16.0214285714286, 13.5833333333333,
35.9928238095238, 522.570385291901, 92.072619047619, 451.015331590632,
276.63253968254, 61.6666666666667, 56.875, 246.15873015873, 52.5833,
73.5964119047619, 28.1214646464646, 30.1333333333333, 53.9054945054945,
206.796237085137, 111.121428571429, 182.169199264787, 59.3175971087736,
64.3332722832723, 16.9333333333333, 13.9166666666667, 23.3833333333333,
33.8173992673993, 1.50952380952381, 1.1, 47.9876584126984, 33.6666666666667,
31.7166666666667, 42.5094738594739, 193.209163059163, 36.8706349206349,
56.4786214285714, 125.781411481369, 1326.37051628773, 128.802066528312,
176.118690340834, 124.811656943091, 221.328297720058, 92.4357277483439,
5.54453781512605, 11.934710550887, 34.1893281555046, 297.559209282097,
10.45, 15.9714285714286, 0.333333333333333, 404.635647619048,
1.33333333333333, 423.917088383838, 31.725, 22.2334666666667,
126.991549902454, 46.2095071428571, 19.9333333333333, 9.41666666666667,
36.1666666666667, 101.691628950685, 88.0833333333333, 1.08333333333333,
60.5678571428571, 44.5857142857143, 10.3333333333333, 27.9333333333333,
59.6450530463688, 33.0823773448773, 15.2018740031898, 139.796428571429,
302.865200865801, 58.4464285714286, 7.50238095238095, 253.278364368964,
98.456746031746, 275.551738539239, 224.303773488182, 43.4340004939634,
14.475, 252.068551587302, 193.944014285714, 97.1103202020202,
522.762237662338, 152.027922077922, 495.599785289496, 15.45,
44.4584599224305, 2.63932178932179, 76.913480952381, 18.5944333333333,
80.5424963924964, 52.8404761904762, 19.602380952381, 21.7789854538307,
2.09285714285714, 15.6, 57.8281523809524, 114.880233333333, 2.5,
582.268982688364, 22.8928571428571, 43.5, 71.0449134199134, 13.45,
71.4832666666667, 382.793654822955, 57.6023587301587, 17.8666666666667,
134.694036507937, 8.65833333333333, 6.48333333333333, 167.456313131313,
108.970238095238, 38.0944444444444, 41.4536075036075, 644.437984476377,
64.2714285714286, 1630.6914617297, 81.8621387218045, 977.944218315018,
825.631676469739, 76.9720238095238, 161.353968253968, 70.9142857142857,
122.307142857143, 49.1575757575758, 38.9833333333333, 119.23980017316,
9.5, 7, 9.03333333333333, 0.285714285714286, 2.81558441558442,
34.3352130179203, 423.489491888615, 26.7138582972583, 20.2610666666667,
70.2504356560596, 84.3197993439266, 133.202467136288, 452.717995233655,
320.773420116725, 209.525511634406, 641.329055345934, 9.29166666666667,
20.0666666666667, 23.4825757575758, 42.336926961927, 21.5083333333333,
48.472619047619, 5.68452380952381, 3.61666666666667, 2.66666666666667,
22.6410952702853, 2596.19741576659, 3701.15679179432, 458.475674942574,
0.177777777777778, 236.511739558926, 178.846204916721, 554.69148345371,
109.069139904866, 27.9428571428571, 865.353323873349, 1315.57171181985,
4.94494734487734, 367.766031285642, 519.099162156913, 703.569199879477,
570.161782712288, 55.7592247797747, 424.061781409081, 4.14444444444444,
7.85, 1.5, 203.543559424236, 417.414520853467, 118.026934176934,
13.8930333333333, 5.3, 195.214038429218, 2, 125.901590928837,
20.183510972172, 174.23474402697, 115.783354224877, 20.9589971153889,
64.2541744390332, 30.1928142135642, 653.283386817422, 45.4998949579832,
2.28333333333333, 35.7234848484849, 13.4766233766234, 1, 1, 151.923361772117,
466.496416114588, 241.639269088134, 208.697684171547, 37.1753432142857,
32.7720180265813, 28.2666666666667, 32.9353202020202, 29.3107466063348,
52.1338661616162, 92.2408604474645, 143.825094880675, 146.094892496393,
185.56378660516, 229.435060026582, 35.8161587301587, 358.75152088854,
9.54144989396568, 100.579542891096, 48.5654928571429, 182.120363315018,
92.411123015873, 213.978268831169, 30.4477001960784, 133.023283627484,
1.48156826833297, 8.58333333333333, 4.44443333333333, 38.2468253968254,
56.047481038406, 67.3214285714286, 123.833316666667, 72.7440476190476,
4.04166666666667, 15.0999833333333, 66.4499333333333, 200.083454172494,
6.04285714285714, 160.691602741703, 6.19924242424242, 1.33333333333333,
108.082979449584, 106.752280952381, 14.5075757575758, 17.3920634920635,
131.341230952381, 44.2768897435897, 313.758134920635, 2.16666666666667,
16.6477124183007, 4.75, 23.7767065934066, 114.554377815518, 67.8246376228347,
127.12717047619, 8.01590909090909, 62.9999458874459, 24.5385558774559,
25.4267800865801, 64.9809956302521, 26.8670829004329, 144.936510045837,
18.2714285714286, 181.673313930514, 6.37619047619048, 122.4944,
163.107067798868, 62.2391525974026, 100.821861471861, 66.6090659340659,
151.295802741703, 227.115548340548, 161.469246031746, 20.8428571428571,
98.9682406349206, 84.2357142857143, 63.5107142857143, 587.042635340803,
291.116304438862, 217.717193917194, 314.73560018413, 198.123701298701,
236.697900710401, 410.192568542569, 118.817857142857, 143.350727050727,
81.387055999556, 43.8719696969697, 203.429180541681, 517.788687667888,
61.2261904761905, 382.272785934066, 75.7309523809524, 112.349503174603,
22.7539682539683, 31.7878787878788, 71.6388888888889, 116.672591197691,
31.4399816686581, 139.147260092848, 38.9365079365079, 142.327696091318,
73.9474025974026, 353.130164019063, 49.7790027560675, 247.005519209059,
98.4489704073704, 22.8163324675325, 49.0166666666667, 398.237265694185,
20.0119047619048, 127.929437229437, 29.906746031746, 11.4833333333333,
29.5477994227994, 17.2627344877345, 1, 275.39396990232, 155.285052380952,
191.24167394958, 17.5547619047619, 32.6397907647908, 48.0516145404303,
20.0202991341991, 296.087292678082, 6.05553333333333, 6.30952380952381,
550.020158730159, 398.502413950429, 697.700455175612, 342.769086313686,
100.248412698413, 578.569767384318, 323.557284593185, 578.870478870574,
799.803117448702, 66.4497474747475, 52.7964285714286, 28.2440476190476,
1, 9.15, 0.333333333333333, 101.279396149946, 20.4504329004329,
2, 0.342857142857143, 11.0416666666667, 114.264102564103, 148.394093406593,
17.3285625923784, 10.2605680868839, 109.262121733822, 5.68568095238095,
4.91666666666667, 27.8404512154512, 95.3755683538683, 134.882303769841,
61.262513966589, 16.5333333333333, 64.6593323051948, 37.6535103785104,
42.0317820956821, 17.3730092063492, 81.8735937673438, 44.7111111111111,
17.4607142857143, 70.0927904761905, 148.696792063492, 170.374507625708,
185.520274170274, 177.809072871573, 86.3721112221112, 176.200008488178,
15.1166666666667, 136.109471067821, 48.0101062250443, 166.262856565657,
148.329752057299, 151.820306375846, 4.18642884892885, 13.65,
17.5384920634921, 158.262582783883, 255.417342568543, 29.2134920634921,
197.809798534799, 29.85, 16.9095238095238, 20.8333333333333,
113.602744444444, 44.002380952381, 36.0333333333333, 318.15949047619,
116.7, 9.73333333333333, 459.457291197691, 200.920720879121,
314.905574729437, 468.928687626263, 127.85367965368, 34.46829004329,
127.564573784059, 168.830957864358, 276.134640779221, 201.892396392496,
1946.09400347577, 201.03562536075, 0.54047619047619, 782.099165160003,
425.714983516484, 89.7872682539683, 146.385452539683, 10.6666666666667,
1025.68925909923, 116.007914285714, 276.85727701204, 289.008666233766,
251.763574012009, 83.7539682539683, 348.782956092124, 241.232478499278,
35.9951548451548, 23.8844904761905, 16.75, 15.6583166666667,
23.4777777777778, 5.83333333333333, 262.787474045562, 285.537711241699,
63.2683473389356, 66.3647186147186, 2, 8.83323333333333, 751.311316139416,
20.0833333333333, 3.48333333333333, 313.547763557495, 24.6952380952381,
2.33333333333333, 60.6101524475524, 111.872585714286, 52.7153693528694,
181.421808730159, 86.6900043290043, 223.108003141303, 16.0825757575758,
304.663375396825, 48.2595238095238, 53.0539682539683, 117.610714285714,
3.1, 1.83333333333333, 305.834008148714, 197.169349200473, 0.5,
8, 33.7777777777778, 1.2, 5.58333333333333, 42.6051282051282,
144.887301587302, 65.7499666666667, 963.598530853141, 217.737908305747,
19.827380952381, 3.775, 229.018578571429, 7.19166666666667, 186.860334126984,
9.33333333333333, 0.75, 1, 43.8273809523809, 62.2753634920635,
301.048005944774, 89.4083452763611, 374.762004736842, 166.820046453546,
1058.5261360623, 872.182726540127, 54.4082666666667, 1227.53727689429,
321.227890629965, 148.721916971917, 277.273484848485, 897.280942113442,
226.137230929597, 72.7005952380952, 140.310317460317, 317.511606180094,
209.189406410256, 104.605501434676, 437.805596256685, 273.362576312576,
8.47222222222222, 227.129921804748, 0.943686868686869, 67.7638777888778,
20.4856893106893, 99.1611000111, 166.165773015873, 82.3694444444444,
227.211077777778, 72.4857142857143, 461.993158401598, 78.8, 210.535976984127,
428.665560794761, 35.797619047619, 133.786890638528, 20.4904761904762,
577.348705757576, 404.170196392496, 1101.04344335286, 270.924821327561,
196.366666666667, 5.83333333333333, 81.6839466089466, 516.43132186441,
2.33333333333333, 10.9095238095238, 54.1369047619048, 48.2956349206349,
676.496237656507, 137.799728238428, 14.4768149941046, 355.509695218997,
422.28376567026, 213.912283405959, 177.353159198024, 14.0459013125763
)), row.names = c(NA, -645L), class = c("tbl_df", "tbl", "data.frame"
))
I've found a solution with the tidyr package:
library(tidyr)
count <- as.data.frame(count)
count <- count %>% complete(numberclass = full_seq(numberclass, period = 1),fill=list(sum=0))
No need for an extra index vector.

Wind Frequency in R

I have used R's circular package as well as openair to plot wind roses. What I am not able to do is show the frequencies of the winds in the bin range (shown below) I would like to use. How does one determine the frequencies (percent and relative) using these packages? I would like to print these out or actually create an histogram using this.
The bins are shown below
N, NNE, NE ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW
require(openair)
require(circular)
df<- read.csv("Direction.csv", header = TRUE)
df1<-df[c(-1,-2,-3,-4,-5,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20)]
dput(head(df2))
structure(list(X850mb = structure(c(355, 349, 350, 65, 36, 56,
197, 282, 162, 219, 353, 32, 6, 14, 195, 45, 8, 182, 285, 285,
260, 315, 341, 343, 321, 10, 49, 61, 49, 159, 170, 49, 64, 98,
137, 178, 279, 173, 223, 221, 283, 246, 227, 231, 301, 212, 259,
329, 293, 229, 205, 261, 354, 349, 254, 311, 47, 176, 195, 224,
253, 293, 21, 34, 98, 225, 187, 204, 276, 280, 277, 233, 204,
233, 218, 212, 279, 320, 334, 205, 288, 15, 325, 322, 356, 308,
0, 343, 349, 10, 301, 340, 346, 281, 218, 305, 344, 304, 254,
275, 246, 306, 271, 309, 191, 197, 296, 312, 224, 265, 336, 2,
356, 336, 340, 349, 351, 11, 15, 306, 270, 249, 257, 271, 303,
328, 358, 52, 241, 298, 309, 259, 285, 268, 313, 339, 359, 314,
348, 309, 302, 322, 315, 285, 327, 327, 306, 29, 105, 111, 316,
1, 25, 332, 352, 270, 275, 238, 308, 338, 343, 349, 339, 346,
352, 24, 17, 5, 8, 9, 346, 318, 338, 22, 18, 337, 280, 274, 301,
302, 286, 6, 43, 35, 6, 26, 356, 4, 356, 325, 336, 20, 345, 354,
18, 54, 358, 313, 12, 327, 317, 329, 339, 274, 157, 143, 190,
306, 327, 332, 359, 49, 14, 323, 328, 331, 1, 306, 339, 21, 33), circularp = structure(list(type = "angles", units = "degrees",
template = "geographics", modulo = "2pi", zero = 1.5707963267949,
rotation = "clock"), .Names = c("type", "units", "template",
"modulo", "zero", "rotation")), class = c("circular", "integer"
))), .Names = "X850mb")
rose.diag(df2, bins = 16, main = '30yrs', col= 'darkgrey', prop = 2.0)
I suspect something like this is what you're after:
x <- cut(((as.vector(df2$X850)) + 360/(16*2) )%% 360,
seq(0,360,360/16) ,
c('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'))
table(x)

Getting the error "level sets of factors are different" when running a for loop

I have the following 3 tables:
AggData <- structure(list(Path = c("NonBrand", "Brand", "NonBrand,NonBrand",
"Brand,Brand", "NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand",
"Brand,NonBrand", "NonBrand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand", "Brand,Brand,NonBrand", "NonBrand,Brand,Brand",
"Brand,NonBrand,NonBrand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand", "Brand,NonBrand,Brand", "NonBrand,Brand,NonBrand",
"NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,Brand",
"Brand,Brand,Brand,NonBrand", "Brand,Brand,Brand,Brand,Brand,Brand,Brand",
"Brand,NonBrand,NonBrand,NonBrand", "NonBrand,NonBrand,Brand,Brand",
"Brand,Brand,NonBrand,NonBrand", "Brand,NonBrand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,NonBrand,Brand", "NonBrand,Brand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand,NonBrand", "Brand,NonBrand,NonBrand,Brand",
"NonBrand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,NonBrand,Brand,NonBrand", "NonBrand,Brand,Brand,NonBrand",
"Brand,Brand,Brand,Brand,NonBrand", "Brand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,NonBrand,Brand,Brand,Brand", "NonBrand,Brand,NonBrand,Brand",
"Brand,Brand,Brand,NonBrand,Brand", "NonBrand,NonBrand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,Brand,Brand", "Brand,Brand,NonBrand,Brand,Brand",
"Brand,Brand,Brand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,Brand,NonBrand", "Brand,Brand,NonBrand,NonBrand,NonBrand",
"NonBrand,Brand,Brand,Brand,Brand,Brand", "NonBrand,Brand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand,NonBrand,NonBrand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,Brand,NonBrand",
"NonBrand,Brand,Brand,NonBrand,NonBrand", "Brand,NonBrand,NonBrand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,Brand,Brand", "NonBrand,NonBrand,Brand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,Brand,NonBrand", "NonBrand,NonBrand,Brand,NonBrand,Brand",
"Brand,NonBrand,NonBrand,Brand,NonBrand", "NonBrand,NonBrand,NonBrand,Brand,Brand,Brand",
"NonBrand,Brand,Brand,NonBrand,Brand", "Brand,NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,Brand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,NonBrand,NonBrand,NonBrand"
), click_count = c(1799265, 874478, 198657, 128159, 45728, 30172,
20520, 17815, 16718, 9479, 6554, 3722, 3561, 3408, 3391, 3366,
3256, 2526, 1846, 1708, 1682, 1013, 951, 899, 881, 782, 780,
703, 642, 625, 615, 601, 453, 442, 414, 407, 362, 343, 313, 284,
281, 281, 271, 269, 268, 229, 223, 218, 215, 212, 204, 162, 161,
158, 155, 145, 132, 130, 115, 103, 102, 86, 77, 77, 72, 68, 68,
67, 58, 52, 32, 18, 18), conv_count = c(30938, 19652, 7401, 3803,
2014, 1072, 1084, 981, 652, 379, 230, 166, 205, 246, 254, 93,
239, 104, 112, 51, 76, 23, 66, 81, 55, 29, 62, 57, 50, 37, 17,
33, 38, 17, 8, 41, 33, 30, 24, 16, 26, 18, 16, 17, 7, 21, 10,
8, 27, 23, 11, 13, 6, 15, 14, 16, 8, 10, 6, 6, 11, 11, 8, 9,
8, 8, 9, 7, 7, 6, 6, 6, 7), CR = c(0.0171947989873643, 0.0224728352228415,
0.0372551684561833, 0.0296740767328085, 0.0440430370888733, 0.0355296301206417,
0.0528265107212476, 0.0550659556553466, 0.0389998803684651, 0.0399831205823399,
0.0350930729325603, 0.0445996775926921, 0.057568098848638, 0.0721830985915493,
0.0749041580654674, 0.0276292335115865, 0.0734029484029484, 0.0411718131433096,
0.0606717226435536, 0.0298594847775176, 0.0451843043995244, 0.0227048371174729,
0.0694006309148265, 0.0901001112347052, 0.0624290578887628, 0.0370843989769821,
0.0794871794871795, 0.0810810810810811, 0.0778816199376947, 0.0592,
0.0276422764227642, 0.0549084858569052, 0.0838852097130243, 0.0384615384615385,
0.0193236714975845, 0.100737100737101, 0.0911602209944751, 0.0874635568513119,
0.0766773162939297, 0.0563380281690141, 0.0925266903914591, 0.0640569395017794,
0.0590405904059041, 0.0631970260223048, 0.0261194029850746, 0.091703056768559,
0.0448430493273543, 0.036697247706422, 0.125581395348837, 0.108490566037736,
0.053921568627451, 0.0802469135802469, 0.0372670807453416, 0.0949367088607595,
0.0903225806451613, 0.110344827586207, 0.0606060606060606, 0.0769230769230769,
0.0521739130434783, 0.058252427184466, 0.107843137254902, 0.127906976744186,
0.103896103896104, 0.116883116883117, 0.111111111111111, 0.117647058823529,
0.132352941176471, 0.104477611940299, 0.120689655172414, 0.115384615384615,
0.1875, 0.333333333333333, 0.388888888888889)), .Names = c("Path",
"click_count", "conv_count", "CR"), row.names = c(NA, -73L), class = "data.frame")
another one here:
breakVector <- structure(list(breakVector = structure(c(1L, 1L), .Label = "NonBrand", class = "factor"),
CR = c(0.461541302855402, 0.538458697144598)), .Names = c("breakVector",
"CR"), row.names = c(NA, -2L), class = "data.frame")
and:
FinalTable <- structure(list(autribution_category = structure(c(2L, 1L), .Label = c("Brand",
"NonBrand"), class = "factor"), attributed_result = c(0, 0)), .Names = c("autribution_category",
"attributed_result"), row.names = 1:2, class = "data.frame")
when I run the following command:
if (FinalTable [2,1] == breakVector[1,1]) {
FinalTable$attributed_result[2] <- FinalTable$attributed_result[2] +
breakVector[1,2] * AggData$conv_count[3];
break}
I get the following error:
Error in Ops.factor(FinalTable[2, 1], breakVector[1, 1]) :
level sets of factors are different
This is pretty weird, since both values that im comparing are factors, I don't see any reason why R cant compare the two levels?
FinalTable[2,1] and breakVector[1,1] do not have the same levels:
> FinalTable[2,1]
[1] Brand
Levels: Brand NonBrand
> breakVector[1,1]
[1] NonBrand
Levels: NonBrand
This is easily fixed by using
breakVector[,1] <- factor(breakVector[,1], levels=c("Brand", "NonBrand"))
or, more generally
breakVector[,1] <- factor(breakVector[,1], levels=levels(FinalTable[,1]))
Perhaps, it will better compare both variables like a string:
if (as.character(FinalTable [2,1]) == as.character(breakVector[1,1])) {
FinalTable$attributed_result[2] <- FinalTable$attributed_result[2] +
breakVector[1,2] * AggData$conv_count[3];
break}

Resources