How to plot average hourly values over time - r

I am trying to plot the average hourly temperature over the month of December. I have a temperature point logged for every hour over the course of the month and would like to plot the average temperature at each hour within a 24hr period to see what a typical day looks like (i.e. average at timepoint 01:00:00, average at timepoint 02:00:00, etc.). Below is a subset of the dataframe. I think I'm having trouble formatting date and time correctly.
structure(list(
Date = structure(c(17866, 17866, 17866, 17866,
17866, 17866, 17866, 17866, 17866, 17866, 17866, 17866, 17866,
17866, 17866, 17866, 17866, 17866, 17866, 17866, 17866, 17866,
17866, 17866, 17867, 17867, 17867, 17867, 17867, 17867, 17867,
17867, 17867, 17867, 17867, 17867, 17867, 17867, 17867, 17867,
17867, 17867, 17867, 17867, 17867, 17867, 17867, 17867, 17868,
17868, 17868, 17868, 17868, 17868, 17868, 17868, 17868, 17868,
17868, 17868, 17868, 17868, 17868, 17868, 17868, 17868, 17868,
17868, 17868, 17868, 17868, 17868), class = "Date"),
Time = structure(c(0,
3600, 7200, 10800, 14400, 18000, 21600, 25200, 28800, 32400,
36000, 39600, 43200, 46800, 50400, 54000, 57600, 61200, 64800,
68400, 72000, 75600, 79200, 82800, 0, 3600, 7200, 10800, 14400,
18000, 21600, 25200, 28800, 32400, 36000, 39600, 43200, 46800,
50400, 54000, 57600, 61200, 64800, 68400, 72000, 75600, 79200,
82800, 0, 3600, 7200, 10800, 14400, 18000, 21600, 25200, 28800,
32400, 36000, 39600, 43200, 46800, 50400, 54000, 57600, 61200,
64800, 68400, 72000, 75600, 79200, 82800),
class = c("hms", "difftime"), units = "secs"),
Tmp = c(25.1, 25.2, 24.9, 24.2, 24.4, 24.6,
24.4, 23.8, 24.4, 25.1, 25.2, 25.3, 25.6, 25.7, 25.7, 25.9, 26,
25.7, 25.7, 25.5, 25.6, 25.5, 25.6, 25.3, 25.2, 25.2, 25, 24.3,
24.6, 24.5, 24.5, 24, 24.7, 25.1, 25.2, 25.4, 25.6, 25.8, 25.8,
25.9, 26, 25.8, 25.8, 25.6, 25.6, 25.6, 25.6, 25.4, 25.2, 25.2,
25, 24.6, 24.6, 24.5, 24.5, 24.1, 24.8, 25.2, 25.1, 25.4, 25.5,
25.8, 25.7, 26, 26, 25.8, 25.7, 25.6, 25.6, 25.6, 25.5, 25.3),
pHx2 = c(8.19, 8.18, 8.17, 8.15, 8.13, 8.12, 8.11, 8.1, 8.11,
8.15, 8.17, 8.19, 8.21, 8.23, 8.25, 8.27, 8.3, 8.31, 8.32,
8.31, 8.29, 8.26, 8.24, 8.23, 8.21, 8.2, 8.18, 8.17, 8.16,
8.15, 8.13, 8.12, 8.12, 8.16, 8.18, 8.19, 8.21, 8.23, 8.25,
8.27, 8.3, 8.31, 8.32, 8.31, 8.27, 8.25, 8.24, 8.22, 8.21,
8.19, 8.18, 8.16, 8.15, 8.14, 8.13, 8.12, 8.13, 8.19, 8.21,
8.23, 8.24, 8.26, 8.29, 8.3, 8.32, 8.33, 8.34, 8.33, 8.31,
8.28, 8.26, 8.24),
Tmpx2 = c(25.1, 25.2, 25, 24.2, 24.3,
24.6, 24.3, 23.7, 24.5, 25.1, 25.1, 25.4, 25.6, 25.8, 25.7,
26, 25.9, 25.8, 25.8, 25.6, 25.5, 25.6, 25.5, 25.4, 25.1,
25.1, 25, 24.3, 24.6, 24.5, 24.5, 24, 24.8, 25.2, 25.2, 25.4,
25.6, 25.8, 25.8, 26, 26, 25.7, 25.8, 25.6, 25.6, 25.6, 25.6,
25.3, 25.2, 25.2, 24.9, 24.6, 24.5, 24.5, 24.5, 24, 24.9,
25.1, 25.2, 25.4, 25.6, 25.7, 25.7, 26, 26, 25.8, 25.7, 25.6,
25.5, 25.5, 25.6, 25.5),
pHx3 = c("8.18", "8.16", "8.15",
"8.13", "8.12", "8.1", "8.09", "8.09", "8.13", "8.18", "8.21",
"8.25", "8.28", "8.32", "8.34", "8.37", "8.39", "8.4", "8.38",
"8.35", "8.32", "8.3", "8.27", "8.25", "8.23", "8.2", "8.18",
"8.17", "8.15", "8.14", "8.13", "8.12", "8.16", "8.2", "8.24",
"8.27", "8.3", "8.33", "8.35", "8.39", "8.41", "8.43", "8.41",
"8.38", "8.35", "8.33", "8.29", "8.27", "8.25", "8.23", "8.2",
"8.19", "8.17", "8.15", "8.14", "8.13", "8.18", "8.24", "8.28",
"8.3", "8.33", "8.36", "8.39", "8.42", "8.44", "8.45", "8.43",
"8.4", "8.36", "8.33", "8.3", "8.28"),
dttm = structure(c(1543622400,
1543626000, 1543629600, 1543633200, 1543636800, 1543640400,
1543644000, 1543647600, 1543651200, 1543654800, 1543658400,
1543662000, 1543665600, 1543669200, 1543672800, 1543676400,
1543680000, 1543683600, 1543687200, 1543690800, 1543694400,
1543698000, 1543701600, 1543705200, 1543708800, 1543712400,
1543716000, 1543719600, 1543723200, 1543726800, 1543730400,
1543734000, 1543737600, 1543741200, 1543744800, 1543748400,
1543752000, 1543755600, 1543759200, 1543762800, 1543766400,
1543770000, 1543773600, 1543777200, 1543780800, 1543784400,
1543788000, 1543791600, 1543795200, 1543798800, 1543802400,
1543806000, 1543809600, 1543813200, 1543816800, 1543820400,
1543824000, 1543827600, 1543831200, 1543834800, 1543838400,
1543842000, 1543845600, 1543849200, 1543852800, 1543856400,
1543860000, 1543863600, 1543867200, 1543870800, 1543874400,
1543878000),
class = c("POSIXct", "POSIXt"), tzone = "UTC")),
.Names = c("Date", "Time", "Tmp", "pHx2", "Tmpx2", "pHx3", "dttm"),
row.names = c(NA, -72L),
class = c("tbl_df", "tbl", "data.frame"))

By changing the class of the time object:
...class = ("Time"), units = "secs"),
I was able to aggregate based on the 24 hour time.
#Asumming we call our structure "df"
#Divide Time series by 3600 to get hour
df$Time<-df$Time/3600
#Aggregate by mean based on hour of day
test<-aggregate(df$Tmp, by=df["Time"], FUN=mean)
#RE-name for clarity
names(test)<-c("Time(24 Hr)", "Temp (degree C)")
>Test
Time(24 Hr) Temp (degree C)
1 0 25.16667
2 1 25.20000
3 2 24.96667
4 3 24.36667
5 4 24.53333
6 5 24.53333
7 6 24.46667
8 7 23.96667
9 8 24.63333
10 9 25.13333
11 10 25.16667
12 11 25.36667
13 12 25.56667
14 13 25.76667
15 14 25.73333
16 15 25.93333
17 16 26.00000
18 17 25.76667
19 18 25.73333
20 19 25.56667
21 20 25.60000
22 21 25.56667
23 22 25.56667
24 23 25.33333

Related

Systematically filling in variable columns

I need code to systematically label and fill in variables.
For example, current dataset looks like this:
data <- data.frame(Time = c(1:30),
Value = c(1:30)*2.3)
Time Value
1 2.3
2 4.6
3 6.9
4 9.2
5 11.5
6 13.8
7 16.1
8 18.4
9 20.7
10 23.0
11 25.3
12 27.6
13 29.9
14 32.2
15 34.5
16 36.8
17 39.1
18 41.4
19 43.7
20 46.0
21 48.3
22 50.6
23 52.9
24 55.2
25 57.5
26 59.8
27 62.1
28 64.4
29 66.7
30 69.0
I want to create two new variables Condition and Trial. There are 3 levels in the Condition variable (1~3) and 2 levels in the Trial variable (A or B). Condition level changes every 5 seconds in a specific pattern (1, 3, 2), and the Trial level alternates (A/B) for the first 4 seconds and disappears on the 5th second. Like this:
Time Condition Trial Value
1 1 A 2.3
2 1 B 4.6
3 1 A 6.9
4 1 B 9.2
5 1 <NA> 11.5
6 3 A 13.8
7 3 B 16.1
8 3 A 18.4
9 3 B 20.7
10 3 <NA> 23.0
11 2 A 25.3
12 2 B 27.6
13 2 A 29.9
14 2 B 32.2
15 2 <NA> 34.5
16 1 A 36.8
17 1 B 39.1
18 1 A 41.4
19 1 B 43.7
20 1 <NA> 46.0
21 3 A 48.3
22 3 B 50.6
23 3 A 52.9
24 3 B 55.2
25 3 <NA> 57.5
26 2 A 59.8
27 2 B 62.1
28 2 A 64.4
29 2 B 66.7
30 2 <NA> 69.0
How can I accomplish this by relying on Time? The code I'm imagining looks something like this:
for(every 5 seconds in Time){
data$Condition <- label as 1, 2, or 3
data$Trial <- label A or B in an alternating manner, skipping out on the last second}
#EDIT: I should specify that my actual dataset differs from the example I provide above. In reality, I am working with a massive dataset, with varying number of rows for a given time range. I need code that will use a specific range (e.g. every 70 seconds) in Time to fill the Condition and Trial values. For example, Condition has 6 levels, which will change every 70 seconds based on a given pattern (let's say, 1, 6, 4, 5, 2, 3). For instance, the Condition variable is labelled as 1 when Time = 0~40 seconds, 6 when Time = 40~80, 4 (80~120), 5 (120~160), 2(160~200), 3(200~240)1 (240~280), and so on until the end of the dataset. For each level in the Condition variable, the Trial variable alternates as A or B every 5 seconds (always starting from A). For example, for Condition 1 (Time = 0~40), Trial is labelled as A when Time = 0~5, B when Time = 5~10, A (10~15),..., B (35~40)..
Snippet of actual dataset:
data <- structure(list(Time = c(1.71, 3.2, 4.73, 5.65, 6.65,
6.75, 7.98, 8.29, 11.39, 13.31, 13.61, 14.28, 16.61, 19.39, 21.57,
22.77, 23.87, 24.05, 24.32, 24.68, 24.72, 24.79, 25.98, 26.43,
27.37, 27.67, 28.04, 29.27, 31.29, 31.42, 32.05, 33.45, 33.56,
34.11, 35.25, 35.84, 37.72, 38.09, 38.59, 39.03, 40.19, 40.64,
41.44, 42.78, 42.81, 43.15, 43.58, 44.43, 44.69, 44.9, 45.16,
45.63, 46.86, 48.91, 50.96, 52.03, 52.46, 53.13, 54.28, 55.51,
55.91, 57.36, 58, 58.17, 58.2, 58.53, 59.3, 59.83, 61.22, 61.75,
62.28, 63.58, 63.91, 65.04, 66.54, 67.1, 69.45, 71.67, 71.81,
74.04, 77.19, 78.04, 78.47, 80, 80.11, 81.36, 81.89, 83.09, 83.63,
83.66, 83.69, 84.26, 84.85, 85.71, 89.29, 90.23, 91.51, 91.78,
91.95, 96.3, 98.61, 99.08, 99.95, 101.14, 101.44, 102.5, 102.77,
103.57, 103.8, 105.15, 105.28, 105.48, 105.72, 107.38, 107.77,
107.93, 108.97, 109.13, 109.23, 109.6, 111.29, 113.12, 113.15,
113.18, 116.17, 116.37, 117.75, 120.44, 120.91, 121, 122.54,
123.17, 123.99, 124.39, 125.49, 127.71, 129.11, 130.4, 130.93,
132.16, 132.73, 133.04, 133.57, 134.15, 134.45, 136.46, 137.43,
138.43, 139.43, 140.25, 140.61, 143.3, 143.5, 143.56, 145.57,
146.65, 147.49, 147.61, 147.85, 148.02, 148.8, 151.07, 151.62,
151.75, 152.16, 153.79, 154.94, 155.04, 155.2, 156.64, 156.7,
156.77, 157.07, 158.95, 159.15, 160.36, 161.4, 162.07, 162.24,
162.44, 162.48, 162.67, 162.81, 163.07, 164.89, 165.39, 165.82,
166.09, 166.72, 166.83, 167.27, 168.61, 170.14, 171.52, 172.26,
173.13, 173.73, 174.04, 174.18, 174.21, 174.48, 175.21, 175.31,
175.48, 176.98, 177.56, 178.93, 179.03, 182.21, 184.03, 184.76,
185.06, 185.77, 186.39, 186.6, 186.95, 187.02, 187.58, 187.91,
188.08, 189.15, 189.88, 190.47, 191, 191.8, 193.5, 194.69, 195.29,
195.59, 197.07, 199.4, 200.35, 201.75, 202.28, 202.36, 202.92,
203.45, 203.62, 204.14, 204.57, 204.78, 204.87, 205.84, 206.47,
206.58, 207, 208.66, 208.99, 209.22, 212.51, 215.13, 216.02,
218.51, 218.61, 220.01, 220.04, 220.38, 221.53, 221.96, 222.63,
223.03, 223.17, 224.28, 225.64, 226.34, 226.38, 226.78, 226.81,
227.7, 227.76, 227.87, 228.2, 229.73, 230.36, 231.15, 231.58,
234.83, 235.66, 236.2, 236.46, 237.58, 237.85, 237.88, 238.32,
238.42, 239.21, 239.38, 240.05, 243.24, 243.87, 243.93, 245.45,
245.56, 245.75, 247.03, 247.12, 249.97, 250.78, 251.89, 253.99,
254.57, 257.68, 258.69, 258.85, 259.52, 259.99, 262.81, 263.28,
263.98, 265.93, 266.06, 268.1, 268.34, 270.18, 274.3, 276.99,
278.77, 279.54, 279.87, 280.43, 282.29, 282.35, 283.15, 283.35,
284.59, 285.2, 285.37, 290.75, 290.89, 291.12, 291.29, 293.53,
294.61, 296.86, 298.64, 299.64, 301.24, 303.29, 307.01, 307.18,
307.95, 309.66, 309.83, 309.86, 310.13, 310.69, 310.73, 312.01,
315.36, 316.1, 316.27, 316.56, 316.93, 317, 317.27, 317.9, 318.1,
319.25, 319.72, 319.99, 320.22, 322.3, 324.96, 326.42, 326.76,
327.62, 328.35, 328.47, 328.84, 329.27, 329.57, 330.43, 331,
332.22, 332.75, 334.05, 334.72, 334.86, 335.74, 338.75, 340.86,
341.84, 341.94, 343.14, 344.61, 344.71, 344.81, 345.85, 349.48,
349.68, 349.85, 350.61, 353.46, 353.53, 353.76, 354.36, 357.58,
360.8, 362.11, 362.15, 362.21, 362.35, 362.68, 364.18, 368.26,
369.02, 369.12, 369.35, 369.49, 369.85, 370.51, 371.68, 371.98,
372.01, 372.17, 372.47, 374.17, 376.28, 376.75, 377.32, 378.66,
379.37, 380.97, 381.3, 381.44, 381.54, 381.64, 381.87, 382.79,
383.13, 385.09, 385.59, 386.74, 387.68, 387.71, 390.29, 390.82,
391.23, 393.14, 393.21, 393.81, 395.08, 395.11, 395.21, 395.66,
395.83, 396.16, 396.29, 397.06, 397.23, 398.19, 398.66, 398.83,
402.77, 404.23, 404.36, 404.64, 405.03, 405.23, 405.27, 405.53,
406.41, 406.71, 407.18, 408.02, 408.08, 408.65, 409.66, 411.26,
411.54, 411.76, 412.3, 412.67, 412.95, 413.18, 413.21, 414.51,
415.09, 415.15, 415.22, 418.1, 418.64, 420.86, 421.55, 423.28,
424.08, 426.49, 427.42, 429.29, 429.54, 429.68, 429.94, 430.27,
430.47, 430.91, 431.64, 431.87, 432.34, 434.29, 434.66, 434.9,
436.21, 438.01, 438.75, 439.08, 439.08, 439.46, 442.56, 443.68,
444.11, 445, 445.5, 446.36, 446.56, 447.33, 447.36, 448.41, 449.25,
450.42, 451.2, 452.54, 454.25, 455.62, 455.75, 456.65, 457.43,
458.5, 460.54, 460.95, 461.02, 461.82, 463.32, 463.48, 464.31,
465.17, 466.99, 467.12, 467.59, 469.69, 470.64, 472.1, 473.49,
474.43, 475.16, 477.78, 478.28, 479.61, 480.56, 482.83, 483.89,
483.96, 484.86, 485.51, 486.76, 487.03, 487.09, 488.8, 489.23,
489.39, 489.64, 489.68, 489.94, 491.24, 491.31, 491.52, 492.65,
493.77, 494.77, 494.99, 495.63, 498.45, 500.6, 501.13, 503.42,
505.42, 505.78, 507.94, 510.02, 511.79, 516.21, 517.26, 517.46,
519.65, 520.98, 522.11, 523.23, 524.46, 526.09, 526.65, 528.64,
528.84, 529.08, 529.25, 529.83, 531.6, 532.39, 533.61, 534.71,
535.25, 535.68, 536.15, 537.53, 537.63, 539.8, 541.28, 542.29,
542.45, 543.12, 543.8, 544.34, 545.3, 545.64, 548.22, 548.28,
548.42, 549.06, 549.19, 549.78, 551.61, 552.97, 554.3, 554.71,
557.79, 558.05, 558.16, 560.54, 562.19, 563.56, 563.59, 563.65,
563.82, 564.09, 564.49, 565.68, 567.24, 567.48, 567.65, 567.68,
568.86, 568.92, 570.23, 571.31, 572.26, 572.76, 573.16, 574.09,
577.21, 579.71, 583.7, 584.1, 585.82, 585.88, 585.95, 586.45,
586.51, 586.65, 588.26, 588.42, 588.64, 588.87, 589.3, 589.47,
589.8, 590.84, 591.27, 591.54, 591.6, 592.52, 594.19, 594.65,
594.82, 595.12, 595.32, 595.64, 596.37, 596.5, 596.57, 596.67,
596.94, 596.97, 597.33, 597.44, 597.97, 598.44, 598.91, 598.96,
600.52, 602.71, 603.18, 603.57, 604.74, 607.12, 607.46, 608.12,
608.26, 608.76, 610.54, 611.08, 611.41, 612.2, 612.73, 615.19,
616.61, 617.68, 617.81, 619.2, 619.67, 620.97, 621.13, 621.63,
622.48, 623.01, 623.15, 624.15, 624.21, 624.55, 625.62, 626.07,
629.98, 630.65, 630.92, 632.57, 632.6, 633.5, 634, 634.77, 635.5,
635.86, 636.12, 638.79, 639.07, 639.41, 640.37, 642.58, 643.79,
644.72, 644.76, 645.05, 645.83, 645.85, 647.01, 647.37, 650.86,
651.09, 651.95, 655.01, 655.61, 656.36, 657.86, 658.83, 660.41,
660.61, 660.85, 662.35, 662.55, 662.64, 663.3, 664.56, 665.1,
665.49, 665.99, 666.13, 667.61, 667.75, 667.88, 667.95, 669.15,
670, 670.37, 670.67, 670.7, 670.9, 671.33, 671.54, 674.18, 677.27,
677.37, 678, 678.44, 679.14, 679.37, 679.69, 680.28, 681.38,
682.69, 682.95, 683.41, 685.67, 685.91, 685.97, 687.02, 687.39,
688.19, 688.29, 690.54, 690.68, 691.31, 692.14, 693.01, 693.24,
695.12, 696.23, 698.51, 699.98, 700.93, 701.23, 703.94, 707.06,
711.78, 712.9, 713, 713.13, 715.54, 718.03, 718.07, 719.39, 719.65,
720.28, 721.02, 721.39, 722.23, 722.77, 724.3, 726.09, 726.66,
727.16, 727.39, 729.1, 729.24, 729.57, 730.17, 730.97, 732.52,
733.93, 734.63, 735.64, 735.67, 735.84, 736.57, 736.91, 736.94,
737.11, 737.67, 738.89, 740.2, 740.7, 741.16, 742.08, 744.41,
744.5, 745.06, 745.86, 747.03, 747.85, 748.81, 749.18, 751.33,
751.63, 753.6, 753.9, 754.03, 754.49, 757.12, 758.67, 758.93,
761.48, 765.27, 767.94, 768.19, 769.12, 769.55, 769.95, 770.16,
771.77, 771.8, 772.74, 773.13, 773.5, 774.3, 774.77, 775.29,
775.96, 776.19, 776.52, 777.35, 777.72, 778.27, 778.61, 779.07,
780.61, 781.28, 781.36, 782.23, 782.7, 783.53, 785.04, 787.58,
788.92, 789.3, 789.8, 790.26, 790.86, 790.99, 791.5, 792.44,
793.78, 793.88, 794.68, 794.85, 795.16, 795.19, 795.96, 796.83,
799.01, 799.05, 799.32, 800.62, 801.48, 803.53, 803.84, 804.17,
806.18, 806.72, 807.06, 807.45, 808.02, 808.64, 809.64, 811.44,
812.28, 813.95, 815.67, 816.1, 818.24, 818.69, 819.42, 819.55,
819.66, 819.82, 821.63, 821.79, 821.87, 822.34, 824.87, 825.07,
825.39, 825.53, 825.96, 827.79, 827.92, 828.26, 828.41, 829.34,
829.64, 832.06, 832.83, 833.06, 833.53, 834.56, 836.91, 837.18,
837.54, 837.65, 839.1, 841.33, 841.4, 842.21, 842.38, 842.58,
842.82, 843.98, 844.52, 844.82, 845.17, 845.6, 846.8, 847.43,
849.78, 849.81, 850.18, 850.95, 851.48, 851.8, 852.37, 852.67,
852.87, 853.84, 855.19, 856.55, 858.05, 858.54, 859.5, 860.57,
860.88, 860.9, 862.19, 862.42, 862.85, 862.96, 863.69), Value = c(35.54,
28.32, 28.39, 27.83, 29.44, 29.94, 30.98, 32.92, 28.17, 29.62,
28.92, 29.91, 29.6, 31.72, 30.77, 30.67, 31.31, 31.04, 30.56,
31.2, 31.12, 31.12, 29.61, 31.43, 32.09, 32.29, 33.03, 34.83,
31.1, 31.73, 32.01, 32.98, 33.12, 32.38, 32.21, 32.92, 29.35,
31.12, 32, 32.08, 32.71, 33.73, 38.35, 38.42, 38.4, 38.77, 36.68,
38.61, 39.67, 40.4, 40.72, 40.54, 41.92, 40.41, 41.51, 39.74,
40.22, 42.03, 41.79, 42.13, 41.32, 41.98, 41.4, 41.01, 40.98,
41.09, 42.13, 41.88, 41.63, 42.42, 43.31, 42.09, 43.61, 44.24,
43.87, 45.36, 48.3, 48.66, 48.78, 32.48, 26.62, 26.02, 26.37,
27.24, 27.56, 29.06, 30.21, 30.16, 28.09, 27.32, 27.04, 27.08,
26.47, 26.18, 30.75, 28.65, 30.16, 30.37, 29.66, 25.69, 25.16,
24.91, 23.46, 25.76, 25.75, 24.21, 24.12, 25.98, 23.75, 22.23,
21.9, 21.85, 21.73, 24.61, 25.73, 25.84, 24.59, 24.3, 24.05,
24.69, 24.8, 27.17, 27.28, 27.26, 39.1, 39.76, 43.77, 45.35,
46.13, 46.03, 44.84, 45.13, 43.99, 43.5, 44.26, 44.79, 44.48,
44.77, 45.11, 45.24, 44.35, 43.7, 43.59, 44.54, 44.74, 44.18,
44.05, 41.75, 43.9, 45.22, 45.35, 45.45, 45.87, 45.79, 46.85,
48.39, 33.07, 32.45, 30.5, 29.41, 28.08, 24.81, 25.36, 25.41,
23.61, 24.48, 23.75, 23.38, 23.06, 25.85, 25.67, 25.35, 25.89,
27.49, 27.25, 26.85, 28.95, 22.96, 22.77, 22.67, 22.68, 23.35,
24.06, 25.23, 27.63, 28.12, 28.22, 28.37, 29.96, 30.35, 31.43,
32.05, 31.5, 32.77, 26.65, 27.91, 28.39, 28.17, 28.34, 28.25,
28.82, 29.06, 28.61, 28.99, 28, 28.6, 29.8, 29.87, 23.96, 23.85,
24.31, 24.14, 24.02, 23.79, 23.79, 24.23, 24.68, 28.65, 30.15,
31.06, 32.87, 34.21, 34.12, 34.12, 37.13, 39.15, 37.07, 37.99,
39.24, 42.75, 46.47, 45.9, 47.55, 47.35, 47.61, 46.34, 47.44,
47.19, 46.81, 47.15, 47.15, 47.4, 46.31, 46.6, 46.47, 46.42,
43.86, 45.1, 45.54, 43.95, 44.76, 45.27, 44.42, 44.58, 38.01,
36.84, 29.47, 27.04, 26.71, 24.72, 24.66, 24.64, 24.26, 23.69,
27.18, 27.15, 27.61, 27.75, 26.89, 26.77, 26.2, 25.65, 27.26,
21.86, 21.36, 21.32, 26.9, 28.57, 29.82, 30.53, 28.63, 27.27,
27.44, 27.06, 27.07, 30.38, 30.53, 25.36, 24.64, 23.12, 23.22,
26.04, 26.4, 27.51, 28.19, 28.05, 25.01, 18.68, 20.67, 23.42,
22.53, 28.56, 26.07, 26.04, 28.38, 26.85, 33.58, 34.9, 35.27,
33.2, 33.18, 32.88, 33.01, 35.34, 31.81, 32.89, 36.26, 36.04,
35.57, 35.25, 35.16, 35.33, 36.51, 36.82, 37.76, 37.67, 37.69,
42.1, 42.17, 42.04, 41.33, 30.25, 26.01, 27.93, 25.78, 28.27,
29.22, 28.64, 23.71, 23.46, 24.2, 23.42, 23.89, 23.88, 23.34,
22.91, 23.11, 24.58, 24.98, 24.25, 24.39, 24.03, 24.14, 24.14,
24.15, 24.69, 25.31, 23.35, 22.55, 22.71, 23.07, 24.62, 24.22,
23.7, 23.17, 23.39, 23.52, 23.05, 20.54, 20.37, 20.49, 20.62,
22.82, 24.33, 24.05, 28.24, 29.71, 30.06, 32.57, 35.14, 36.04,
35.25, 35.41, 38.18, 36.75, 36.65, 36.58, 39.1, 40.92, 41.23,
41.48, 38.61, 40.14, 40.14, 39.76, 40.31, 42.69, 41.24, 40.99,
40.87, 40.79, 40.38, 40.46, 42.82, 29.03, 30.32, 30.05, 29.86,
29.55, 29.05, 28.02, 28.68, 24.92, 24.77, 24.28, 25.34, 27.04,
27.84, 27.91, 28.63, 31.68, 30.74, 30.8, 30.34, 30.22, 30.31,
29.49, 25.3, 26.12, 26.94, 29.79, 29.16, 27.01, 28.54, 28.68,
28.01, 27.35, 27.63, 27.58, 27.42, 27.31, 23.24, 23.4, 23.32,
23.82, 23.12, 23.92, 24.14, 24.98, 25.17, 25.86, 25.71, 25.33,
23.64, 25.76, 25.52, 24.7, 24.15, 24.34, 24.4, 24.87, 25.75,
26.03, 28.34, 29.46, 29.38, 29.02, 30.2, 31.34, 31.06, 31.65,
31.66, 32.37, 33.28, 34.38, 34.41, 36.18, 35.25, 35.48, 35.9,
37.12, 36.49, 35.38, 35.92, 36.32, 36.85, 37.47, 37.9, 37.5,
37.2, 37.43, 37.64, 37.56, 37.39, 37.5, 36.7, 36.81, 36.05, 40.22,
39.11, 38.5, 38.97, 39.23, 40.3, 39.91, 39.62, 38.43, 22.1, 21.16,
21.51, 22.14, 23.15, 25.9, 25.29, 26.81, 26.87, 27.95, 25.05,
21.3, 21.28, 22.25, 24.42, 26.44, 27.01, 27.83, 26.74, 24.39,
21.13, 21.75, 21.78, 22.76, 24.01, 24.1, 24.61, 24.62, 25.13,
25.5, 26.6, 27.37, 23.47, 24.67, 24.28, 23.98, 23.33, 24.57,
25.34, 22.1, 25.41, 27.3, 30.81, 31.03, 35.26, 36.44, 36.46,
36.28, 36.68, 36.5, 36.77, 37.05, 37.69, 37.69, 38.26, 37.72,
38.02, 37.86, 38.6, 40, 40.5, 40.52, 42.02, 40.48, 36.9, 38.67,
38.12, 41.4, 41.87, 42.19, 39.6, 38.18, 22.66, 23.31, 24.07,
28.23, 28.73, 26.96, 25.21, 22.78, 23.07, 22.75, 21.77, 21.18,
21.72, 22.79, 24.25, 25.52, 24.09, 19.38, 20.42, 22.06, 21.88,
22.13, 21.74, 22.46, 23.42, 23.3, 23.7, 24.06, 25.72, 22.35,
24.7, 26.49, 25.8, 24.26, 24.49, 24.48, 25.63, 26.05, 25.9, 24.68,
23.99, 27.54, 26.73, 30.1, 30.17, 30.61, 33.7, 35.43, 39.35,
39.3, 39.43, 39.56, 40.18, 40.45, 41.19, 41.75, 41.58, 41.42,
41.63, 40.56, 40.6, 42.25, 41.04, 41.18, 41.56, 38.42, 37.57,
33.8, 38.25, 39.56, 41.87, 46.15, 46.23, 46.24, 39.31, 38, 35.89,
31.62, 30.74, 30.11, 30.44, 30.69, 30.64, 29.5, 27.87, 27.79,
23.97, 23.71, 22.41, 23.02, 24.78, 24.94, 24.52, 25.06, 24.95,
26.42, 26.09, 25.82, 25.13, 24.64, 24.67, 26.61, 27.55, 28.27,
28.1, 29.09, 29.14, 30.58, 27.81, 27.76, 29.08, 28.83, 29.98,
29.8, 29.31, 29.04, 27.59, 30.26, 30.69, 26.8, 21.32, 21.89,
25.36, 26.36, 26.15, 26.18, 27.75, 27.85, 26.3, 26.31, 21.29,
21.25, 20.7, 20.64, 21.66, 21.69, 21.06, 21.9, 20.57, 31.85,
32.71, 33.74, 37.93, 37.99, 37.47, 37.35, 39.15, 41.59, 42.64,
43.03, 43.12, 43.06, 43.59, 42.12, 36.73, 37.13, 38.57, 38.44,
38.23, 36.87, 36.71, 33.52, 35.4, 37.74, 38.44, 40.39, 39.12,
37.85, 35.71, 34.55, 32.94, 19.84, 19.52, 19.18, 20.23, 20.19,
20.08, 20.68, 21.35, 26.09, 27.68, 29.22, 29.2, 28.82, 28.32,
27.69, 27.7, 33.02, 21.7, 23.97, 24.85, 25.08, 25.45, 25.98,
24.65, 25.38, 32.03, 31.75, 31.32, 31.59, 30.15, 28.8, 22.79,
22.09, 23.24, 25.04, 25.51, 25.98, 27.46, 27.71, 27.69, 27.56,
26.96, 25.82, 25.3, 20.97, 21.08, 22.18, 22.95, 24.39, 23.71,
26.47, 30.37, 33.35, 27.92, 32.17, 33.73, 42.17, 46.03, 46.36,
46.49, 46.53, 46.25, 42.34, 41.32, 41.48, 40.65, 39.84, 39.87,
37.17, 37.34, 37.63, 37.93, 39.1, 42.72, 42.14, 42.01, 42.44,
41.78, 41.87, 42.63, 41.21, 41.86, 45.11, 33.58, 35.21, 35.98,
36.03, 35.03, 33.5, 32.57, 32.49, 31.72, 31.39, 30.1, 29.55,
29, 28.6, 26.68, 26.82, 26.81, 27.16, 30.05, 30.39, 28.92, 27.95,
27.66, 27.67, 28.15, 27.51, 28.21, 28.34, 28.78, 27.03, 24.3,
24.62, 26.67, 26.03, 24.02, 22.97, 25.12, 25.81, 25.61, 25.55,
26.67, 26.89, 27.75, 29.21, 30.68, 33.93, 36.45, 38.18, 38.85,
38.85, 36.66, 35.16, 35.77, 37.94, 39.01, 39.28, 41.23, 43.02,
43.33, 44.4, 43.69, 44.51, 45.45, 43.49, 41.61, 40.32, 40.81,
40.51, 41.82, 42.14, 42.39, 42.32, 41.96, 41.99, 41.64, 41.71,
41.63, 41.6, 41.66, 40.55, 40.51, 40.59, 41.31, 43.52, 42.96,
41.95, 42.12, 41.77, 32.63, 28.05, 29.48, 30.68, 31.49, 30.03,
30.22, 24.67, 28.49, 27.23, 26.41, 26.52, 29.27, 28.79, 28.65,
29.42, 29.6, 29.71, 24.26, 24.34, 24.37, 24.6, 24.24, 23.72,
23.69, 23.89, 24.73, 25.76, 25.77, 26.02, 26.55, 26.5, 26.94,
22.51, 24.7, 24.11, 24.83, 23.39, 24.2, 23.39, 23.16, 23.37,
24.85, 23.16, 23.1, 24.34, 24.6, 24.58, 24.56, 26.69, 27.8, 27.91,
27.22, 26.6, 31.89, 35.08, 38.79, 38.8, 40.26, 40.81, 40.71,
39.31, 38.55, 38.27, 38.45, 37.41, 38.27, 39.23, 37.43, 36.85,
35.66, 37.19, 36.85, 36.78, 35.91, 36.03, 36.87, 37.03, 37.28
)), row.names = c(NA, -1000L), class = c("tbl_df", "tbl", "data.frame"
))
I am offering a simple and transparent solution. Get the length of time, as 30 in your example. Create a list for Condition with a "rep" function using the length (30) and members of the respective list (3 or 5).
Condition= rep(c(1,3,2), 30/3)
Follow the same idea with Trial,
Trial=rep(c("A", "B", "A", "B", "NA"), 30/5)
Add the columns to the original data set.
data$Condition=Condition
data$Trial=Trial
You should be able to achieve this by using %/% and %% operations
data <- data.frame(Time = c(1:30),
Value = c(1:30)*2.3)
conditionlabel=c(1,3,2)
triallabel=c('A','B','A','B', NA)
data2 = data %>%
mutate(
condition = conditionlabel[((Time-1) %/% 5 %% 3) + 1],
trial = triallabel[(Time-1) %% 5 + 1]
)
> data2
Time Value condition trial
1 1 2.3 1 A
2 2 4.6 1 B
3 3 6.9 1 A
4 4 9.2 1 B
5 5 11.5 1 <NA>
6 6 13.8 3 A
7 7 16.1 3 B
8 8 18.4 3 A
9 9 20.7 3 B
10 10 23.0 3 <NA>
11 11 25.3 2 A
12 12 27.6 2 B
13 13 29.9 2 A
14 14 32.2 2 B
15 15 34.5 2 <NA>
16 16 36.8 1 A
17 17 39.1 1 B
18 18 41.4 1 A
19 19 43.7 1 B
20 20 46.0 1 <NA>
21 21 48.3 3 A
22 22 50.6 3 B
23 23 52.9 3 A
24 24 55.2 3 B
25 25 57.5 3 <NA>
26 26 59.8 2 A
27 27 62.1 2 B
28 28 64.4 2 A
29 29 66.7 2 B
30 30 69.0 2 <NA>

Get position indices after min / max aggregation on matrix

Sequel to my first problem here (How to aggregate hourly values into 24h-average means without timestamp).
Now I want to calculate the max (and min) from my timeseries of each 12-hour interval.
I have got my hourly data measurements (data_measure). Now I changed it into a time series of half-days.
t_measure <- ts(data = data_measure, frequency = 12)
then I used the aggregate function from {stats}
data_measure_daily_max <- aggregate(t_measure, 1, max)
data_measure <- structure(c(8.29, 7.96, 8.14, 7.27, 7.37, 7.3, 7.23, 7.53,
7.98, 10.2, 12.39, 14.34, 14.87, 14.39, 12.54, 11.84, 10.3, 10.62,
10.65, 10.56, 10.43, 10.35, 9.85, 9.12, 8.95, 8.82, 8.92, 9.33,
9.44, 9.3, 9.15, 9.37, 9.54, 10.24, 12.13, 12.43, 12.65, 13,
13.18, 13.58, 13.64, 13.75, 13.85, 13.94, 13.79, 13.84, 13.94,
14.26, 24.93, 24.64, 23.67, 21.46, 21.33, 20.83, 21.12, 21.1,
23.75, 25.39, 30.72, 30.71, 30.81, 30.92, 32.61, 32.37, 32.49,
30.68, 30.23, 30.45, 28.1, 26.9, 25.09, 25.07, 24.59, 24.22,
23.05, 22.21, 22.07, 21.6, 21.24, 21.22, 21.85, 24.87, 28.85,
29.42, 30.82, 30.97, 31.32, 30.81, 30.83, 29.9, 30.01, 30.31,
30, 27.91, 25.78, 25.88, 8.78, 8.47, 8.49, 7.65, 8.63, 9.02,
9.02, 8.11, 7.63, 9.19, 11.25, 12.24, 13.62, 12.09, 10.6, 11.1,
10.16, 10.44, 9.58, 10.04, 10.01, 10.23, 9.51, 9.2, 9.34, 9.6,
9.4, 9.45, 9.36, 9.26, 9.3, 9.46, 9.58, 9.89, 10.6, 11.04, 12.1,
12.61, 13.12, 13.47, 13.55, 13.51, 13.63, 13.84, 13.93, 14.17,
13.97, 13.86), .Dim = c(48L, 3L), .Dimnames = list(NULL, c("station1",
"station2", "station3")))
So actually I need an index/vector which tells me where my max and min of these time intervals are, so later on I can extract exactly these for an other data sets to make a comparison.
My first trial:
max_index <- which(aggregate(t_measure, 1, max)) # argument to 'which' is not logical
Use which.max and which.min with aggregate
a1 <- aggregate(t_measure, 1, which.min)
a2 <- aggregate(t_measure, 1, which.max)
a1
#Time Series:
#Start = 1
#End = 4
#Frequency = 1
# station1 station2 station3
#1 7 6 9
#2 12 12 12
#3 2 8 6
#4 1 11 1
a2
#Time Series:
#Start = 1
#End = 4
#Frequency = 1
# station1 station2 station3
#1 12 11 12
#2 1 3 1
#3 12 12 12
#4 12 3 10
If you want index for min with reference to original data_measure dataframe we can do
vals <- nrow(t_measure)/12
index_min <- a1 + (12 * (seq_len(vals) - 1))
index_min
#Time Series:
#Start = 1
#End = 4
#Frequency = 1
# station1 station2 station3
#1 7 6 9
#2 24 24 24
#3 26 32 30
#4 37 47 37
This can be read as for station1 in 1st 12 hour interval max value is present in 7th row of data_measure, for next 12 hour interval it is present in 24th row and same for other stations.

Inserting value in NAs repetitively in order [duplicate]

This question already has answers here:
Replacing NAs with latest non-NA value
(21 answers)
Closed 5 years ago.
I have a problem of making my data complete. Below is my data
> head(DF1)
# A tibble: 6 x 4
Date Coalprice Gasprice Co2emissionprice
<date> <dbl> <dbl> <dbl>
1 2015-12-31 47.45 14.40 8.22
2 2015-12-30 47.45 14.30 8.22
3 2015-12-29 47.40 15.40 8.27
4 2015-12-28 47.00 14.42 8.32
5 2015-12-25 47.00 14.20 8.22
6 2015-12-24 47.00 14.20 8.22
So data goes down all the way down to 2011-01-01 from 2015-12-31. But now, if you look carefully, my data has regular missing values. Every weekend's value is missing. So I want to put the prices for weekends as well to fill up NA. What I want to do is fill up every weekend (Sat and Sun) with the same prices on a day before every weekend, so Friday.
So in this example, 2015-12-25' prices 47 14.20 8.22 will go to Sat and Sun as well. Then next weekend's prices will be the same as Friday in that week.
Can you guys help me out with syntax?
Thank you very much for your advice.
dput info is below:
> dput(head(DF1, 30))
structure(list(Date = structure(c(16800, 16799, 16798, 16797,
16794, 16793, 16792, 16791, 16790, 16787, 16786, 16785, 16784,
16783, 16780, 16779, 16778, 16777, 16776, 16773, 16772, 16771,
16770, 16769, 16766, 16765, 16764, 16763, 16762, 16759), class = "Date"),
Coalprice = c(47.45, 47.45, 47.4, 47, 47, 47, 47, 47.6, 47.6,
47.8, 47.75, 47.75, 47.7, 47.65, 47.35, 47.4, 47.45, 47.4,
47.75, 48.55, 48.95, 49.1, 49.7, 49.95, 50.3, 53.85, 53.95,
53.95, 54, 54.35), Gasprice = c(14.4, 14.3, 15.4, 14.42,
14.2, 14.2, 13.93, 13.85, 14.35, 14.9, 15.5, 15.25, 15.95,
16.08, 16.23, 16.5, 16.65, 16.75, 16.78, 17.15, 17.15, 17.85,
17.95, 18.2, 17.7, 17.7, 17.88, 17.7, 17.6, 17.5), Co2emissionprice = c(8.22,
8.22, 8.27, 8.32, 8.22, 8.22, 8.22, 8.25, 8.18, 8.07, 8.07,
8.12, 8.19, 8.09, 8.07, 8.36, 8.4, 8.42, 8.42, 8.52, 8.58,
8.49, 8.55, 8.58, 8.56, 8.58, 8.62, 8.65, 8.56, 8.51)), .Names = c("Date",
"Coalprice", "Gasprice", "Co2emissionprice"), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))
You can use tidyr packages to do this in a single line.
library(tidyr)
df <- fill(df, contains("price"), .direction = "down")

R: Unable to get reasonable estimates from optim()

Here is my data:
test <- structure(list(date = structure(c(16436, 16437, 16438, 16439,
16440, 16441, 16442, 16443, 16444, 16445, 16446, 16447, 16448,
16449, 16450, 16451, 16452, 16453, 16454, 16455, 16456, 16457,
16458, 16459, 16460, 16461, 16462, 16463, 16464, 16465, 16466,
16467, 16468, 16469, 16470, 16471, 16472, 16473, 16474, 16475,
16476, 16477, 16478, 16479, 16480, 16481, 16482, 16483, 16484,
16485, 16486, 16487, 16488, 16489, 16490, 16491, 16492, 16493,
16494, 16495, 16496, 16497, 16498, 16499, 16500, 16501, 16502,
16503, 16504, 16505, 16506, 16507, 16508, 16509, 16510, 16511,
16512, 16513, 16514, 16515, 16516, 16517, 16518, 16519, 16520,
16521, 16522, 16523, 16524, 16525, 16526, 16527, 16528, 16529,
16530, 16531, 16532, 16533, 16534, 16535, 16536, 16537, 16538,
16539, 16540, 16541, 16542, 16543, 16544, 16545, 16546, 16547,
16548, 16549, 16550, 16551, 16552, 16553, 16554, 16555, 16556,
16557, 16558, 16559, 16560, 16561, 16562, 16563, 16564, 16565,
16566, 16567, 16568, 16569, 16570, 16571, 16572, 16573, 16574,
16575, 16576, 16577, 16578, 16579, 16580, 16581, 16582, 16583,
16584, 16585, 16586, 16587, 16588, 16589, 16590, 16591, 16592,
16593, 16594, 16595, 16596, 16597, 16598, 16599, 16600, 16601,
16602, 16603, 16604, 16605, 16606, 16607, 16608, 16609, 16610,
16611, 16612, 16613, 16614, 16615, 16616, 16617, 16618, 16619,
16620, 16621, 16622, 16623, 16624, 16625, 16626, 16627, 16628,
16629, 16630, 16631, 16632, 16633, 16634, 16635, 16636, 16637,
16638, 16639, 16640, 16641, 16642, 16643, 16644, 16645, 16646,
16647, 16648, 16649, 16650, 16651, 16652, 16653, 16654, 16655,
16656, 16657, 16658, 16659, 16660, 16661, 16662, 16663, 16664,
16665, 16666, 16667, 16668, 16669, 16670, 16671, 16672, 16673,
16674, 16675, 16676, 16677, 16678, 16679, 16680, 16681, 16682,
16683, 16684, 16685, 16686, 16687, 16688, 16689, 16690, 16691,
16692, 16693, 16694, 16695, 16696, 16697, 16698, 16699, 16700,
16701, 16702, 16703, 16704, 16705, 16706, 16707, 16708, 16709,
16710, 16711, 16712, 16713, 16714, 16715, 16716, 16717, 16718,
16719, 16720, 16721, 16722, 16723, 16724, 16725, 16726, 16727,
16728, 16729, 16730, 16731, 16732, 16733, 16734, 16735, 16736,
16737, 16738, 16739, 16740, 16741, 16742, 16743, 16744, 16745,
16746, 16747, 16748, 16749, 16750, 16751, 16752, 16753, 16754,
16755, 16756, 16757, 16758, 16759, 16760, 16761, 16762, 16763,
16764, 16765, 16766, 16767, 16768, 16769, 16770, 16771, 16772,
16773, 16774, 16775, 16776, 16777, 16778, 16779, 16780, 16781,
16782, 16783, 16784, 16785, 16786, 16787, 16788, 16789, 16790,
16791, 16792, 16793, 16794, 16795, 16796, 16797, 16798, 16799,
16800), class = "Date"), radn = c(9.66, 9.54, 8.21, 5, 5.98,
9.39, 8.54, 9.68, 6.74, 2.95, 9.24, 7.39, 10.47, 9.04, 7.1, 4.12,
6.42, 6.89, 10.96, 9.49, 11.72, 8.83, 11.48, 11.42, 11.49, 10.98,
2.87, 11.92, 8.92, 4, 12.92, 8.37, 5.73, 4.47, 8.73, 5.76, 9.34,
10.41, 6.72, 8.44, 13.34, 11.95, 12.2, 10.94, 10.5, 15.72, 14.63,
15.67, 15.91, 14.79, 14.11, 15.89, 17.07, 17.62, 17.22, 14.93,
11.17, 4.83, 8.78, 17.46, 10.35, 19.09, 19.39, 19.48, 19.12,
18.94, 19.93, 20.24, 17.47, 6.07, 19.4, 18.26, 10, 6.33, 10.67,
15.2, 21.39, 22.43, 18.02, 19.4, 18.55, 14.91, 9.15, 21.84, 22.8,
23.16, 23.43, 24.16, 22.56, 23.58, 23.45, 25.09, 25.46, 22.85,
17.05, 23.87, 12.45, 8.88, 25.7, 25.86, 17.28, 24.77, 25.08,
15.62, 27.4, 27.35, 27.71, 26.91, 27.93, 27.99, 26.42, 20.49,
27.9, 11.89, 10.38, 28.43, 28.74, 29.2, 27.62, 28.88, 28.81,
28.92, 29.07, 24.41, 29.1, 26.43, 18, 23.94, 30.68, 29.47, 18.88,
18.58, 25.79, 18.76, 12.18, 12.92, 20.18, 10.75, 14.09, 19.86,
19.47, 15.9, 12.82, 22.62, 21.23, 24.62, 29.5, 30.21, 30.12,
21.87, 25.45, 31.68, 32.18, 29.67, 17.27, 22.41, 24.28, 31.27,
30, 30.12, 21.6, 32.76, 32.27, 32.24, 32.81, 32.45, 32.66, 30.52,
30.5, 32.68, 32.85, 30.42, 32.62, 32.45, 31.29, 32.15, 25.84,
26.21, 27.22, 26.36, 30.72, 26.26, 24.34, 21.45, 18.58, 25.95,
29.09, 21.53, 21.88, 20.76, 17.56, 24.69, 22.83, 27.72, 28.07,
31.18, 30.23, 28.86, 30.61, 30.79, 30.08, 27.28, 16.81, 23.82,
30.09, 30.29, 30.45, 30.8, 31.12, 30.89, 30.19, 25.01, 24.27,
18.93, 28.27, 26.62, 27.97, 22.9, 11.1, 22.29, 24.4, 27.78, 28.17,
28.41, 26.01, 27.18, 25.08, 26.65, 27.95, 27.67, 24.39, 26.59,
26.9, 26.54, 26.02, 25.31, 26.03, 22.22, 24.29, 21.01, 19.73,
23.03, 25.38, 24.98, 24.74, 19.75, 20.24, 24.99, 21.01, 24.53,
24.3, 23.95, 23.36, 22.92, 20.66, 15.42, 6.66, 15.28, 16.1, 16.73,
22.14, 22.02, 21.59, 21.4, 21.41, 21.45, 15.48, 17.78, 19.93,
15.58, 19.22, 17.29, 8.64, 8.94, 15.46, 12.52, 17.79, 18.36,
18.28, 15.27, 13.04, 13.78, 17.88, 17.88, 17.5, 17.31, 16.84,
14.55, 15.17, 7.43, 4.34, 5.23, 12.79, 15.84, 13.32, 15.43, 11.48,
6.13, 14.64, 9.04, 5.09, 11.84, 9.86, 11.4, 4.92, 2.81, 5.76,
7.92, 9.15, 13.14, 13.14, 9.94, 9.77, 11.15, 12.45, 12.33, 11.99,
11.8, 6.92, 11.23, 6.2, 9.6, 4.89, 11.43, 11.05, 10.83, 7.44,
5.4, 6.17, 3.52, 10.71, 10.64, 10.67, 10.6, 10.17, 6.02, 6.96,
6.5, 7.43, 3.49, 2.03, 5.22, 5.02, 4.24, 4.44, 5.52, 2.72, 3.75,
2.31, 8.38, 1.88, 3.07, 2.02, 2.66, 1.67, 5.77, 7.59, 1.9, 1.5,
9.72, 2.66, 2.39, 1.67, 2.38, 9.88), maxt = c(-4.4, -1.9, 0.8,
4.8, 6.8, 11, 13, 12.6, 11.4, 7, 5.8, 10, 7.2, 6.5, 5.9, 5.5,
10.4, 12, 15.6, 11.2, 7.1, 6.3, 6.5, 9.4, 12.8, 14.6, 14.3, 7.8,
11.9, 9.6, 4.5, 10.8, 13.2, 11.4, 14, 14.8, 14.9, 16.3, 17.2,
15.4, 13.3, 12.4, 15.1, 17.6, 19.6, 19.8, 15.1, 12.8, 15.9, 18.7,
18, 13.1, 10.6, 6, 7.6, 12.7, 14, 9.2, 8.3, 7.1, 9.5, 10, 6,
10.1, 15.5, 18.4, 19.9, 19.6, 19.9, 21.5, 13.9, 17, 20.5, 20.6,
22.7, 18.4, 18.5, 16, 19.9, 22.2, 19.1, 19.3, 12.6, 11.7, 17.1,
22.2, 26.5, 19.7, 22.9, 26.3, 20.7, 12.2, 12.4, 16.3, 17.4, 12.7,
12.7, 13, 11.4, 16.4, 20.6, 16.6, 18.4, 24.4, 11.7, 11.8, 18.6,
23, 21.9, 23.3, 24.6, 26, 22.5, 21.6, 13.2, 11.9, 14.8, 21.2,
25.8, 25.5, 22.6, 26.7, 27.6, 26.9, 27.2, 24.2, 18.6, 14.1, 20.5,
21.6, 24.2, 22.6, 20.9, 19.6, 16.9, 14.8, 17.1, 20.6, 18.3, 16.9,
20.2, 21.2, 19.6, 19.2, 22.6, 24, 23.9, 25.6, 27.1, 29.3, 30.2,
31.6, 26.4, 24.7, 25.2, 21, 25.9, 26.4, 30.7, 33.4, 34.7, 29,
30.5, 32.3, 31.9, 32.6, 32.6, 32.7, 33.6, 34, 31.6, 32.4, 31.4,
31.5, 33.7, 35.9, 37.1, 38.8, 39.2, 38.9, 37.8, 38.4, 38.3, 38.6,
37.2, 35.7, 27.9, 33.4, 32.7, 27.5, 29.2, 26.3, 26.9, 28, 29.1,
31.1, 32, 33.1, 29.4, 29.2, 32.3, 34, 33, 29, 29.3, 30.8, 31.5,
30.4, 24.9, 28.5, 33.6, 36.3, 37.7, 38.2, 34.5, 33.2, 33.9, 29.2,
32.3, 25.4, 28.8, 32.4, 32.9, 34.9, 34.6, 36.2, 34.5, 32, 34.1,
33.7, 33.3, 34.8, 34.5, 32.7, 32.3, 35.7, 35.3, 35, 34.2, 33.5,
33.9, 31.4, 27.6, 30.9, 32.2, 30.5, 25.9, 23.5, 19.6, 24.1, 28.1,
30.8, 33.2, 34.8, 35.8, 35.4, 33.5, 27.7, 21.7, 19.4, 20.1, 23.7,
28.5, 31.5, 31.6, 31, 29.3, 31.2, 32.6, 30.5, 28.6, 29.8, 30.9,
26.8, 21.1, 21.8, 20.4, 22.5, 24.9, 26.7, 27.1, 28, 30.7, 29.6,
25.5, 29.3, 30.4, 30.8, 30.5, 29, 22, 18, 13.1, 16, 19, 19.1,
19.3, 20.1, 20, 20.4, 18.6, 15.2, 13.7, 17.1, 22.3, 18.1, 6.3,
6, 5.7, 7.1, 10.3, 11.1, 14.2, 8, 7.1, 8.9, 10.7, 12.3, 14.8,
10.8, 3.2, 7.6, 12.6, 14.4, 9.6, 10.6, 11.7, 12.3, 13.4, 1.3,
-0.9, -0.2, 0.6, 2.5, 4, 5.4, 7.3, 13, 8, 6.7, 11.5, 13.2, 14.2,
14.9, 12.3, 5.5, 6.1, 11.1, 0.3, 0.5, 2, 2.8, 7, 4.9, 2.4, 7.3,
6.2, 2.9, 0.5, -1.2, -2.5, -4, -2.7, -1.1, -3), mint = c(-15.9,
-16.5, -14.4, -11.2, -5.7, -2.4, -2.5, -3.2, -4.3, -4.6, -1.5,
-1, -0.9, -6.3, -7, -5.7, -1.2, -0.9, 0.3, -2.7, -5.9, -10.1,
-8.7, -7.3, -5.7, -3.5, -1.2, -0.4, -0.9, -0.7, -4.3, -4.3, -2.8,
1, 2.7, 3.1, 5.8, 6.2, 3.8, 2.2, -0.7, -1.5, -0.9, -0.3, 1, 1,
-1.6, -3.8, -3.9, -1.9, -0.6, -0.8, -3.8, -7, -8.8, -7, -2.2,
-0.3, -1.1, -2.9, -5.1, -5.2, -9.2, -9.7, -6.9, -4.2, -3.1, -3.5,
-3.8, -2.3, 3.5, 0.3, 0.7, 5.8, 7, 7.4, 2.3, -0.6, -2.2, 0.7,
0.9, 1.6, 3.8, -0.9, -2.5, 1, 2.6, 1.8, -1.6, 2.3, -4.2, -6.6,
-4.7, -4.2, -0.5, -1.4, -3, 0.3, -2.9, -2.3, 1.1, -0.4, -1.5,
0.5, -6.1, -7.3, -5, -0.5, 0.6, 0.7, 1.2, 2.9, 4.3, 4.7, 2.1,
0.3, 0.5, 1.4, 3.4, 5, 4.9, 4.2, 6.3, 6.7, 6, 6.3, 3.6, 3.5,
3.7, 1.1, 1.9, 4.9, 0.7, 1.2, 5.8, 5.6, 4, 6.2, 8.3, 7, 6, 4.7,
7, 9.2, 8.1, 6.9, 7.9, 8.6, 9.6, 9.4, 10.3, 10.4, 9.6, 8.2, 9.4,
9.8, 7.2, 9.4, 10.8, 12.4, 14.5, 11.8, 11, 10.7, 11.3, 10.8,
9.7, 10.4, 10.6, 12.1, 10.3, 10.5, 11.3, 10, 12.6, 13.6, 17.4,
19.9, 19.9, 18.9, 18.4, 18.9, 20.1, 19, 17, 16.9, 14.8, 13.1,
14, 11.5, 10.6, 11.1, 12.7, 11.4, 11.9, 12.5, 13.3, 13.6, 13.2,
11.8, 11.8, 12.6, 15, 11.4, 10, 9.6, 9.3, 9.3, 8.2, 9.6, 9.7,
12, 14.3, 16.1, 16.5, 12.8, 13.7, 11.3, 10.3, 12.2, 11.4, 11.8,
11.1, 10.9, 11.2, 13, 11.8, 9, 9.7, 8.9, 10.1, 10, 11.5, 10.6,
12.2, 10.9, 12.6, 11.9, 11.9, 13.1, 13.4, 11.4, 6.9, 6, 7.7,
9.7, 7.8, 2.2, 1.5, 0.9, 2.3, 4.8, 6.3, 8.3, 10.4, 11.2, 12.8,
11, 7.5, 6.1, 5.5, 2.4, 3.5, 5.8, 5.9, 6.2, 5.6, 6.1, 7.4, 9.9,
7.8, 6.4, 7.8, 11, 10.1, 4.8, 3.5, 6.6, 4.6, 5.5, 5.9, 9.8, 8.3,
8.6, 6.4, 4.4, 6, 7.1, 6.9, 7.5, 7.8, 6.9, 3.9, 1.8, 0.3, 0.3,
-0.5, 3.2, 2.4, -0.3, 0.2, 5.1, -1.5, -1.4, 4.7, 5.6, 1.6, -1.3,
-3.8, -4.1, -4.6, -3.5, -0.8, -1.4, -6.5, -6, -5, -4.9, -3.9,
-4.2, -6.1, -1.7, -0.2, -0.3, -3.6, -7.1, -6.4, -3.4, -5.2, -8.6,
-9.6, -13.8, -16.3, -15.6, -14.5, -11.8, -4.6, 0, -7.6, -7.7,
-1.3, 4.8, 4.6, 2.3, 0.1, -2.2, -1.4, -2.6, -4.7, -9, -6.8, -4.4,
-3.7, -3.9, -5.1, 0, -1.8, -3.2, -9, -14.2, -17.4, -13, -8.2,
-12.7, -17.5), rain = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.8,
0.96, 0, 0, 0, 1.38, 0.25, 0.32, 0, 0, 0, 0, 0, 0, 0, 0, 5.68,
0, 0, 0, 0, 0, 1.12, 0, 0, 0, 4.24, 0.13, 6.84, 1.44, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.28, 2.13, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.65,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.65, 0, 3.6, 1.9, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.59, 1.19, 11.03, 5.43, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.77, 0, 0, 0, 0, 0, 5.06, 5.6,
0.01, 2.23, 5.45, 7.43, 4.47, 0.11, 4.02, 6.36, 0.38, 0.79, 1.46,
0, 0, 0, 0, 0, 0, 0, 0, 0.82, 3.06, 0.06, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.37, 0,
2.3, 1.74, 3.2, 1.72, 3.53, 2, 1.08, 0.46, 0.38, 0.3, 0, 0, 0,
0.47, 0, 0, 0.56, 4.86, 9.66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.86,
0, 0, 0, 0, 2.44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.55, 0.83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16.08,
0.93, 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.24, 4.25, 14.52,
13.45, 0, 0, 0, 0, 0, 0, 0, 0, 1.2, 1.23, 0, 0, 4.15, 11.05,
2.29, 0, 0, 0, 0, 0.77, 3.04, 0, 0, 0, 0, 0, 0.88, 0, 0, 0, 0,
0, 0, 0, 0, 0.94, 0, 0, 0, 0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0,
0.66, 1.85, 0.95, 0.61, 3.89, 0, 0, 1.23, 4.81, 0, 1.96, 1.67,
6.94, 9.65, 0, 1.99, 0, 0, 2.24, 2.67, 0.16, 0.52), evap = c(8.48,
8.48, 8.48, 8.48, 8.48, 8.48, 8.48, 8.31, 8.31, 8.31, 8.31, 8.31,
8.31, 8.31, 8.09, 8.09, 8.09, 8.09, 8.09, 8.09, 8.09, 7.86, 7.86,
7.86, 7.86, 7.86, 7.86, 7.86, 7.62, 7.62, 7.62, 7.62, 7.62, 7.62,
7.62, 7.39, 7.39, 7.39, 7.39, 7.39, 7.39, 7.39, 7.16, 7.16, 7.16,
7.16, 7.16, 7.16, 7.16, 6.93, 6.93, 6.93, 6.93, 6.93, 6.93, 6.93,
6.71, 6.71, 6.71, 6.71, 6.71, 6.71, 6.71, 6.48, 6.48, 6.48, 6.48,
6.48, 6.48, 6.48, 6.23, 6.23, 6.23, 6.23, 6.23, 6.23, 6.23, 5.96,
5.96, 5.96, 5.96, 5.96, 5.96, 5.96, 5.66, 5.66, 5.66, 5.66, 5.66,
5.66, 5.66, 5.32, 5.32, 5.32, 5.32, 5.32, 5.32, 5.32, 4.95, 4.95,
4.95, 4.95, 4.95, 4.95, 4.95, 4.56, 4.56, 4.56, 4.56, 4.56, 4.56,
4.56, 4.15, 4.15, 4.15, 4.15, 4.15, 4.15, 4.15, 3.75, 3.75, 3.75,
3.75, 3.75, 3.75, 3.75, 3.38, 3.38, 3.38, 3.38, 3.38, 3.38, 3.38,
3.05, 3.05, 3.05, 3.05, 3.05, 3.05, 3.05, 2.78, 2.78, 2.78, 2.78,
2.78, 2.78, 2.78, 2.58, 2.58, 2.58, 2.58, 2.58, 2.58, 2.58, 2.45,
2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.37, 2.37, 2.37, 2.37, 2.37,
2.37, 2.37, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.38, 2.38,
2.38, 2.38, 2.38, 2.38, 2.38, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46,
2.46, 2.57, 2.57, 2.57, 2.57, 2.57, 2.57, 2.57, 2.72, 2.72, 2.72,
2.72, 2.72, 2.72, 2.72, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 3.1,
3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.33, 3.33, 3.33, 3.33, 3.33, 3.33,
3.33, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.83, 3.83, 3.83,
3.83, 3.83, 3.83, 3.83, 4.13, 4.13, 4.13, 4.13, 4.13, 4.13, 4.13,
4.47, 4.47, 4.47, 4.47, 4.47, 4.47, 4.47, 4.85, 4.85, 4.85, 4.85,
4.85, 4.85, 4.85, 5.26, 5.26, 5.26, 5.26, 5.26, 5.26, 5.26, 5.67,
5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 6.08, 6.08, 6.08, 6.08, 6.08,
6.08, 6.08, 6.46, 6.46, 6.46, 6.46, 6.46, 6.46, 6.46, 6.79, 6.79,
6.79, 6.79, 6.79, 6.79, 6.79, 7.09, 7.09, 7.09, 7.09, 7.09, 7.09,
7.09, 7.35, 7.35, 7.35, 7.35, 7.35, 7.35, 7.35, 7.6, 7.6, 7.6,
7.6, 7.6, 7.6, 7.6, 7.84, 7.84, 7.84, 7.84, 7.84, 7.84, 7.84,
8.07, 8.07, 8.07, 8.07, 8.07, 8.07, 8.07, 8.28, 8.28, 8.28, 8.28,
8.28, 8.28, 8.28, 8.46, 8.46, 8.46, 8.46, 8.46, 8.46, 8.46, 8.58,
8.58, 8.58, 8.58, 8.58, 8.58, 8.58, 8.63, 8.63, 8.63, 8.63, 8.63,
8.63, 8.63, 8.6, 8.6, 8.6, 8.6, 8.6, 8.6, 8.6, 8.6), index = 8767:9131), .Names = c("date",
"radn", "maxt", "mint", "rain", "evap", "index"), na.action = structure(1L, .Names = "1", class = "omit"), row.names = 8768:9132, class = "data.frame")
I am trying to optimize a function to it to simulate some data. I have done this in the past with other datasets with success, but with this data optim is converging but visually the fit is terrible. I do a much better job using guess and check. Here I am looking at minimum temperature. I have many years of data, but in the interest of space I only included 1 year.
Here is my optimization code:
TMIN <- function(a,b,x){a*sin(b*x)}
plot(h$mint~h$index,type='l')
curve(TMIN(x, a=20, b=.017),add=TRUE, col="red")
normTMIN<-function(params,k){
a=params[1]
b=params[2]
c=params[3]
Mean<-mean(a*sin(b*k))
-sum(dnorm(k,mean=Mean,sd=c,log=TRUE)) #shape= Mean(a,b)/scale
}
optTMIN <- optim(par=c(a=60,b=.017,c=1),k=test$mint,fn=normTMIN) #par doesn't equal params
optTMIN
curve(TMIN(optTMIN$par[1],optTMIN$par[2],x), add=TRUE,col="blue")
I can't figure out why optim is going so terribly wrong. Thanks in advance.
Do you want to do something like the following (find list square estimate):
head(test)
TMIN <- function(a,b,x){a*sin(b*x)}
plot(test$mint~test$index,type='l')
curve(TMIN(x, a=20, b=.017),add=TRUE, col="red")
normTMIN<-function(params,k,x){
a=params[1]
b=params[2]
sum((k - TMIN(a,b,x))^2)
}
optTMIN <- optim(par=c(a=1,b=0.001),k=test$mint,x=test$index,fn=normTMIN, control=list(trace = TRUE)) #par doesn't equal params
optTMIN
curve(TMIN(optTMIN$par[1],optTMIN$par[2],x), add=TRUE,col="blue")
#$par
# a b
#10.97271664 0.01349994

R levelplot and interpolation [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
some days ago I managed to make levelplots, with interpolation, with the following command within a script:
levelplot(jan~lon*lat,APM,main="Jan",panel=panel.levelplot.raster,interpolate=T)
I accidentaly did not keep that session, only the script, so it is not possible for me to go through the history and recover all the commands I had used.
Now, after loading latticeExtra, the same command produces an empty plot. On the other hand, leaving out the last two elements of the instruction, i.e.:
levelplot(jan~lon*lat,APM,main="Jan")
The graph is drawn.
I'd like to have interpolation at the surface, but something is going wrong with
panel=panel.levelplot.raster
the same behaviour happens when rasterVis is loaded.
I guess I am missing something... any help?
The data has the following structure:
> head(APM)
lat lon jan feb mar apr may jun jul aug sep oct nov dec
1 -18.5 10.5 29.7 28.8 25.6 25.6 26.8 29.9 35.5 46.8 35.5 27.5 27.5 27.9
2 -17.5 10.5 28.8 29.8 26.3 26.2 27.8 31.6 39.7 63.1 40.4 27.6 27.6 28.3
3 -16.5 10.5 28.7 30.0 26.9 26.8 28.6 32.1 41.1 109.4 42.8 29.7 28.9 29.4
4 -15.5 10.5 28.4 29.5 27.5 26.9 29.1 34.2 46.4 109.5 40.8 29.7 29.7 28.5
5 -14.5 10.5 28.2 29.3 27.4 27.8 27.8 42.8 60.7 104.3 49.1 29.4 28.8 28.6
6 -13.5 10.5 27.8 28.4 27.7 28.3 29.8 41.2 102.8 105.7 47.8 29.5 28.5 28.0
totalling 224 cells listed by latitude and longitude:
> dput(APM)
structure(list(lat = c(-18.5, -17.5, -16.5, -15.5, -14.5, -13.5,
-12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5,
-16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
-7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5,
-11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5,
-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5,
-5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5,
-10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5,
-14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5,
-18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,
-9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5,
-13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5,
-17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5,
-8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5,
-12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5,
-16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
-7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5,
-11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5,
-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5,
-5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5,
-10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5,
-14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5,
-18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,
-9.5, -8.5, -7.5, -6.5, -5.5), lon = c(10.5, 10.5, 10.5, 10.5,
10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 11.5,
11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5,
11.5, 11.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5,
13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 14.5, 14.5, 14.5,
14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5,
15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5,
15.5, 15.5, 15.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5,
16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 17.5, 17.5, 17.5, 17.5, 17.5,
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 18.5, 18.5,
18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5,
18.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5,
19.5, 19.5, 19.5, 19.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5,
20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 21.5, 21.5, 21.5, 21.5,
21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 22.5,
22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5,
22.5, 22.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5,
23.5, 23.5, 23.5, 23.5, 23.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5,
24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5
), jan = c(29.7, 28.8, 28.7, 28.4, 28.2, 27.8, 28, 29.4, 30.3,
32.5, 33.2, 33.5, 33.1, 34.3, 28.4, 29.4, 29.6, 29, 28.9, 28.8,
28.5, 28.9, 29.9, 31, 32.2, 32.9, 35.8, 37.4, 30, 27.4, 29.6,
30, 28.4, 30.6, 30.4, 29.9, 30.2, 31.3, 33.9, 35.5, 35.8, 34.5,
30.3, 23.8, 25.2, 23.2, 22.9, 24, 27.2, 29.3, 31.6, 32.3, 31,
31.4, 37.3, 37.4, 24.9, 25.1, 23.4, 21.2, 23.5, 22.8, 23.5, 24.5,
26, 27.7, 28.6, 33.3, 37.3, 40.7, 37.9, 38.5, 27.1, 22.5, 24.7,
23.3, 24.7, 24.8, 26.6, 27.4, 30.7, 33.9, 35.9, 37.6, 30.7, 29.5,
26.6, 24.1, 24.1, 25.4, 25.4, 25.7, 28, 28.2, 32.9, 36.3, 35.2,
40.4, 22.7, 25.5, 26.5, 24.6, 24.3, 24, 25.8, 26.7, 29.4, 31.8,
35.4, 38, 37.4, 41.3, 22.2, 23.7, 26.8, 25.8, 25.3, 24, 25.1,
26.9, 29.6, 31.8, 34.4, 35.6, 39.3, 40.1, 26.2, 26.2, 26, 25.8,
25.3, 23.2, 24.5, 25.9, 26.9, 30.8, 33.3, 38.3, 40.2, 41.2, 26.9,
26.4, 27.1, 23.1, 22.9, 24, 28.5, 26.9, 27.1, 31, 32.7, 36.6,
38, 41.6, 26.6, 27.2, 27.3, 26, 23.6, 25.8, 33.2, 33.8, 25.8,
28.1, 31.6, 34.7, 35.3, 38.5, 27.3, 28.1, 28.6, 27, 31.5, 31.8,
29.9, 27.6, 25.4, 28.4, 29.8, 32.2, 36.4, 36.8, 28.2, 26.2, 27.3,
27, 27.1, 23.7, 23.5, 25.3, 26.1, 29, 29.3, 29.3, 36.4, 35.1,
24.2, 25.1, 23.5, 23.1, 24, 24.2, 24.3, 26.7, 26.7, 26.8, 29.3,
30.4, 33.3, 33.4, 24.8, 24.3, 24.5, 24.8, 26.1, 24.6, 25, 25.3,
27.2, 26.8, 28.9, 30.5, 31.2, 33.1), feb = c(28.8, 29.8, 30,
29.5, 29.3, 28.4, 28.6, 28.7, 28.7, 28.5, 29.4, 32.2, 34.4, 37.4,
29.4, 30.5, 30.8, 30.7, 29.6, 28.3, 28, 28.4, 28.4, 29, 29.5,
31.4, 33.3, 37.1, 31.7, 26.5, 29.8, 30.4, 28.4, 28.9, 27.7, 27.8,
29.3, 30, 30.4, 31.7, 32.9, 34.7, 29.1, 23.5, 24.2, 23.3, 22.7,
23.6, 25.4, 26.4, 27.7, 28.1, 26.9, 27.5, 30.4, 31.6, 22.3, 24.7,
23.7, 21.2, 23.4, 22.5, 22.3, 21.7, 23.2, 23.3, 23.2, 27.5, 31.6,
34, 33, 33.3, 26.3, 22.5, 23.9, 24, 24.5, 23.7, 24.2, 24.5, 25.4,
27.8, 31, 33.4, 29.3, 27.5, 24.7, 23.2, 23.5, 24.7, 25.7, 25.4,
25.8, 25.6, 27.2, 30.7, 30.4, 35, 21.4, 23.3, 24.1, 24.7, 25.2,
25.2, 25.6, 25.9, 27.7, 26.9, 30.1, 31.5, 32, 34.5, 20.2, 22.4,
24.9, 25.1, 25.3, 25.5, 24.1, 25.8, 26.2, 28.4, 29.3, 30.9, 33.5,
35.6, 24.3, 24.2, 24, 24.5, 24.9, 24.7, 23.8, 25, 26.2, 27.1,
29.9, 31.3, 34.6, 34.7, 25.3, 23.6, 24.9, 22.6, 23.1, 23.1, 25.3,
26.8, 24.9, 25.1, 28.1, 30.7, 32.1, 34.2, 24, 23.4, 24.6, 25.6,
24.9, 26.4, 30.6, 31.2, 22.2, 23.9, 27.6, 30.8, 29.1, 29.7, 24,
23.6, 25.4, 25.8, 29.9, 29.5, 27.2, 26.3, 23.5, 24.5, 25.6, 27,
28.9, 28.3, 24.4, 22, 23.4, 24.9, 25.4, 22.7, 23, 23, 22.4, 23.7,
24.6, 24.9, 29.8, 28.5, 21.3, 22.8, 21.3, 21.1, 21.7, 22.8, 24.4,
23.2, 23.3, 22.4, 25.4, 26.3, 27.3, 27.1, 22.8, 22.1, 21.4, 21.9,
24.1, 24.1, 26, 23.5, 23.8, 22.5, 23, 24.9, 26.5, 28)), .Names = c("lat",
"lon", "jan", "feb"), row.names = c(NA,
-224L))
The following produces a plot in a fresh interactive session of R
APM <- results of dput(APM) from the OP
library(latticeExtra)
levelplot(jan~lon*lat,APM,main="Jan",panel=panel.levelplot.raster,interpolate=T)
or alternatively
levelplot(jan~lon*lat,APM,main="Jan",interpolate=TRUE, useRaster = TRUE)
If this is not working for you, check to see if you have the latest versions of the packages in question; for example,
R version 2.15.2 (2012-10-26)
latticeExtra_0.6-24
lattice_0.20-10
RColorBrewer_1.0-5
If the problem still is not resolved, there's something else that hasn't been mentioned in this post.

Resources