Calculate the slope of a linear regression line - r

I calculate the slope in a data frame but I have a problem returning the original value at different area sizes. my data frame in dput format is below.
My current approach is to do this:
coefLM <- function(x) {
coef(lm(log(mean) ~ log(area), tar.un_sap.10[-1,]))[2]
}
And this code gives the following problem:
tar.un_sap.10$slope=apply(tar.un_sap.10[,1:n],1,coefLM)
> head(tar.un_sap.10$slope)
[1] 0.02201471 0.02201471 0.02201471 0.02201471 0.02201471 0.02201471
Here is my data frame:
> dput(tar.un_sap.10)
structure(list(X1 = c(0, 209, 249, 127, 275, 204, 254, 121, 303,
275, 231, 200, 293, 261, 196, 287, 212, 261, 300, 260, 249, 297,
297, 270, 297, 248, 260, 262, 279, 288, 233, 232, 229, 252, 271,
264, 260, 257, 255, 246, 265, 252, 259, 251, 258, 255, 254, 256,
254, 320), X2 = c(0, 267, 266, 100, 300, 219, 300, 202, 114, 284,
264, 247, 271, 249, 287, 290, 244, 268, 231, 264, 282, 222, 276,
282, 297, 285, 246, 290, 262, 280, 233, 243, 238, 266, 271, 263,
267, 258, 264, 249, 258, 260, 260, 259, 259, 254, 256, 254, 254,
320), X3 = c(0, 179, 300, 278, 232, 224, 57, 268, 289, 264, 291,
288, 178, 289, 187, 258, 287, 241, 259, 268, 242, 265, 260, 268,
280, 277, 278, 249, 277, 251, 238, 242, 234, 268, 265, 280, 267,
277, 273, 246, 266, 252, 256, 259, 263, 254, 254, 256, 254, 320
), X4 = c(0, 250, 117, 186, 283, 284, 280, 205, 281, 260, 285,
243, 294, 214, 246, 252, 193, 231, 296, 300, 279, 264, 289, 262,
265, 270, 283, 225, 278, 265, 285, 262, 272, 277, 265, 264, 280,
259, 270, 271, 265, 262, 252, 260, 259, 254, 255, 257, 254, 320
), X5 = c(0, 190, 285, 201, 275, 136, 255, 159, 245, 281, 223,
231, 297, 267, 211, 300, 262, 299, 221, 286, 297, 275, 252, 262,
283, 267, 255, 248, 276, 223, 267, 242, 247, 242, 266, 282, 267,
276, 274, 267, 258, 261, 261, 300, 260, 256, 255, 258, 253, 320
), X7 = c(0, 292, 51, 208, 263, 270, 180, 266, 270, 296, 188,
295, 125, 223, 287, 237, 299, 223, 242, 268, 281, 240, 288, 297,
265, 247, 229, 251, 241, 271, 266, 241, 240, 266, 281, 280, 260,
251, 249, 246, 258, 259, 260, 262, 260, 254, 254, 256, 254, 320
), X8 = c(0, 280, 280, 100, 285, 220, 293, 277, 298, 305, 298,
292, 269, 255, 223, 259, 296, 248, 277, 265, 297, 288, 259, 291,
259, 255, 234, 292, 283, 279, 215, 272, 277, 266, 248, 274, 276,
263, 249, 270, 251, 246, 253, 259, 258, 255, 257, 256, 254, 320
), X9 = c(0, 249, 290, 143, 252, 116, 217, 291, 280, 255, 188,
273, 240, 254, 253, 280, 282, 285, 282, 249, 286, 252, 260, 234,
239, 273, 269, 281, 235, 261, 250, 247, 265, 246, 273, 280, 257,
267, 264, 273, 258, 232, 260, 259, 262, 254, 254, 254, 255, 320
), X10 = c(0, 307, 73, 19, 303, 174, 164, 237, 244, 247, 249,
286, 188, 301, 210, 295, 281, 268, 230, 281, 274, 245, 257, 248,
258, 267, 278, 263, 260, 262, 229, 281, 227, 265, 266, 251, 276,
276, 247, 271, 262, 262, 300, 262, 260, 255, 256, 258, 254, 320
), X11 = c(0, 53, 170, 278, 233, 269, 219, 249, 298, 259, 283,
303, 182, 255, 243, 282, 282, 232, 282, 221, 282, 280, 279, 297,
283, 279, 282, 240, 248, 277, 235, 250, 248, 259, 281, 276, 261,
272, 269, 246, 239, 241, 253, 249, 260, 255, 254, 256, 254, 320
), X12 = c(0, 223, 295, 210, 164, 269, 268, 286, 226, 307, 293,
211, 195, 240, 264, 243, 249, 232, 206, 252, 241, 242, 250, 269,
260, 280, 281, 233, 262, 246, 248, 265, 280, 247, 258, 276, 254,
251, 255, 247, 260, 260, 260, 251, 259, 254, 254, 257, 255, 320
), X14 = c(0, 300, 295, 273, 308, 290, 263, 196, 261, 213, 302,
243, 281, 298, 273, 241, 300, 291, 281, 261, 228, 196, 279, 278,
278, 235, 234, 265, 274, 269, 241, 277, 247, 248, 277, 276, 267,
272, 248, 274, 239, 246, 250, 256, 259, 254, 256, 257, 254, 320
), X15 = c(0, 235, 259, 70, 303, 295, 82, 280, 101, 293, 286,
278, 291, 302, 276, 281, 281, 288, 260, 287, 252, 295, 260, 258,
219, 284, 269, 248, 260, 266, 267, 277, 238, 238, 242, 271, 280,
257, 276, 271, 258, 261, 259, 260, 259, 254, 258, 257, 254, 320
), X16 = c(0, 161, 283, 237, 178, 253, 292, 188, 228, 305, 296,
244, 210, 214, 262, 193, 283, 278, 287, 268, 235, 221, 293, 270,
289, 260, 256, 283, 259, 279, 270, 269, 283, 271, 248, 271, 266,
275, 264, 270, 258, 260, 252, 260, 260, 253, 254, 255, 254, 320
), X17 = c(0, 304, 278, 249, 186, 286, 279, 301, 288, 246, 284,
188, 254, 283, 300, 262, 242, 276, 255, 294, 282, 253, 262, 274,
273, 292, 249, 290, 274, 252, 243, 277, 234, 271, 277, 242, 276,
248, 264, 274, 268, 252, 246, 260, 258, 254, 254, 254, 257, 320
), X18 = c(0, 211, 268, 263, 226, 180, 115, 258, 308, 295, 292,
243, 258, 264, 249, 231, 298, 278, 276, 275, 269, 227, 277, 267,
298, 250, 265, 283, 222, 256, 245, 269, 242, 266, 261, 243, 269,
242, 248, 267, 264, 235, 259, 263, 261, 259, 256, 256, 255, 320
), X19 = c(0, 242, 88, 278, 263, 304, 204, 183, 277, 247, 231,
282, 195, 242, 242, 222, 256, 203, 264, 280, 263, 262, 299, 260,
250, 241, 280, 271, 206, 230, 256, 283, 243, 277, 280, 267, 274,
264, 269, 271, 250, 243, 254, 253, 261, 254, 259, 255, 255, 320
), X20 = c(0, 400, 286, 306, 284, 204, 217, 202, 226, 226, 266,
288, 239, 246, 301, 287, 277, 263, 284, 280, 299, 288, 270, 279,
259, 244, 240, 279, 280, 256, 300, 263, 281, 281, 251, 234, 267,
251, 275, 253, 265, 262, 261, 250, 259, 254, 256, 257, 253, 320
), area = c(0, 84.298625572678, 333.194502290712, 749.687630154103,
1332.77800916285, 2082.46563931695, 3000.75052061641, 4081.63265306123,
5331.1120366514, 6747.18867138692, 8329.8625572678, 10079.133694294,
11995.0020824656, 14077.4677217826, 16326.5306122449, 18742.1907538526,
21324.4481466056, 24073.302790504, 26988.7546855477, 30070.8038317368,
33319.4502290712, 36734.693877551, 40316.5347771762, 44064.9729279467,
47980.0083298626, 52061.6409829238, 56309.8708871304, 60724.6980424823,
65306.1224489796, 70054.1441066222, 74968.7630154102, 80049.9791753436,
85297.7925864223, 90712.2032486464, 96293.2111620158, 102040.816326531,
107955.018742191, 114035.818408996, 120283.215326947, 126600.209496043,
133277.800916285, 140024.989587672, 146938.775510204, 154019.158683882,
161266.139108705, 168679.716784673, 176259.891711787, 184006.663890046,
191920.03331945, 2e+05), mean = c(0, 239.95, 233.95, 202.4, 256.25,
232.1, 213.2, 227.3, 253.5, 272.9, 260.85, 257.55, 237.2, 253.25,
250.3, 255.1, 268.2, 258, 262.6, 300.95, 270.35, 252.85, 270.35,
270.55, 270.4, 262.65, 261.25, 263.75, 256.45, 261.1, 260.1,
262.6, 250, 262.15, 265.7, 265.6, 267.55, 261.65, 260.9, 260.55,
258.5, 253.15, 256.65, 257.65, 259.75, 254.5, 255.4, 265.1, 254.4,
335), slope = c(0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401,
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401
)), row.names = c(NA, -50L), class = "data.frame")
>
How can I solve this problem?
Thanks so much for your help!

I think you would first have to modify the function a bit:
coefLM <- function(x) {
coef(lm(log(mean+1) ~ log(area+1), data=x))[2]
}
You've got 0 in mean and area, so adding a start to them (or using a different transformation) will make it so you don't lose those observations. You also need to have x, the data you're passing in, be the data in the model.
To make the calculation, I would suggest sapply() which is basically a way of looping over values. You can loop over the values 1:nrow(tar.un_sap.10) and each time pass the coefLM() function the data up to that row number.
tar.un_sap.10$slope=sapply(1:nrow(tar.un_sap.10),
function(i)coefLM(tar.un_sap.10[1:i, ]))
This produces the following result:
tar.un_sap.10$slope
# [1] NA 1.2368375 1.0122426 0.8672056 0.7885012 0.7211055
# [7] 0.6636478 0.6202517 0.5881208 0.5626241 0.5382451 0.5160296
# [13] 0.4937550 0.4754918 0.4585715 0.4435312 0.4307508 0.4180439
# [19] 0.4065729 0.3964291 0.3868378 0.3766481 0.3681876 0.3601827
# [25] 0.3525755 0.3449165 0.3375712 0.3307222 0.3238242 0.3174924
# [31] 0.3109094 0.3052338 0.2992165 0.2940177 0.2891791 0.2845205
# [37] 0.2801134 0.2756314 0.2712853 0.2670833 0.2629539 0.2587712
# [43] 0.2548675 0.2511334 0.2475967 0.2439944 0.2405411 0.2372216
# [49] 0.2339506 0.2326499

Related

How to identify the main peaks in data that contains multiple broad peaks

I've got some data that looks like it contains 23 peaks. The x-axis is time and y-axis is peak. However, each peak is quite broad, including several smaller peaks. I'd like to remove the smaller peaks, so I'm just left with the time for each main peak.
I'd be very grateful for some help!
time <- c(1562, 1563, 1564, 1565, 1566, 1810, 1811, 1812, 1813, 1814,
2058, 2059, 2060, 2061, 2306, 2307, 2308, 2309, 2310, 2560, 2561,
2562, 2563, 2564, 3064, 3065, 3066, 3067, 3580, 3581, 3582, 3583,
3584, 4095, 4096, 4097, 4098, 4099, 4610, 4611, 4612, 4613, 4614,
5128, 5129, 5130, 5131, 5132, 5133, 5637, 5638, 5639, 5640, 5641,
5876, 5877, 5878, 5879, 5880, 5881, 5882, 6125, 6126, 6127, 6128,
6129, 6130, 6607, 6608, 6609, 6610, 6611, 6612, 6613, 7072, 7073,
7074, 7075, 7076, 7077, 7078, 7079, 7519, 7520, 7521, 7522, 7523,
7524, 7525, 7526, 7527, 7528, 7941, 7942, 7943, 7944, 7945, 7946,
7947, 7948, 7949, 8342, 8343, 8344, 8345, 8346, 8347, 8348, 8349,
8350, 8351, 8708, 8709, 8710, 8711, 8712, 8713, 8714, 8715, 8716,
8717, 8718, 9045, 9046, 9047, 9048, 9049, 9050, 9051, 9052, 9053,
9054, 9055, 9352, 9353, 9354, 9355, 9356, 9357, 9358, 9359, 9360,
9361, 9362, 9363, 9624, 9625, 9626, 9627, 9628, 9629, 9630, 9631,
9632, 9633, 9634, 9867, 9868, 9869, 9870, 9871, 9872, 9873, 9874,
9875, 9876)
peak <- c(509, 672, 758, 686, 584, 559, 727, 759, 688, 528, 562, 711,
768, 678, 644, 750, 822, 693, 531, 566, 738, 793, 730, 511, 587,
739, 761, 651, 579, 747, 768, 705, 544, 551, 687, 756, 749, 645,
564, 680, 724, 691, 596, 535, 625, 685, 689, 612, 512, 537, 616,
657, 653, 573, 506, 598, 675, 685, 668, 609, 515, 575, 656, 687,
678, 626, 533, 509, 587, 641, 680, 663, 602, 515, 505, 583, 646,
693, 696, 684, 630, 549, 500, 572, 637, 681, 725, 736, 736, 703,
649, 556, 568, 637, 682, 743, 765, 767, 709, 660, 587, 548, 622,
690, 761, 779, 764, 749, 694, 631, 525, 571, 646, 724, 788, 811,
834, 818, 776, 712, 616, 536, 556, 649, 738, 801, 857, 866, 837,
808, 718, 647, 568, 508, 605, 714, 823, 872, 917, 916, 890, 825,
742, 642, 543, 549, 656, 766, 851, 921, 947, 951, 892, 830, 730,
617, 586, 675, 760, 804, 816, 795, 740, 690, 613, 522)
data <- data.frame(time = time, peak = peak)
ggplot(data, aes(x=time, y=peak)) +
geom_line()
ADDITIONAL QUESTION:
I've been trying to apply this to another data set and it picks out too many peaks. The new data contains a broad normal distribution. I'd like it to pick out just the very highest peak in this distribution, but as you can see from the image, it picks out every individual peak on either side. Is there a modification that can be made to the code to pick them out?
data2 <- data.frame(time = c(5001, 5002, 5003, 5004, 5005, 5006, 5007, 5008, 5009, 5010,
5011, 5012, 5013, 5014, 5015, 5016, 5017, 5018, 5019, 5020, 5021,
5022, 5023, 5024, 5025, 5026, 5027, 5028, 5029, 5030, 5031, 5032,
5033, 5034, 5035, 5036, 5037, 5038, 5039, 5040, 5041, 5042, 5043,
5044, 5045, 5046, 5047, 5048, 5049, 5050, 5051, 5052, 5053, 5054,
5055, 5056, 5057, 5058, 5059, 5060, 5061, 5062, 5063, 5064, 5065,
5066, 5067, 5068, 5069, 5070, 5071, 5072, 5073, 5074, 5075, 5076,
5077, 5078, 5079, 5080, 5081, 5082, 5083, 5084, 5085, 5086, 5087,
5088, 5089, 5090, 5091, 5092, 5093, 5094, 5095, 5096, 5097, 5098,
5099, 5100, 5101, 5102, 5103, 5104, 5105, 5106, 5107, 5108, 5109,
5110, 5111, 5112, 5113, 5114, 5115, 5116, 5117, 5118, 5119, 5120,
5121, 5122, 5123, 5124, 5125, 5126, 5127, 5135, 5136, 5137, 5138,
5139, 5140, 5141, 5142, 5143, 5144, 5145, 5146, 5147, 5148, 5149,
5150, 5151, 5152, 5153, 5154, 5155, 5156, 5157, 5158, 5159, 5160,
5161, 5162, 5163, 5164, 5165, 5166, 5167, 5168, 5169, 5170, 5171,
5172, 5173, 5174, 5175, 5176, 5177, 5178, 5179, 5180, 5181, 5182,
5183, 5184, 5185, 5186, 5187, 5188, 5189, 5190, 5191, 5192, 5193,
5194, 5195, 5196, 5197, 5198, 5199, 5200, 5201, 5202, 5203, 5204,
5205, 5206, 5207, 5208, 5209, 5210, 5211, 5212, 5213, 5214, 5215,
5216, 5217, 5218, 5219, 5220, 5221, 5222, 5223, 5224, 5225, 5226,
5227, 5228, 5229, 5230, 5231, 5232, 5233, 5234, 5235, 5236, 5237,
5238, 5239, 5240, 5241, 5242, 5243, 5244, 5245, 5246, 5247, 5248,
5249, 5250, 5251, 5252, 5253, 5254, 5255, 5256, 5257, 5258, 5259,
5260, 5261, 5262, 5263, 5264, 5265, 5266, 5267, 5268, 5269, 5270,
5271, 5272, 5273, 5274, 5275, 5276, 5277, 5278, 5279, 5280, 5281,
5282, 5283, 5284, 5285, 5286, 5287, 5288, 5289, 5290, 5291, 5292,
5293, 5294, 5295, 5296, 5297, 5298, 5299, 5300, 5301, 5302, 5303,
5304, 5305, 5306, 5307, 5308, 5309, 5310, 5311, 5312, 5313, 5314,
5315, 5316, 5317, 5318, 5319, 5320, 5321, 5322, 5323, 5324, 5325,
5326, 5327, 5328, 5329, 5330, 5331, 5332, 5333, 5334, 5335, 5336,
5337, 5338, 5339, 5340, 5341, 5342, 5343, 5344, 5345, 5346, 5347,
5348, 5349, 5350, 5351, 5352, 5353, 5354, 5355, 5356, 5357, 5358,
5359, 5360, 5361, 5362, 5363, 5364, 5365, 5366, 5367, 5368, 5369,
5370, 5371, 5372, 5373, 5374, 5375, 5376, 5377, 5378, 5379, 5380,
5381, 5382, 5383, 5384, 5385, 5386, 5387, 5388, 5389, 5390, 5391,
5392, 5393, 5394, 5395, 5396, 5397, 5398, 5399, 5400, 5401, 5402,
5403, 5404, 5405, 5406, 5407, 5408, 5409, 5410, 5411, 5412, 5413,
5414, 5415, 5416, 5417, 5418, 5419, 5420, 5421, 5422, 5423, 5424,
5425, 5426, 5427, 5428, 5429, 5430, 5431, 5432, 5433, 5434, 5435,
5436, 5437, 5438, 5439, 5440, 5441, 5442, 5443, 5444, 5445, 5446,
5447, 5448, 5449, 5450, 5451, 5452, 5453, 5454, 5455, 5456, 5457,
5458, 5459, 5460, 5461, 5462, 5463, 5464, 5465, 5466, 5467, 5468,
5469, 5470, 5471, 5472, 5473, 5474, 5475, 5476, 5477, 5478, 5479,
5480, 5481, 5482, 5483, 5484, 5485, 5486, 5487, 5488, 5489, 5490,
5491, 5492, 5493, 5494, 5495, 5496, 5497, 5498, 5499, 5500, 5501,
5502, 5503, 5504, 5505, 5506, 5507, 5508, 5509, 5510, 5511, 5512,
5513, 5514, 5515, 5516, 5517, 5518, 5519, 5520, 5521, 5522, 5523,
5524, 5525, 5526, 5527, 5528, 5529, 5530, 5531, 5532, 5533, 5534,
5535, 5536, 5537, 5538, 5539, 5540, 5541, 5542, 5543, 5544, 5545,
5546, 5547, 5548, 5549, 5550, 5551, 5552, 5553, 5554, 5555, 5556,
5557, 5558, 5559, 5560, 5561, 5562, 5563, 5564, 5565, 5566, 5567,
5568, 5569, 5570, 5571, 5572, 5573, 5574, 5575, 5576, 5577, 5578,
5579, 5580, 5581, 5582, 5583, 5584, 5585, 5586, 5587, 5588, 5589,
5590, 5591, 5592, 5593, 5594, 5595, 5596, 5597, 5598, 5599, 5600,
5601, 5602, 5603, 5604, 5605, 5606, 5607, 5608, 5609, 5610, 5611,
5612, 5613, 5614, 5615, 5616, 5617, 5618, 5619, 5620, 5621, 5622,
5623, 5624, 5625, 5626, 5627, 5628, 5629, 5630, 5631, 5632, 5633,
5634, 5635, 5636, 5637, 5638, 5639, 5640, 5641, 5642, 5643, 5644,
5645, 5646, 5647, 5648, 5649, 5650, 5651, 5652, 5653, 5654, 5655,
5656, 5657, 5658, 5659, 5660, 5661, 5662, 5663, 5664, 5665, 5666,
5667, 5668, 5669, 5670, 5671, 5672, 5673, 5674, 5675, 5676, 5677,
5678, 5679, 5680, 5681, 5682, 5683, 5684, 5685, 5686, 5687, 5688,
5689, 5690, 5691, 5692, 5693, 5694, 5695, 5696, 5697, 5698, 5699,
5700, 5701, 5702, 5703, 5704, 5705, 5706, 5707, 5708, 5709, 5710,
5711, 5712, 5713, 5714, 5715, 5716, 5717, 5718, 5719, 5720, 5721,
5722, 5723, 5724, 5725, 5726, 5727, 5728, 5729, 5730, 5731, 5732,
5733, 5734, 5735, 5736, 5737, 5738, 5739, 5740, 5741, 5742, 5743,
5744, 5745, 5746, 5747, 5748, 5749, 5750, 5751, 5752, 5753, 5754,
5755, 5756, 5757, 5758, 5759, 5760, 5761, 5762, 5763, 5764, 5765,
5766, 5767, 5768, 5769, 5770, 5771, 5772, 5773, 5774, 5775, 5776,
5777, 5778, 5779, 5780, 5781, 5782, 5783, 5784, 5785, 5786, 5787,
5788, 5789, 5790, 5791, 5792, 5793, 5794, 5795, 5796, 5797, 5798,
5799, 5800, 5801, 5802, 5803, 5804, 5805, 5806, 5807, 5808, 5809,
5810, 5811, 5812, 5813, 5814, 5815, 5816, 5817, 5818, 5819, 5820,
5821, 5822, 5823, 5824, 5825, 5826, 5827, 5828, 5829, 5830, 5831,
5832, 5833, 5834, 5835, 5836, 5837, 5838, 5839, 5840, 5841, 5842,
5843, 5844, 5845, 5846, 5847, 5848, 5849, 5850, 5851, 5852, 5853,
5854, 5855, 5856, 5857, 5858, 5859, 5860, 5861, 5862, 5863, 5864,
5865, 5866, 5867, 5868, 5869, 5870, 5871, 5872, 5873, 5874, 5875,
5876, 5885, 5886, 5887, 5888, 5889, 5890, 5891, 5892, 5893, 5894,
5895, 5896, 5897, 5898, 5899, 5900, 5901, 5902, 5903, 5904, 5905,
5906, 5907, 5908, 5909, 5910, 5911, 5912, 5913, 5914, 5915, 5916,
5917, 5918, 5919, 5920, 5921, 5922, 5923, 5924, 5925, 5926, 5927,
5928, 5929, 5930, 5931, 5932, 5933, 5934, 5935, 5936, 5937, 5938,
5939, 5940, 5941, 5942, 5943, 5944, 5945, 5946, 5947, 5948, 5949,
5950, 5951, 5952, 5953, 5954, 5955, 5956, 5957, 5958, 5959, 5960,
5961, 5962, 5963, 5964, 5965, 5966, 5967, 5968, 5969, 5970, 5971,
5972, 5973, 5974, 5975, 5976, 5977, 5978, 5979, 5980, 5981, 5982,
5983, 5984, 5985, 5986, 5987, 5988, 5989, 5990, 5991, 5992, 5993,
5994, 5995, 5996, 5997, 5998, 5999),
peak = c(245, 236, 220, 189, 173, 154, 142, 124, 118, 105, 107, 99,
100, 98, 111, 116, 115, 126, 129, 128, 128, 132, 134, 145, 158,
170, 188, 207, 225, 242, 237, 229, 211, 189, 173, 151, 131, 116,
113, 98, 105, 96, 101, 112, 106, 112, 113, 111, 124, 131, 127,
130, 137, 149, 146, 164, 184, 210, 228, 239, 228, 225, 217, 185,
168, 149, 140, 126, 110, 101, 104, 100, 102, 103, 109, 105, 109,
114, 117, 122, 128, 119, 130, 142, 148, 166, 182, 204, 226, 235,
247, 226, 224, 193, 174, 153, 137, 124, 119, 110, 99, 105, 96,
98, 102, 116, 122, 117, 116, 132, 123, 130, 139, 138, 145, 166,
182, 191, 215, 221, 212, 207, 186, 153, 112, 66, 8, 27, 68, 86,
113, 125, 130, 139, 148, 153, 159, 178, 188, 213, 230, 249, 259,
246, 234, 219, 197, 175, 151, 133, 126, 117, 122, 120, 115, 109,
115, 116, 125, 133, 141, 139, 142, 148, 147, 155, 170, 178, 203,
226, 254, 259, 273, 257, 249, 221, 191, 173, 152, 145, 130, 121,
127, 118, 121, 119, 124, 130, 136, 146, 144, 154, 166, 155, 163,
168, 190, 215, 223, 252, 273, 282, 275, 268, 237, 220, 191, 168,
159, 140, 137, 130, 122, 131, 120, 134, 132, 134, 137, 142, 151,
153, 164, 162, 176, 190, 196, 217, 243, 266, 271, 262, 257, 237,
214, 187, 169, 147, 138, 126, 118, 116, 114, 117, 116, 123, 123,
123, 129, 140, 146, 152, 156, 166, 178, 185, 209, 241, 264, 264,
261, 262, 232, 204, 178, 162, 140, 125, 117, 110, 112, 103, 98,
105, 108, 116, 117, 130, 128, 142, 135, 138, 149, 161, 169, 193,
222, 244, 256, 265, 246, 222, 201, 174, 147, 130, 125, 110, 102,
97, 95, 90, 98, 99, 107, 117, 115, 119, 128, 131, 137, 135, 144,
173, 188, 209, 232, 251, 251, 244, 217, 190, 162, 144, 122, 116,
103, 94, 87, 88, 92, 97, 101, 97, 98, 108, 121, 122, 118, 128,
132, 144, 162, 180, 209, 230, 246, 233, 230, 218, 187, 159, 125,
124, 105, 92, 89, 84, 81, 85, 76, 78, 92, 92, 102, 102, 109,
115, 126, 134, 137, 164, 179, 208, 227, 241, 258, 247, 223, 203,
173, 140, 121, 111, 94, 98, 85, 87, 89, 91, 84, 91, 106, 103,
107, 111, 123, 126, 142, 150, 166, 194, 221, 242, 247, 254, 237,
229, 194, 170, 140, 130, 110, 94, 85, 84, 84, 79, 82, 88, 89,
100, 108, 111, 114, 121, 122, 142, 149, 175, 198, 236, 252, 268,
279, 257, 232, 194, 170, 151, 127, 108, 104, 95, 91, 81, 83,
94, 92, 96, 106, 111, 122, 115, 131, 142, 152, 167, 183, 222,
248, 282, 290, 296, 279, 249, 226, 190, 163, 136, 116, 109, 93,
95, 92, 86, 86, 91, 108, 109, 120, 121, 131, 140, 141, 157, 187,
216, 240, 276, 311, 319, 333, 309, 281, 237, 203, 167, 134, 123,
112, 104, 92, 95, 99, 103, 112, 110, 114, 128, 138, 140, 161,
180, 193, 223, 242, 276, 317, 352, 372, 383, 354, 318, 285, 222,
190, 163, 144, 124, 114, 100, 98, 107, 105, 117, 121, 133, 144,
166, 176, 185, 199, 225, 268, 306, 348, 384, 439, 466, 463, 427,
392, 329, 274, 221, 181, 161, 141, 131, 123, 127, 128, 131, 138,
147, 163, 173, 188, 208, 221, 242, 294, 338, 380, 468, 518, 568,
596, 604, 554, 488, 403, 336, 275, 229, 182, 159, 141, 143, 140,
143, 160, 163, 183, 204, 230, 241, 244, 281, 316, 382, 448, 515,
624, 720, 790, 840, 790, 709, 616, 505, 412, 329, 267, 218, 186,
166, 155, 148, 158, 171, 183, 204, 211, 230, 243, 235, 268, 280,
315, 395, 525, 690, 881, 986, 1037, 1002, 930, 790, 668, 556,
444, 335, 276, 236, 218, 201, 201, 216, 227, 257, 290, 331, 368,
401, 451, 525, 599, 693, 823, 988, 1160, 1354, 1466, 1468, 1399,
1220, 1016, 824, 648, 501, 402, 326, 273, 253, 233, 235, 266,
271, 300, 359, 380, 422, 462, 532, 592, 703, 886, 1062, 1253,
1444, 1588, 1701, 1691, 1551, 1372, 1141, 929, 713, 563, 445,
362, 305, 258, 245, 240, 254, 280, 319, 348, 394, 416, 478, 537,
608, 717, 829, 989, 1197, 1426, 1551, 1579, 1563, 1447, 1244,
1026, 801, 638, 500, 379, 303, 265, 230, 221, 202, 203, 225,
246, 289, 303, 327, 374, 396, 462, 539, 625, 744, 899, 1017,
1094, 1138, 1058, 946, 832, 677, 543, 415, 337, 272, 230, 200,
182, 173, 170, 173, 174, 196, 208, 218, 236, 250, 280, 324, 364,
424, 505, 567, 627, 649, 660, 626, 568, 484, 400, 325, 276, 222,
185, 159, 143, 136, 129, 130, 131, 135, 142, 153, 162, 178, 191,
208, 228, 245, 285, 317, 347, 378, 397, 380, 367, 323, 288, 242,
202, 171, 151, 130, 112, 112, 103, 101, 103, 100, 113, 119, 118,
127, 135, 143, 155, 158, 177, 195, 207, 226, 247, 253, 244, 224,
201, 177, 156, 134, 118, 113, 102, 100, 90, 88, 84, 94, 87, 94,
102, 103, 101, 108, 107, 121, 120, 131, 136, 127, 135, 122, 91,
41, 3, 5, 32, 48, 50, 61, 71, 75, 80, 87, 90, 98, 108, 107, 127,
136, 139, 145, 149, 138, 139, 129, 117, 106, 94, 91, 84, 74,
75, 70, 71, 74, 72, 74, 81, 78, 87, 85, 81, 91, 93, 98, 102,
122, 120, 127, 129, 126, 133, 113, 103, 97, 90, 85, 75, 79, 69,
63, 65, 68, 68, 71, 74, 73, 78, 71, 81, 78, 87, 92, 104, 109,
118, 115, 130, 121, 122, 111, 97, 89, 87, 85, 75, 71, 70, 62,
65, 60, 69, 60, 68, 64, 66, 70, 81, 82, 85, 89, 90, 96, 105,
122, 115, 126, 114, 119, 105, 96, 94, 85, 72, 68, 62, 70, 58,
59))
blue.peaks <- tibble(time = data2$time, value = data2$peak)
blue.peaks <- blue.peaks %>% mutate(left_v = c(0.01, diff(value)),
right_v = -lead(left_v, default = -0.01),
is_peak = if_else(left_v > 0 & right_v >= 0, TRUE, FALSE))
data2$keep <- blue.peaks$is_peak
data2$keep <- ifelse(data2$keep == TRUE, data2$peak, NA)
ggplot(data2, aes(x=time, y=peak)) +
geom_col() +
geom_point(aes(x=time, y=keep))
BEFORE EDIT
data (only peak values):
peak <- c(
509, 672 ,758, 686 ,584, 559, 727, 759, 688, 528, 562, 711, 768, 678, 644, 750, 822, 693, 531, 566, 738, 793, 730, 511, 587 ,739, 761, 651, 579,
747, 768, 705, 544, 551, 687, 756, 749, 645, 564, 680, 724, 691, 596, 535, 625, 685, 689, 612, 512, 537, 616, 657, 653 ,573, 506, 598, 675, 685,
668, 609 ,515, 575, 656, 687, 678, 626, 533, 509, 587, 641, 680, 663, 602, 515 ,505, 583, 646, 693, 696, 684, 630, 549, 500 ,572, 637, 681, 725,
736 ,736, 703, 649, 556, 568, 637, 682, 743, 765, 767, 709, 660, 587, 548, 622, 690, 761, 779, 764, 749, 694, 631 ,525 ,571 ,646, 724, 788, 811,
834, 818, 776, 712, 616, 536, 556, 649, 738, 801, 857, 866, 837, 808, 718 ,647 ,568, 508, 605, 714, 823, 872, 917 ,916, 890 ,825, 742, 642, 543,
549, 656, 766, 851, 921, 947, 951, 892, 830, 730, 617, 586, 675, 760, 804, 816 ,795, 740, 690, 613, 522)
code:
peak_data <- tibble(
value = peak
)
peak_data <- peak_data %>%
mutate(
left_v = c(0.01, diff(value)),
right_v = -lead(left_v, default = -0.01),
peak = if_else(
(left_v > 0 & right_v >= 0) | (left_v >= 0 & right_v > 0), TRUE, FALSE
)
) %>%
filter(peak == TRUE) %>%
select(-left_v, -right_v, -peak)
I've put some postive number (0.01) for cheking if edges are peaks. I've got 24 peaks (local max) from this code:
peak_data %>% nrow()
[1] 24
I'm still not exactly sure what you mean by peak, for example vector:
x <- c(5, 6, 6, 4)
has 2 peaks in my code. Its easy to adapt it to have 0 peaks with change:
(left_v > 0 & right_v >= 0) | (left_v >= 0 & right_v > 0)
to:
left_v > 0 & right_v > 0
then I get 22 peaks. Or you can count it as 1 by putting one of:
left_v >= 0 & right_v > 0
left_v > 0 & right_v >= 0
depending what side you want to count for 23 peaks.
AFTER EDIT AND COMMENT
peak_data <- tibble(
peak = peak,
time = time
)
peak_data <- peak_data %>%
mutate(
left_v = c(0.01, diff(peak)),
right_v = -lead(left_v, default = -0.01),
is_peak = if_else(
left_v > 0 & right_v >= 0, TRUE, FALSE
)
) %>%
filter(is_peak == TRUE) %>%
select(peak, time)
gives peaks and peaks time.

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.

Turning a Standard XTS Object Into a Seasonal Plot

The package fpp2 has a wonderful function ggseaonplot(). I would like to make use of it by turning my xts object into a matrix where "Years" are the row names and the 12 months are the columns names.
#here is the data. This is stock data from yahoo finance
library(quantmod)
getSymbols("SPY")
adjusted = Ad(SPY)
head(adjusted)
#here is an example of what I would like the data to look like using the
AirPassengers matrix.
dput(AirPassengers)
structure(c(112, 118, 132, 129, 121, 135, 148, 148, 136, 119,
104, 118, 115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114,
140, 145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166,
171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194, 196,
196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201, 204, 188,
235, 227, 234, 264, 302, 293, 259, 229, 203, 229, 242, 233, 267,
269, 270, 315, 364, 347, 312, 274, 237, 278, 284, 277, 317, 313,
318, 374, 413, 405, 355, 306, 271, 306, 315, 301, 356, 348, 355,
422, 465, 467, 404, 347, 305, 336, 340, 318, 362, 348, 363, 435,
491, 505, 404, 359, 310, 337, 360, 342, 406, 396, 420, 472, 548,
559, 463, 407, 362, 405, 417, 391, 419, 461, 472, 535, 622, 606,
508, 461, 390, 432), .Tsp = c(1949, 1960.91666666667, 12), class = "ts")
Here is what the final project should look similar to.
library(fpp2)
ggseasonplot(AirPassengers)
class(adjusted)
#[1] "xts" "zoo"
adjusted is of type xts but ggseasonplot needs object of type ts.
Here is an alternative with ggplot2 though
Prepare the data
library(zoo)
df <- data.frame(date = index(adjusted),value = coredata(adjusted),row.names = NULL)
df$month <- factor(format(df$date, "%b"), levels = month.abb)
df$year <- format(df$date, "%Y")
Plot the data
library(ggplot2)
ggplot(df) +
aes(month, SPY.Adjusted, group = year, color = year) +
geom_line()

Plot Cumulative Distribution Function of Kernel in R

My task is to plot cumulative distribution function of asymptotic Kernels. For this purpose, i prepared following R-code for cdf of Log-normal kernel; but my problem is that cdf graph is moving downward. After I used transformation provided by author of kernel (Lognormal kernel ).
Kindly provide suggestions/correction in this problem.
R code:
k <- 200
y <- c(306, 455, 210, 883, 310, 361, 218, 166, 170, 654, 728, 71, 567, 144, 613, 707, 61, 88, 301, 81, 624, 371, 394, 520, 574, 118, 390, 12, 473, 26, 533, 107, 53, 122, 814, 93,731, 460, 153, 433, 145, 583, 95, 303, 519, 643, 765, 735, 189, 53, 246, 689, 65, 5,132, 687, 345, 444, 223, 175, 60, 163, 65, 208, 428, 230, 305, 11, 132, 226, 426, 705,363, 11, 176, 791, 95, 167, 284, 641, 147, 163, 655, 239, 88, 245, 30, 179, 310, 477,166, 450, 364, 107, 177, 156, 11, 429, 351, 15, 181, 283, 201, 524, 13, 212, 524, 288, 363, 442, 199, 550, 54, 558, 207, 92, 60, 293, 202, 353, 267, 371, 387, 457, 337, 201, 222, 62, 353, 163, 31, 340, 229, 182, 156, 329, 291, 179, 268, 142, 194, 320, 181, 285,348, 197, 180, 186, 145, 350, 285, 110, 286, 270, 81, 131, 269, 135, 79, 59, 105, 239, 13, 183, 116)
n <- length(y)
h <- 0.79 * IQR(y) * length(y) ^ (-1/5)
x <- seq(min(y) + 0.05, max(y), length = k)
Fhat <- rep(0, k)
for (j in 1:k) {
for (i in 1:n) {
PhiLN <- matrix(rep(0, k * n), ncol = k)
PhiLN[i, j] <- pnorm((log(y[i])-log(x[j]))/(sqrt(4*log(1+h))))
}
Fhat[j] <- 1/n * (sum(PhiLN[, j]))
}
plot(x, Fhat, type = "l")

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)

Resources