Ggplot2 doesn't scale Y axis - r

I'm having a problem regarding scaling Y axis on the ggplot2. I have a dataset (dane_dlugie) consisted of dates (Data) and river flow observations at three different spots (Osielec, Jordanów and Skawica Dolna). My goal is to plot flows from Osielec regarding the proper date.
My plot code looks like this:
ggplot(dane_dlugie, aes(x=Data, y=Osielec, group=1)) +
geom_line()+labs(x="Data", y="Flow") +
ggtitle("Osielec")+
scale_x_datetime(date_breaks = "1 day", date_labels = "%d-%m")
On the Y axis, I would like to have flow scale with the break of 1 m3/s. I've tried using the 'scale_y_discrete' however without any success.
Could anyone help me with that?
Please find below the reproducible example.
dane_dlugie <- structure(list(Data = structure(list(sec = c(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), min = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 0L), mday = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L), mon = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L),
year = c(110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L), wday = c(6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 0L), yday = c(120L, 120L, 120L,
120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L,
120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L, 120L,
120L, 121L), isdst = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), zone = c("CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST"), gmtoff = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst",
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), Osielec = c("1.281",
"1.294", "1.294", "1.281", "1.268", "1.281", "1.294", "1.333",
"1.32", "1.32", "1.333", "1.307", "1.333", "1.346", "1.346",
"1.359", "1.32", "1.32", "1.294", "1.5328", "2.0296", "2.1952",
"2.7541", "4.1775", "4.5983"), Jordanów = structure(c(124L, 124L,
118L, 115L, 108L, 108L, 108L, 115L, 103L, 111L, 127L, 120L, 122L,
120L, 116L, 125L, 122L, 111L, 122L, 206L, 258L, 236L, 234L, 266L,
281L), .Label = c("", "0,1672", "0,1696", "0,172", "0,1744",
"0,1768", "0,1792", "0,1816", "0,184", "0,1864", "0,1888", "0,1912",
"0,1936", "0,196", "0,1984", "0,2008", "0,2032", "0,2056", "0,208",
"0,2104", "0,2128", "0,2152", "0,2176", "0,22", "0,2224", "0,2248",
"0,2272", "0,2296", "0,232", "0,2344", "0,2368", "0,2392", "0,2416",
"0,244", "0,2464", "0,2488", "0,2512", "0,2536", "0,256", "0,2584",
"0,2608", "0,2632", "0,2656", "0,268", "0,2704", "0,2728", "0,2752",
"0,2776", "0,28", "0,2824", "0,2848", "0,2872", "0,2896", "0,2944",
"0,2968", "0,2992", "0,3016", "0,304", "0,3064", "0,3088", "0,3112",
"0,3136", "0,316", "0,3184", "0,3208", "0,3232", "0,3256", "0,3264",
"0,328", "0,3304", "0,3328", "0,333", "0,3352", "0,3354", "0,3362",
"0,3376", "0,34", "0,3424", "0,3448", "0,3472", "0,3496", "0,3497",
"0,352", "0,3544", "0,3568", "0,3588", "0,3592", "0,3616", "0,3623",
"0,364", "0,3664", "0,3688", "0,3712", "0,3736", "0,376", "0,3784",
"0,3808", "0,3832", "0,3856", "0,388", "0,3904", "0,3928", "0,394",
"0,3952", "0,3976", "0,4", "0,4042", "0,4076", "0,4084", "0,4126",
"0,4144", "0,4168", "0,4178", "0,421", "0,4212", "0,4246", "0,4252",
"0,428", "0,4294", "0,4314", "0,4336", "0,4348", "0,4378", "0,4382",
"0,4416", "0,442", "0,445", "0,4462", "0,4484", "0,4504", "0,4518",
"0,4546", "0,4552", "0,4588", "0,463", "0,4672", "0,4714", "0,4756",
"0,4798", "0,484", "0,4882", "0,4924", "0,4966", "0,5008", "0,505",
"0,5092", "0,5134", "0,5176", "0,5218", "0,526", "0,5302", "0,5344",
"0,5386", "0,5428", "0,547", "0,5512", "0,5554", "0,5596", "0,5638",
"0,568", "0,5722", "0,5764", "0,5806", "0,5848", "0,589", "0,5932",
"0,5974", "0,6016", "0,6058", "0,61", "0,6116", "0,6142", "0,6184",
"0,6226", "0,6268", "0,631", "0,6352", "0,6394", "0,6436", "0,6478",
"0,652", "0,6562", "0,6604", "0,6646", "0,6688", "0,673", "0,6772",
"0,6814", "0,6856", "0,6898", "0,694", "0,6982", "0,7024", "0,7066",
"0,7108", "0,715", "0,7192", "0,7234", "0,7276", "0,7318", "0,736",
"0,7402", "0,7444", "0,7486", "0,7528", "0,7556", "0,757", "0,7584",
"0,7612", "0,7654", "0,7696", "0,7738", "0,778", "0,7822", "0,7864",
"0,7906", "0,7948", "0,799", "0,8032", "0,8074", "0,8116", "0,8158",
"0,82", "0,8258", "0,8316", "0,8374", "0,8432", "0,849", "0,8548",
"0,8606", "0,8664", "0,8722", "0,878", "0,8804", "0,8838", "0,8856",
"0,8896", "0,8954", "0,9012", "0,907", "0,9128", "0,9186", "0,9244",
"0,9302", "0,936", "0,9418", "0,9476", "0,9534", "0,9592", "0,965",
"0,9708", "0,9766", "0,9824", "0,9882", "0,9916", "0,994", "0,9998",
"1,0052", "1,0056", "1,0064", "1,0114", "1,0172", "1,023", "1,0288",
"1,0346", "1,0364", "1,0404", "1,0462", "1,052", "1,0578", "1,0636",
"1,0694", "1,0752", "1,081", "1,0868", "1,0926", "1,0984", "1,1042",
"1,11", "1,1158", "1,1196", "1,1216", "1,1248", "1,1274", "1,1332",
"1,139", "1,1448", "1,1506", "1,156", "1,1564", "1,1622", "1,1664",
"1,168", "1,1738", "1,1796", "1,1854", "1,1912", "1,197", "1,2028",
"1,2086", "1,2144", "1,2184", "1,2202", "1,2236", "1,226", "1,2288",
"1,2318", "1,234", "1,2376", "1,2492", "1,2496", "1,2548", "1,255",
"1,26", "1,2608", "1,2666", "1,2724", "1,2754", "1,2782", "1,284",
"1,2898", "1,2908", "1,2956", "1,2985", "1,3014", "1,3072", "1,313",
"1,3139", "1,3188", "1,3246", "1,3293", "1,3304", "1,3362", "1,337",
"1,3415", "1,342", "1,3478", "1,3536", "1,3594", "1,3652", "1,3678",
"1,371", "1,3755", "1,3768", "1,3826", "1,3884", "1,3942", "1,4",
"1,408", "1,414", "1,416", "1,4217", "1,424", "1,432", "1,4371",
"1,44", "1,4448", "1,448", "1,4525", "1,4602", "1,464", "1,4679",
"1,472", "1,4756", "1,48", "1,4833", "1,488", "1,491", "1,496",
"1,4987", "1,5064", "1,512", "1,5141", "1,52", "1,528", "1,536",
"1,5372", "1,544", "1,5449", "1,552", "1,5526", "1,56", "1,5603",
"1,568", "1,5757", "1,576", "1,5834", "1,584", "1,5911", "1,592",
"1,6065", "1,608", "1,616", "1,6219", "1,624", "1,632", "1,64",
"1,648", "1,6527", "1,664", "1,672", "1,68", "1,6835", "1,688",
"1,6912", "1,696", "1,6989", "1,704", "1,7066", "1,712", "1,7143",
"1,7297", "1,736", "1,744", "1,7451", "1,752", "1,7528", "1,76",
"1,768", "1,776", "1,7836", "1,784", "1,792", "1,799", "1,8",
"1,8067", "1,816", "1,8221", "1,824", "1,8298", "1,832", "1,8375",
"1,84", "1,8452", "1,848", "1,8529", "1,856", "1,864", "1,872",
"1,876", "1,888", "1,8914", "1,896", "1,8991", "1,904", "1,9068",
"1,912", "1,92", "1,9222", "1,928", "1,936", "1,9376", "1,944",
"1,9453", "1,952", "1,953", "1,96", "1,968", "1,9684", "1,976",
"1,984", "1,9915", "1,992", "10,0136", "10,014", "10,035", "10,098",
"10,14", "10,203", "10,371", "10,434", "10,455", "10,518", "10,539",
"10,56", "10,833", "10,854", "10,875", "10,938", "10,959", "11,064",
"11,106", "11,169", "11,184", "11,211", "11,232", "11,274", "11,337",
"11,358", "11,444", "11,576", "11,664", "11,686", "11,862", "11,884",
"12,104", "12,214", "12,236", "12,368", "12,434", "12,456", "12,72",
"12,786", "12,918", "12,94", "13,028", "13,05", "13,072", "13,094",
"13,182", "13,27", "13,314", "13,325", "13,424", "13,446", "13,578",
"13,715", "13,991", "14,014", "14,037", "14,083", "14,152", "14,29",
"14,451", "14,497", "14,635", "14,727", "14,819", "14,957", "15,003",
"15,164", "15,21", "15,225", "15,375", "15,417", "15,44", "15,601",
"15,67", "15,805", "15,808", "16,125", "16,15", "16,183", "16,291",
"16,35", "16,399", "16,45", "16,65", "16,725", "16,925", "17,15",
"17,225", "17,475", "17,775", "17,938", "18,1", "18,2", "18,325",
"18,35", "18,562", "18,778", "18,886", "19,021", "19,048", "19,102",
"19,237", "19,453", "19,588", "19,696", "19,885", "19,912", "2",
"2,0069", "2,008", "2,0146", "2,016", "2,024", "2,032", "2,04",
"2,0494", "2,056", "2,064", "2,0688", "2,072", "2,08", "2,088",
"2,0882", "2,096", "2,104", "2,1076", "2,112", "2,12", "2,127",
"2,128", "2,136", "2,1367", "2,144", "2,152", "2,16", "2,1658",
"2,168", "2,176", "2,184", "2,1852", "2,192", "2,2", "2,21",
"2,2143", "2,22", "2,224", "2,23", "2,2337", "2,24", "2,2434",
"2,25", "2,2531", "2,26", "2,27", "2,2725", "2,28", "2,29", "2,3",
"2,3016", "2,31", "2,3113", "2,32", "2,321", "2,33", "2,3404",
"2,35", "2,3598", "2,36", "2,3695", "2,37", "2,3889", "2,39",
"2,3986", "2,4", "2,41", "2,418", "2,4277", "2,43", "2,4374",
"2,44", "2,4471", "2,45", "2,4568", "2,46", "2,47", "2,4762",
"2,48", "2,4859", "2,49", "2,5053", "2,51", "2,515", "2,52",
"2,5247", "2,54", "2,5441", "2,55", "2,56", "2,5635", "2,58",
"2,5829", "2,59", "2,5926", "2,6", "2,61", "2,62", "2,63", "2,64",
"2,6411", "2,65", "2,6508", "2,66", "2,67", "2,6799", "2,68",
"2,69", "2,6993", "2,7", "2,71", "2,7187", "2,72", "2,7284",
"2,73", "2,7381", "2,7478", "2,75", "2,7575", "2,76", "2,7672",
"2,78", "2,7866", "2,79", "2,7963", "2,8", "2,81", "2,82", "2,8254",
"2,83", "2,8351", "2,84", "2,8448", "2,85", "2,8545", "2,86",
"2,8642", "2,87", "2,8739", "2,88", "2,89", "2,8933", "2,9",
"2,91", "2,9127", "2,92", "2,9224", "2,93", "2,94", "2,9418",
"2,95", "2,9515", "2,96", "2,97", "2,98", "2,9806", "2,99", "2,9903",
"20,101", "20,155", "20,182", "20,217", "20,506", "20,695", "21,43",
"21,52", "21,713", "21,94", "22,03", "22,09", "22,27", "22,63",
"22,78", "23,015", "23,046", "23,14", "23,59", "23,65", "23,98",
"24,298", "24,595", "24,727", "24,958", "25,189", "25,42", "26,113",
"26,179", "26,641", "26,74", "26,872", "26,971", "27,616", "27,832",
"28,336", "29,668", "29,956", "3", "3,01", "3,011", "3,02", "3,022",
"3,03", "3,033", "3,04", "3,044", "3,05", "3,055", "3,06", "3,07",
"3,077", "3,08", "3,088", "3,09", "3,099", "3,1", "3,11", "3,13",
"3,14", "3,143", "3,15", "3,154", "3,16", "3,165", "3,17", "3,176",
"3,18", "3,19", "3,198", "3,2", "3,209", "3,212", "3,224", "3,242",
"3,248", "3,253", "3,26", "3,264", "3,272", "3,275", "3,284",
"3,286", "3,296", "3,297", "3,308", "3,319", "3,32", "3,33",
"3,332", "3,341", "3,344", "3,352", "3,363", "3,368", "3,38",
"3,392", "3,396", "3,404", "3,407", "3,416", "3,428", "3,429",
"3,44", "3,451", "3,452", "3,462", "3,473", "3,488", "3,5", "3,506",
"3,512", "3,517", "3,524", "3,536", "3,539", "3,548", "3,56",
"3,561", "3,572", "3,583", "3,594", "3,596", "3,605", "3,608",
"3,616", "3,62", "3,632", "3,638", "3,649", "3,656", "3,668",
"3,68", "3,682", "3,692", "3,693", "3,704", "3,715", "3,716",
"3,728", "3,74", "3,752", "3,759", "3,764", "3,776", "3,788",
"3,792", "3,8", "3,812", "3,814", "3,824", "3,825", "3,836",
"3,847", "3,86", "3,872", "3,884", "3,891", "3,896", "3,908",
"3,932", "3,956", "3,968", "3,98", "3,99", "3,992", "30,028",
"30,532", "30,676", "30,892", "31,533", "31,798", "31,943", "32,189",
"32,763", "32,927", "33,009", "33,173", "33,296", "33,46", "33,583",
"33,73", "33,788", "34,948", "35,241", "35,57", "36,181", "36,369",
"37,027", "37,121", "37,5", "38,202", "38,343", "38,437", "38,484",
"38,531", "39,001", "39,236", "39,659", "39,753", "4,001", "4,004",
"4,028", "4,04", "4,045", "4,056", "4,064", "4,076", "4,088",
"4,089", "4,1", "4,112", "4,121", "4,124", "4,136", "4,139",
"4,16", "4,172", "4,178", "4,191", "4,204", "4,22", "4,232",
"4,243", "4,256", "4,268", "4,28", "4,282", "4,304", "4,308",
"4,316", "4,321", "4,328", "4,34", "4,352", "4,364", "4,388",
"4,4", "4,425", "4,428", "4,442", "4,456", "4,484", "4,498",
"4,526", "4,529", "4,54", "4,554", "4,582", "4,594", "4,596",
"4,607", "4,624", "4,638", "4,652", "4,666", "4,685", "4,694",
"4,708", "4,722", "4,736", "4,75", "4,764", "4,778", "4,792",
"4,806", "4,82", "4,834", "4,841", "4,848", "4,876", "4,904",
"4,918", "4,932", "4,945", "4,958", "4,96", "4,974", "4,984",
"40,06", "40,164", "40,216", "40,528", "40,944", "41,048", "41,152",
"41,412", "41,568", "41,88", "41,932", "42,4", "42,816", "45,29",
"45,812", "46,16", "46,682", "47,088", "47,668", "47,842", "48,132",
"49,872", "49,93", "5,016", "5,023", "5,03", "5,044", "5,058",
"5,072", "5,075", "5,1", "5,114", "5,128", "5,142", "5,153",
"5,184", "5,212", "5,218", "5,226", "5,24", "5,244", "5,254",
"5,268", "5,282", "5,296", "5,324", "5,338", "5,352", "5,366",
"5,374", "5,38", "5,394", "5,422", "5,436", "5,45", "5,475",
"5,478", "5,492", "5,506", "5,52", "5,534", "5,562", "5,576",
"5,59", "5,604", "5,618", "5,632", "5,646", "5,674", "5,685",
"5,716", "5,73", "5,744", "5,8", "5,832", "5,848", "5,864", "5,88",
"5,91", "5,912", "5,925", "5,928", "5,944", "5,96", "5,976",
"50,8", "52,226", "52,474", "53,032", "53,962", "54,024", "54,458",
"54,582", "55,078", "55,45", "55,636", "56,194", "57,068", "58,564",
"59,176", "6,008", "6,024", "6,04", "6,045", "6,056", "6,072",
"6,088", "6,104", "6,12", "6,136", "6,152", "6,232", "6,248",
"6,264", "6,285", "6,296", "6,312", "6,328", "6,33", "6,345",
"6,36", "6,375", "6,376", "6,408", "6,44", "6,45", "6,456", "6,472",
"6,48", "6,488", "6,495", "6,504", "6,51", "6,52", "6,54", "6,552",
"6,555", "6,584", "6,6", "6,632", "6,645", "6,648", "6,66", "6,664",
"6,68", "6,705", "6,735", "6,744", "6,76", "6,776", "6,808",
"6,824", "6,856", "6,872", "6,888", "6,904", "6,92", "6,936",
"6,952", "6,968", "6,984", "60,808", "62,372", "63,664", "64,448",
"65,024", "66,968", "68,336", "68,696", "69,632", "7,016", "7,08",
"7,096", "7,112", "7,128", "7,144", "7,176", "7,192", "7,208",
"7,24", "7,288", "7,304", "7,32", "7,336", "7,352", "7,368",
"7,384", "7,4", "7,419", "7,438", "7,476", "7,533", "7,552",
"7,571", "7,59", "7,609", "7,628", "7,647", "7,666", "7,685",
"7,704", "7,723", "7,742", "7,78", "7,818", "7,837", "7,856",
"7,913", "7,932", "7,97", "7,989", "70,712", "71,539", "74,388",
"74,542", "76,929", "78,623", "8,008", "8,027", "8,084", "8,103",
"8,122", "8,198", "8,217", "8,236", "8,2588", "8,274", "8,293",
"8,331", "8,388", "8,407", "8,426", "8,464", "8,502", "8,54",
"8,635", "8,673", "8,692", "8,711", "8,768", "8,787", "8,806",
"8,882", "8,901", "8,92", "8,939", "8,958", "8,996", "80,212",
"83,74", "86,092", "88,664", "9,034", "9,072", "9,11", "9,129",
"9,205", "9,321", "9,405", "9,447", "9,489", "9,51", "9,552",
"9,741", "9,783", "9,867", "9,888", "9,93", "9,951", "90,32",
"91,608", "92,528", "94,552"), class = "factor"), Skawica.Dolna..Skawica. = structure(c(44L,
35L, 35L, 35L, 35L, 35L, 44L, 58L, 71L, 71L, 71L, 71L, 189L,
174L, 174L, 166L, 71L, 71L, 161L, 166L, 166L, 166L, 182L, 258L,
258L), .Label = c("", "1,023", "1,045", "1,056", "1,067", "1,078",
"1,089", "1,1", "1,118", "1,136", "1,154", "1,172", "1,19", "1,208",
"1,226", "1,244", "1,262", "1,298", "1,316", "1,334", "1,352",
"1,37", "1,388", "1,406", "1,424", "1,43", "1,442", "1,46", "1,478",
"1,496", "1,514", "1,518", "1,532", "1,55", "1,56", "1,568",
"1,586", "1,604", "1,606", "1,622", "1,64", "1,658", "1,676",
"1,686", "1,694", "1,712", "1,73", "1,7359", "1,748", "1,7644",
"1,766", "1,7732", "1,782", "1,784", "1,7908", "1,7996", "1,802",
"1,812", "1,8172", "1,82", "1,826", "1,838", "1,8436", "1,856",
"1,8612", "1,87", "1,874", "1,892", "1,91", "1,928", "1,938",
"1,946", "1,964", "1,982", "10", "10,014", "10,068", "10,08",
"10,245", "10,32", "10,41", "10,44", "10,64", "10,641", "10,74",
"10,812", "10,96", "101,5", "107", "11,07", "11,184", "11,268",
"11,28", "11,4", "11,556", "11,6", "11,76", "11,92", "11,928",
"110,3", "112,5", "116,9", "118", "12,12", "12,192", "12,228",
"12,24", "12,3", "12,48", "12,56", "12,74", "12,84", "12,88",
"122,8", "124", "13,2", "13,54", "13,56", "13,62", "13,88", "13,92",
"130", "14,06", "14,22", "14,28", "14,5", "14,56", "14,64", "14,9",
"14,94", "15", "15,24", "15,39", "15,58", "15,78", "15,82", "15,92",
"16,17", "16,26", "16,56", "16,7", "16,95", "16,96", "17,34",
"17,418", "17,68", "17,73", "18,12", "18,51", "18,66", "18,76",
"18,9", "19,15", "19,32", "19,48", "19,74", "2", "2,029", "2,036",
"2,058", "2,064", "2,087", "2,116", "2,145", "2,174", "2,19",
"2,202", "2,203", "2,232", "2,261", "2,29", "2,348", "2,368",
"2,376", "2,377", "2,406", "2,435", "2,464", "2,522", "2,534",
"2,551", "2,562", "2,58", "2,609", "2,638", "2,696", "2,7", "2,725",
"2,748", "2,783", "2,812", "2,87", "2,899", "2,928", "2,934",
"2,957", "2,98", "2,986", "20,13", "20,16", "20,58", "21", "21,11",
"21,42", "21,6", "21,84", "22,13", "22,26", "22,6", "22,66",
"22,68", "23,1", "23,19", "23,56", "24,02", "24,2", "24,25",
"24,48", "24,78", "24,94", "25,31", "25,354", "25,4", "25,84",
"25,86", "25,96", "26,32", "26,78", "26,9", "27,24", "27,7",
"28,67", "28,72", "28,9556", "29,09", "29,23", "29,58", "29,74",
"29,85", "3,015", "3,044", "3,12", "3,131", "3,16", "3,189",
"3,218", "3,26", "3,276", "3,334", "3,363", "3,392", "3,421",
"3,45", "3,479", "3,508", "3,54", "3,595", "3,61", "3,624", "3,653",
"3,74", "3,798", "3,82", "3,827", "3,856", "3,914", "3,972",
"30,25", "30,76", "31,27", "31,78", "32,03", "32,29", "32,8",
"33,01", "33,304", "33,36", "33,5", "33,92", "34,03", "34,48",
"35,09", "35,6", "36", "36,68", "36,72", "37,28", "37,84", "38,4",
"38,82", "39", "39,2", "39,6", "4,001", "4,03", "4,059", "4,1",
"4,117", "4,146", "4,233", "4,291", "4,32", "4,345", "4,378",
"4,39", "4,407", "4,494", "4,552", "4,59", "4,61", "4,639", "4,68",
"4,697", "4,755", "4,813", "4,835", "4,871", "4,9", "4,97", "4,996",
"40,2", "40,8", "41,4", "42", "42,5", "42,6", "43,2", "44,4",
"45,04", "45,8", "45,808", "46,32", "46,96", "47,6", "48,24",
"48,304", "48,88", "49,2", "49,52", "5,028", "5,06", "5,08",
"5,124", "5,22", "5,26", "5,325", "5,348", "5,38", "5,444", "5,476",
"5,54", "5,55", "5,57", "5,636", "5,796", "5,828", "5,84", "5,86",
"5,871", "5,956", "50,16", "50,8", "52,6", "52,78", "53,308",
"56,05", "56,278", "56,872", "57,4", "57,604", "58,896", "59,44",
"59,5", "59,848", "59,984", "6,084", "6,13", "6,148", "6,172",
"6,18", "6,308", "6,404", "6,42", "6,473", "6,5", "6,628", "6,66",
"6,71", "6,774", "6,82", "6,884", "6,948", "6,98", "60,8", "61,344",
"62,16", "62,976", "63,15", "63,52", "66,8", "67,12", "67,193",
"67,558", "67,85", "68,58", "68,726", "69,456", "7", "7,075",
"7,14", "7,204", "7,236", "7,3", "7,364", "7,376", "7,46", "7,492",
"7,6", "7,677", "7,716", "7,78", "7,844", "7,9", "7,978", "70,04",
"70,77", "71,208", "71,354", "71,4", "71,5", "72,54", "75,42",
"76", "76,14", "76,3", "77,26", "79,5", "8,1", "8,166", "8,2",
"8,232", "8,279", "8,364", "8,43", "8,5", "8,58", "8,628", "8,727",
"8,76", "8,793", "8,8", "8,952", "80,35", "81", "82,05", "83,75",
"86", "86,3", "88,9", "89,8", "9,09", "9,1", "9,189", "9,324",
"9,4", "9,42", "9,486", "9,585", "9,696", "9,7", "9,75", "9,849",
"90,25", "91", "96"), class = "factor")), .Names = c("Data",
"Osielec", "Jordanów", "Skawica.Dolna..Skawica."), row.names = 26:50, class = "data.frame")

You need to convert your variable into numeric and you're more or less done. I use scale_y_continuous here.
library(ggplot2)
dane_dlugie$Osielec <- as.numeric(dane_dlugie$Osielec)
ggplot(dane_dlugie, aes(x=Data, y=Osielec, group=1)) +
geom_line()+labs(x="Data", y="Flow") +
ggtitle("Osielec")+
scale_y_continuous(breaks = 1:5) +
scale_x_datetime(date_breaks = "1 day", date_labels = "%d-%m")

Related

ggpairs formatting for points only

I'm looking to increase the size of the points AND outline them in black while keeping the line weight the same across the remaining plots.
library(ggplot2)
library(GGally)
pp <- ggpairs(pp.sed, columns = c(1,2), aes(color=pond.id, alpha = 0.5)) +
theme_bw()
print(pp)
Which gives me the following figure:
Data for reproducibility, and TIA!
> dput(pp.sed)
structure(list(Fe.259.941 = c(905.2628883, 825.7883359, 6846.128702,
1032.932924, 997.8037721, 588.9599882, 6107.641947, 798.4493611,
1046.38376, 685.2485692, 6452.273486, 730.8656684, 902.8585447,
1039.886406, 7408.801001, 2512.089991, 911.2101809, 941.3712067,
659.1069185, 1070.090445, 1017.666402, 925.3221586, 645.0500668,
954.0009756, 1022.594904, 803.5865352, 7653.184537, 1082.714082,
1048.51115, 773.9070604, 6889.060748, 973.0971769, 1002.091143,
798.9670583, 5089.035978, 2361.713222, 970.8258109, 748.3574529,
3942.04816, 889.1760124), Mn.257.611 = c(17.24667962, 14.90488024,
14.39265671, 20.51133433, 19.92596564, 11.76690074, 19.76386229,
14.29779164, 20.23646264, 13.55374658, 16.8847698, 13.11784439,
15.91777975, 20.64068844, 16.78681661, 28.61732162, 15.88328987,
19.59750367, 13.09735943, 21.59458118, 17.680152, 19.87127449,
12.8082581, 20.12050221, 17.57143193, 18.72196029, 16.21525793,
22.0518966, 18.39642397, 18.32238508, 16.17696923, 20.69668404,
17.96018218, 18.71945309, 16.50162126, 30.60719123, 17.69058768,
14.99048753, 16.28302375, 18.32277507), pond.id = structure(c(6L,
5L, 2L, 1L, 3L, 5L, 2L, 1L, 3L, 5L, 2L, 1L, 6L, 3L, 2L, 4L, 6L,
3L, 4L, 4L, 6L, 3L, 4L, 1L, 6L, 3L, 2L, 1L, 6L, 3L, 2L, 1L, 6L,
3L, 2L, 1L, 6L, 5L, 2L, 1L), .Label = c("LIL", "RHM", "SCS",
"STN", "STS", "TS"), class = "factor")), class = "data.frame", row.names = c(11L,
12L, 13L, 15L, 26L, 27L, 28L, 30L, 36L, 37L, 38L, 40L, 101L,
102L, 103L, 105L, 127L, 128L, 129L, 131L, 142L, 143L, 144L, 146L,
157L, 158L, 159L, 161L, 172L, 173L, 174L, 176L, 184L, 185L, 186L,
188L, 199L, 200L, 201L, 203L))
The GGally package already offers a family of wrap_xxx functions which could be used to set parameters to override default behaviour, e.g. using wrap you could override the default size of points using wrap(ggally_points, size = 5).
To use the wrapped function instead of the default you have to call
ggpairs(..., lower = list(continuous = wrap(ggally_points, size = 5))).
Switching the outline is a bit more tricky. Using wrap we could switch the shape of the points to 21 and set the outline color to "black". However, doing so the points are no longer colored. Unfortunately I have found no way to override the mapping. While it is possible to add a global fill aes, a drawback of doing so is that we lose the black outline for the densities.
One option to fix that is to write a wrapper for ggally_points which adjusts the mapping so that the fill aes is used instead of color.
library(ggplot2)
library(GGally)
ggally_points_filled <- function(data, mapping, ...) {
names(mapping)[grepl("^colour", names(mapping))] <- "fill"
ggally_points(data, mapping, ..., shape = 21)
}
w_ggally_points_filled <- wrap(ggally_points_filled, size = 5, color = "black")
ggpairs(pp.sed, columns = c(1, 2), aes(color = pond.id, alpha = 0.5),
lower = list(continuous = w_ggally_points_filled)) +
theme_bw()

ddply dropping rows with zero sum

I am trying to sum my data per Meter, then average out the sumCover by Transect. My issue is that when I mean the transects, at the meter points where the cover data was taken if no native species were recorded, then that transect is effectively dropped from the dataframe after the ddply function. I have tried using the .drop function, but the issue is each site has unequal transect sampling because it was scaled to site size, so it effectively adds transects to every site. What I need to figure out to do is how to fill in within a list of numbers for missing Transect while taking into account each site varies from 3 to 16 transects - EDIT - the data preview seem to of got cut off and does not have sufficient rows so here is a file:
Here is a downloadable link of the data csv
read.csv()
require(ddply)
NativeNonnativeCoverperMeter <- ddply(RestoredGrasslandSurveys, c("Site","Transect","Locality","Meter"), summarise,
sumCover = sum(Cover))
NativeNonnativeCoverperTransect <- ddply(NativeNonnativeCoverperMeter, c("Site","Transect","Locality"), summarise,
avgCover = mean(sumCover), .drop = F)
dput(RestoredGrasslandSurveys[1:10, ])
structure(list(Site = structure(c(10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L), .Label = c("AzevedoNorth", "AzevedoSouth",
"Big.Banana", "BlohmRanch", "CypressGrove", "Diablo.Canyon",
"Dipsea.Moors", "Elkhorn.Nursery", "Elkhorn.Owl", "ElkhornHotwire",
"FacultyHousing", "Glass.Beach", "Hanson.ESHA", "Hanson.Uplands",
"Hawk.Hill", "LightHouse", "Modoc", "MooreCreek", "Morning.Sun",
"Noyo.Headlands", "Paradise.Ridge", "Prosper.Ridge", "RussianRidge",
"Stinson.Gulch", "Tennessee.Valley", "Watsonville.Uplands", "YoungerLagoon"
), class = "factor"), County = structure(c(4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L), .Label = c("Humboldt", "Marin", "Mendocino",
"Monterery", "Monterey", "San.Luis.Obispo", "SanMateo", "Santa.Barbara",
"SantaCruz", "Sonoma"), class = "factor"), Transect = c(3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), Meter = c(0L, 5L, 10L, 15L,
20L, 25L, 30L, 35L, 40L, 45L), Lifeform = structure(c(4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("AnnualForb", "AnnualGrass",
"Fern", "Groundcover", "Horsetail", "Nfixer", "PerennialForb",
"PerennialGrass", "PerrenialForb", "Rush", "Sedge", "Shrub",
"Tree"), class = "factor"), Locality = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Groundcover", "Native",
"Nonnative"), class = "factor"), Species = structure(c(265L,
265L, 265L, 265L, 265L, 265L, 265L, 265L, 265L, 265L), .Label = c("Achillea.millefolium",
"Acmispon.glaber", "Acmispon.maritimus", "Acmispon.parviflorus",
"Acmispon.strigosus", "Agropyron.cristatum", "Aira.caryophyllea",
"Aira.elegans", "Aira.praecox", "Amsinckia.menziesii", "Anaphalis.margaritacea",
"Angelica.hendersonii", "Anthoxanthum.odoratum", "Anthriscus.caucalis",
"Artemisia.californica", "Asclepias.fascicularis", "Atriplex.semibucatta",
"Avena.barbata", "Avena.Barbata", "Avena.fatua", "Baccharis. pilularis",
"Baccharis.pilularis", "Bareground", "Bellis.perennis", "Berberis.pinnata",
"Brachypodium.distachyon", "Brassica.nigra", "Brassica.rapa",
"Brassica.tournefortii", "Briza.maxima", "Briza.minor", "Bromus.carinatus",
"Bromus.catharticus", "Bromus.diandrus", "Bromus.hordeaceous",
"Bromus.madritensis", "Bromus.maritimus", "Bromus.tectorum",
"Calamagrostis.nutkaensis", "Calandrinia.menziesii", "Calendula.arvensis",
"Calystegia.collina", "Calystegia.purpurata", "Cardamine.oligiosperma",
"Carduus.pycnocephalus", "carex.athrostachya", "Carex.gynodynama",
"Carex.lasiocarpa", "Carex.Praegracilis", "Carex.spp", "Carex.suberecta",
"Carex.tomentosa", "Carex.tumulicola", "Carpobrotus.edulis",
"Castilleja.affinis", "Castilleja.densiflora", "Cerastium.fontanum",
"Cerastium.glomeratum", "Chlorogalum.pomeridianum", "Cirsium.brevistylum",
"Cirsium.vulgare", "Clarkia.purpurea", "Clarkia.spp", "Claytonia.perfoliata",
"Clinopodium.douglasii", "Conium.maculatum", "Convolvulus.arvensis",
"Corethrogyne.filaginifolia", "Cortaderia.jubata", "Cotula.coronopifolia",
"Crassula.connata", "Crepis.vesicaria", "Croton.setigerus", "Cynodon.dactylon",
"Cynosurus.echinatus", "Cyperus.eragrostis", "Danthonia.californica",
"Daucus.pusillus", "Deschampsia.cespitosa", "Dichelostemma.capitatum",
"Dichondra.donelliana", "Dichondra.Donelliana", "Dichondra.micrantha",
"Distichlis.spicata", "Dudleya.cymosa", "Dudleya.farinosa", "Dysphania.ambrosioides",
"Ehrharta.erecta", "Elymus.condensatus", "Elymus.glaucus", "Elymus.triticoides",
"Elymus.vancouverensis", "Epilobium.brachycarpum", "Epilobium.cilatum",
"Equisetum.arvense", "Erigeron.canadensis", "Erigeron.glaucus",
"Erigeron.sumatrensis", "Eriogonum.latifolium", "Eriogonum.parvifolium",
"Eriophyllum.staechadifolium", "Erodium.botrys", "Erodium.cicutarium",
"Erodium.moscatum", "Eschscholzia.californica", "Eucalyptus.globulus",
"Festua.muyros", "Festuca.arundinacea", "Festuca.bromioides",
"Festuca.californica", "Festuca.idahoensis", "Festuca.microstachys",
"Festuca.muyros", "Festuca.perennis", "Festuca.pratensis", "Festuca.rubra",
"Foeniculum.vulgare", "Fragaria.vesca", "Frangula.californica",
"Fritillaria.affinis", "Galium.aparine", "Galium.divaricatum",
"Galium.porrigens", "Gamochaeta.ustulata", "Genista.monspessulana",
"Geranium.dissectum", "Geranium.molle", "Gilia.capitata", "Gnaphalium.palustre",
"Grindelia.latifolia", "Grindelia.stricta", "Helminthotheca.echioides",
"Hemiparasitic.ericaceae", "Heracleum.lanatum", "Heterotheca.grandiflora",
"Heterotheca.sessiliflora", "Hirschfieldia.incana", "Holcus.lanatus",
"Hordeum.brachyantherum", "Hordeum.marinum", "Hordeum.murinum",
"Horkelia.californica", "Hosackia.gracilis", "Hypochaeris.spp",
"Iris.douglasiana", "Iris.macrosiphon", "Juncus.bufonis", "Juncus.effusus",
"Juncus.mexicanus", "Juncus.occidentalis", "Juncus.patens", "Juncus.phaeocephalus",
"Koeleria.macrantha", "Lactuca.serriola", "Lasthenia.californica",
"Lathyrus.vestitus", "Leontodon.taraxacoides", "Lichen", "Linum.bienne",
"Logfia.gallica", "Lomatium.dasycarpum", "Lomatium.utriculatum",
"Lonicera.hispidula", "Lotus.corniculatus", "Lotus.micranthus",
"Lupinus.arboreus", "Lupinus.bicolor", "Lupinus.littoralis",
"Lupinus.nanus", "Lupinus.variicolor", "Luzula.comosa", "Luzula.subsessilis",
"Lysimachia.arvensis", "Lythrum.hyssopifolia", "Madia.exigua",
"Madia.gracilis", "Madia.madioides", "Madia.spp", "Malva.parviflora",
"Marah.fabaceus", "Matricaria.discoides", "Medicago.polymorpha",
"Melica.californica", "Melica.imperfecta", "Melica.torreyana",
"Melilotus.indicus", "Melilotus.officinalis", "Modiola.caroliniana",
"Moss", "Mulch", "Mushroom.cover", "Myosotis.discolor", "Oxalis.corniculata",
"Oxalis.pes-caprae", "Parentucellia.latifolia", "Parentucellia.viscosa",
"Paronychia.franciscana", "Pennisetum.clandestinum", "Perideridia.kelloggii",
"Phacelia.californica", "Phacelia.malvifolia", "Phalaris.aquatica",
"Pholistoma.auritum", "Plagiobothyrs.nothofulvus", "Plantago.coronopus",
"Plantago.erecta", "Plantago.lanceolata", "Poa.annua", "Poa.pratensis",
"Polygonum.arenastrum", "Polygonum.aviculare", "Polypodium.califomicum",
"Polypodium.californicum", "Polypogon.monspeliensis", "Polystichum.munitum",
"Prunella.vulgaris", "Pseudognaphalium.beneolens", "Pseudognaphalium.bioletti",
"Pseudognaphalium.californicum", "Pseudognaphalium.canescens",
"Pseudognaphalium.luteoalbum", "Pseudognaphalium.ramosissimum",
"Pseudotsuga.meziesii", "Pteridium.aquilinum", "Quercus.agrifolia",
"Ranunculus.californicus", "Ranunculus.occidentalis", "Raphanus.sativus",
"Raphanus.spp", "Rock", "Rubus.armeniacus", "Rubus.ursinus",
"Rumex.acetosella", "Rumex.crispus", "Rumex.Crispus", "Rumex.transitorius",
"Salix.lasiolepis", "Sanicula.arctopoides", "Sanicula.bipinnatifida",
"Sanicula.crassicaulis", "Scandix.peten-veneris", "Senecio.vulgare",
"Sherardia.arvensis", "Sidalcea.malviflora", "Silene.gallica",
"Sisyrinchium.bellum", "Solanum.americanum", "Solidago.velutina",
"Soliva.sessilis", "Sonchus.asper", "Sonchus.oleraceus", "Spergula.arvensis",
"Stachys.ajugoides", "Stachys.bullata", "Stellaria.media", "Stipa.cernua",
"Stipa.lepida", "Stipa.pulchra", "Stipa.purpurata", "Symphiotrichum.chilensis",
"Taraxia.ovata", "Tauschia.hartwegii", "Thatch.cover", "Thatch.Cover",
"Thatch.Depth", "Thysanocarpus.laciniatus", "Toxicodendron.diversilobum",
"Toxicoscordion.fremontii", "Tragopogon.porrifolius", "Tribulus.terrestris",
"Trifolium.angustifolium", "Trifolium.barbigerum", "Trifolium.bifidum",
"Trifolium.depauperatum", "Trifolium.dubium", "Trifolium.glomeratum",
"Trifolium.hirtum", "Trifolium.hybridum", "Trifolium.macraei",
"Trifolium.microcephalum", "Trifolium.repens", "Trifolium.subterraneum",
"Trifolium.variegatum", "Trifolium.willdenovii", "Triphysaria.pusilla",
"Triphysaria.versicolor", "Trisetum.canescens", "Vaccinium.ovatum",
"Veronica.persica", "Vicia.americana", "Vicia.benghalensis",
"Vicia.sativa", "Vicia.tetrasperma", "Vicia.villosa", "Viola.adunca",
"Viola.pedunculata", "Wyethia.angustifolia", "Wyethia.glabra"
), class = "factor"), Cover = c(1, 1, 0.5, 0.5, 0.5, 8, 2, 2,
5, 1)), row.names = c(NA, 10L), class = "data.frame")

For loop for function that works on segments of data

I have a sample dataset as follows (Out):
Out <- structure(list(Dist_Out = structure(c(223L, 224L, 195L, 195L,
195L, 235L, 299L, 64L, 336L, 28L, 191L, 129L, 63L, 303L, 249L,
194L, 222L, 177L, 199L, 309L), .Label = c("0", "0.110574578321468",
"0.110574578385818", "0.110574578646219", "0.110574578769975",
"0.110574578837889", "0.110574578901783", "0.110574578961973",
"0.110574579093701", "0.110574579157825", "0.11057457934999",
"0.110574579413902", "0.110574579478479", "0.11057457973394",
"0.110574579798528", "0.11057457999076", "0.110574580247396",
"0.11057458031112", "0.110574580503848", "0.110574580567801",
"0.110574580694844", "0.11057458095289", "0.110574581402704",
"0.110574583204168", "0.111304214830553", "0.111304253300095",
"0.111304307772237", "0.111304317308227", "0.111304330093688",
"0.11130434287376", "0.111304358897914", "0.111304361977123",
"0.11130436522592", "0.111304368417738", "0.11130437160922",
"0.111304371665862", "0.111304374743724", "0.111304374800365",
"0.111304374857007", "0.111304377934532", "0.111304384315138",
"0.11130438437178", "0.111304384428421", "0.111304387561577",
"0.111304390694397", "0.11130439080768", "0.111304393883521",
"0.111304393940163", "0.111304397072309", "0.11130439712895",
"0.111304397185592", "0.111304400317401", "0.111304400374043",
"0.111304403505515", "0.111304403562157", "0.111304413067836",
"0.111304413124478", "0.111304425756241", "0.15689285571989",
"0.156892869769221", "0.15689287418468", "0.156892889774207",
"0.156892912024679", "0.156892934292016", "0.156892940915204",
"0.156892943136249", "0.156892949836902", "0.156892951976477",
"0.156892954196025", "0.156892954236129", "0.156892954236208",
"0.156892954276312", "0.156892955820879", "0.156892955820883",
"0.156892955861062", "0.156892955861066", "0.156892958674593",
"0.156892958714776", "0.156892958714863", "0.156892960893118",
"0.156892960893133", "0.156892960933301", "0.156892960933317",
"0.156892962437304", "0.156892962437361", "0.156892962477487",
"0.156892962477544", "0.156892962517727", "0.156892969726529",
"0.156892969766712", "0.15689296976675", "0.156892969806895",
"0.156892971984017", "0.156892971984051", "0.1568929720242",
"0.156892972024234", "0.156892974161436", "0.156892974201513",
"0.156892974201619", "0.156892974241696", "0.156892974241802",
"0.156892975744282", "0.156892975824626", "0.156892978001093",
"0.156892978001154", "0.156892980852422", "0.156892980892616",
"0.156892983068896", "0.156892983109079", "0.156892989757201",
"0.156892991892444", "0.156892991892504", "0.15689299197287",
"0.156892994188015", "0.156892995093831", "0.15689299509388",
"0.156892998578116", "0.221149157095331", "0.221149157735701",
"0.221149158248122", "0.221149158251639", "0.221149158504029",
"0.221149158760615", "0.221149158892054", "0.221149159532802",
"0.221149160045349", "0.221149161071762", "0.221149161712213",
"0.22114916222707", "0.222608333407496", "0.22260837837114",
"0.222608384735182", "0.222608455374887", "0.222608493783746",
"0.222608596296507", "0.222608692192514", "0.222608730508481",
"0.222608736835477", "0.222608736892118", "0.222608768800201",
"0.222608794314542", "0.222608800691444", "0.222608857996604",
"0.247579423347402", "0.247579452035982", "0.247579496248119",
"0.247579500729161", "0.24757950684186", "0.247579510261327",
"0.247579510261392", "0.247579513391942", "0.247579513417395",
"0.247579513417407", "0.247579513442859", "0.247579516860718",
"0.247579518205451", "0.247579519185166", "0.247579519236095",
"0.247579525289911", "0.247579525315375", "0.24757952875664",
"0.247579529784064", "0.248558365959636", "0.248558423080156",
"0.248558474437371", "0.2485585599757", "0.248558577056858",
"0.248558611101923", "0.248558622479214", "0.24855862258067",
"0.248558628217488", "0.248558650962925", "0.248558656698174",
"0.248558662331659", "0.313785772234975", "0.313785790083671",
"0.313785798944933", "0.313785861163643", "0.313785884051598",
"0.313785919567943", "0.313785932204374", "0.313785932284703",
"0.31378593228474", "0.313785941750578", "0.313785949945705",
"0.313785953785589", "0.313785958853473", "0.313785958853573",
"0.331723736573009", "0.331723737017998", "0.331723741058604",
"0.331723747874274", "0.33172374819982", "0.333912827211437",
"0.333912980750125", "0.333913067001972", "0.333913133973521",
"0.349898963305821", "0.349898967518276", "0.349898985849967",
"0.349898989161391", "0.349898989161427", "0.349898997505614",
"0.349898997505615", "0.351744805744357", "0.351744887722199",
"0.351745133189943", "0.351745187980364", "0.3517453055621",
"0.35174531490497", "0.351745323909966", "0.399493757974483",
"0.399493788597454", "0.399493795359845", "0.399493832734277",
"0.399493843627049", "0.399493845554748", "0.400505368728602",
"0.400505423976401", "0.400505448284272", "0.400505527258931",
"0.400505598712409", "0.400505694328303", "0.400505709727528",
"0.400505725573516", "0.400505765671134", "0.40050589965877",
"0.400505923319168", "0.442298314835368", "0.442298317140206",
"0.442298324709909", "0.442298327797584", "0.445216846546918",
"0.445217192536371", "0.445217307591311", "0.445217345994231",
"0.445217512021335", "0.456088216475352", "0.456088223195701",
"0.458742575215647", "0.45874273685115", "0.458742885695134",
"0.458742910728267", "0.458743282342636", "0.470678474711803",
"0.47067880878535", "0.470678942002923", "0.495158931652482",
"0.495158944897039", "0.497116508401871", "0.49711679476651",
"0.49711679476653", "0.497116851569911", "0.497116897580156",
"0.554189163095155", "0.55418920215693", "0.554189262709751",
"0.554189324595007", "0.555210390817594", "0.555210746638391",
"0.556520945842888", "0.556521218492923", "0.556521458662341",
"0.563965519478444", "0.567399619528133", "0.567399713876817",
"0.567399933968277", "0.567399934023851", "0.567400184553882",
"0.596005878229009", "0.59600588239606", "0.598851042068279",
"0.598851279385347", "0.59885148700117", "0.627571384004223",
"0.627571616364075", "0.645883965371449", "0.645884171696778",
"0.64588421378803", "0.6478858784372", "0.647886220479167", "0.647886397912576",
"0.663447495427633", "0.667825269848697", "0.667825558298464",
"0.676917586632606", "0.676917795416644", "0.676918079344979",
"0.676918476303909", "0.703489456836576", "0.703489456890362",
"0.703490602009756", "0.709849619231796", "0.709850031391416",
"0.710875054488227", "0.745675302791038", "0.745675354954646",
"0.74567545723098", "0.745675662288711", "0.774022055439241",
"0.779129683432762", "0.779130802335089", "0.784464537327535",
"0.784464615110459", "0.786936919065818", "0.801010975018698",
"0.809907498350714", "0.809907713628001", "0.846808290999277",
"0.846808722035823", "0.866982968100186", "0.866983288894348",
"0.897273124467317", "0.897273302678894", "0.897273328167299",
"0.897273404335645", "0.917485187536906", "0.917485560660896",
"0.917485808994169", "0.955359158283399", "0.955359809834417",
"0.994234273564382", "1.0017384527634", "1.00782274966041", "1.0233303727427",
"1.09503832959811", "1.09503864342042", "1.09825019601709", "1.12793099939348",
"1.14021123855837", "1.24279165758788", "1.2441584622863", "1.25514264374951",
"1.2638862424017", "1.34543853012779", "1.44555679702924", "1.45117535398978",
"1.59180424399744", "1.66256690191307", "1.7271568926302", "1.79454617273675",
"1.83497057428696", "3.07757525439021"), class = "factor"), Speed_Out = structure(c(2L,
8L, 8L, 4L, 15L, 15L, 6L, 15L, 17L, 8L, 8L, 10L, 23L, 25L, 17L,
11L, 8L, 8L, 9L, 8L), .Label = c("0", "0.03", "0.06", "0.09",
"0.095", "0.12", "0.125", "0.155", "0.185", "0.19", "0.215",
"0.22", "0.245", "0.25", "0.275", "0.28", "0.31", "0.34", "0.345",
"0.37", "0.375", "0.4", "0.405", "0.435", "0.465", "0.495", "0.5",
"0.525", "0.555", "0.62", "0.715", "0.745", "0.775", "0.965",
"1.085", "1.12"), class = "factor"), Acceleration_Out = structure(c(20L,
25L, 6L, 20L, 28L, 9L, 19L, 28L, 7L, 2L, 21L, 19L, 31L, 9L, 19L,
6L, 21L, 2L, 23L, 6L), .Label = c("-0.012", "-0.014", "-0.024",
"-0.026", "-0.036", "-0.038", "-0.048", "-0.05", "-0.062", "-0.074",
"-0.076", "-0.088", "-0.1", "-0.112", "-0.138", "-0.162", "-0.222",
"-0.286", "0", "0.012", "0.014", "0.024", "0.026", "0.036", "0.038",
"0.048", "0.05", "0.062", "0.074", "0.076", "0.086", "0.088",
"0.1", "0.112", "0.124", "0.162", "0.222", "0.286"), class = "factor"),
Absolute_Heading_Out = structure(c(113L, 114L, 166L, 275L,
275L, 273L, 121L, 211L, 260L, 288L, 1L, 1L, 92L, 213L, 134L,
274L, 22L, 54L, 74L, 183L), .Label = c("0", "104.038094380956",
"104.038099175016", "104.038100287972", "104.038104254506",
"104.933371095486", "108.437291746436", "108.437292238906",
"108.437292594443", "108.437321740952", "111.804138048148",
"113.201423738397", "113.965388161297", "116.568179327572",
"116.568179982698", "116.568185226911", "116.568202500823",
"116.568207149774", "123.693672004617", "123.693687366444",
"123.693691078493", "123.693703315054", "123.693711549325",
"126.873674566913", "130.605207664786", "135.003902542311",
"135.00390464519", "135.003904674347", "135.003905470793",
"135.003907948238", "135.003907962816", "135.003908259113",
"135.003908774132", "135.003908968467", "135.003910222254",
"135.003911252625", "135.003911281782", "135.003911680336",
"135.003912078869", "135.003912905428", "135.003912920006",
"135.003912934585", "135.00391516638", "135.003915180958",
"135.00391581295", "135.003916226374", "135.003917038677",
"135.003917053256", "135.003918707167", "135.003919548889",
"135.003920346791", "135.003922843585", "135.003923671108",
"135.003938050425", "135.003939340192", "135.003948537839",
"135.003950183691", "135.0039588283", "14.0343965682479",
"14.0344008293472", "141.344008995685", "146.313535791806",
"146.313537999457", "153.438076376928", "153.438078534744",
"153.438079763631", "153.438085623885", "153.43812488595",
"161.567394954315", "161.567399368491", "180", "191.30841097092",
"198.432585234004", "198.432587185674", "198.432598104786",
"201.798689939092", "206.561897223892", "206.561914376115",
"206.561918159163", "206.561920224708", "206.561923646396",
"210.960297150126", "213.686445206476", "213.686465120369",
"216.866121212474", "216.866126783458", "218.655948156729",
"224.99604368852", "224.996057271547", "224.9960602985",
"224.99606521108", "224.996065562981", "224.996081278255",
"224.99608235309", "224.996082367668", "224.996082946744",
"224.996083788204", "224.996084848199", "224.996086776282",
"224.996086790861", "224.996087079994", "224.996087906553",
"224.996088732797", "224.996089777746", "224.996090081518",
"224.996090618394", "224.996094529207", "224.996097816515",
"230.190571318522", "231.336326241386", "234.458609341892",
"236.306289792777", "236.306305752159", "236.306307393042",
"236.306325713134", "239.032745245313", "239.032773084164",
"243.431791522707", "243.431797625395", "243.431808769598",
"243.431811475523", "245.221858182135", "251.562680070773",
"251.56268455414", "251.562697978643", "251.562700819679",
"254.052524049611", "255.961907894589", "255.961909409804",
"255.961917967213", "257.469536195034", "258.68855355037",
"258.688558827016", "26.561897224065", "26.561909979624",
"26.5619152872187", "26.5619165055352", "26.561920485388",
"26.5619245326763", "26.5619257724073", "260.536399738673",
"262.874018076851", "262.87401868698", "263.658950185096",
"270.000000825397", "270.000000825739", "270.000000825967",
"270.000000826081", "270.000000826195", "270.000000826309",
"270.000000826423", "270.000000826651", "270.000000826765",
"270.000000826878", "270.000000826879", "270.00000082722",
"270.000000827562", "270.00000165051", "270.000001652902",
"270.000001653814", "270.000001657631", "270.000001661505",
"270.000001663556", "270.000001664809", "270.000002479914",
"270.000002484092", "270.000003306546", "270.000003311075",
"270.000003315262", "270.000004144966", "270.000004158936",
"270.000004979198", "270.000005782192", "270.000007465657",
"277.125995411424", "277.125996790413", "277.126003803657",
"281.311454747624", "284.038107767959", "288.437300278551",
"29.0512461969053", "291.804137106086", "293.201447869319",
"296.568183955338", "296.568186601703", "296.568187257637",
"296.568187269301", "296.568188593043", "296.568211697749",
"296.568217115325", "296.568231666689", "30.9602599760489",
"30.9602987561062", "303.693725157126", "315.003904193081",
"315.003905470703", "315.003906325462", "315.003910440718",
"315.003911033758", "315.003911563215", "315.003911845393",
"315.003912005172", "315.003913746145", "315.003914572848",
"315.003916515341", "315.003917865386", "315.003917879964",
"315.00391928621", "315.003919300789", "315.003925739793",
"315.003927795227", "315.003929770525", "315.00394101503",
"315.003946345431", "315.003956610649", "322.128861928285",
"323.133860711499", "323.13386938527", "323.133916640514",
"326.313549702075", "33.6864487776922", "333.438072719985",
"333.438075455662", "333.438081405064", "333.438081416726",
"333.438112671245", "338.201312240365", "341.567396605941",
"341.567401021678", "348.691571595965", "350.538959809096",
"44.9960299262225", "44.9960433893507", "44.9960647858047",
"44.9960714405142", "44.9960796387537", "44.9960796533323",
"44.9960813075886", "44.9960821346143", "44.996083165653",
"44.9960831802316", "44.9960854271517", "44.9960862684335",
"44.9960870802999", "44.9960870948785", "44.9960889662424",
"50.1905434337506", "53.1262908803736", "54.4585694603986",
"56.3062693546384", "56.3062770335264", "59.032755274607",
"63.4317723208886", "63.4317848802781", "63.4317895372392",
"63.4317935836423", "63.4317955060977", "63.4318074457203",
"66.0345895371247", "67.3773348964212", "68.1958709110713",
"71.562668281165", "74.052516819087", "75.9618996568146",
"78.6885389399677", "78.6885408616536", "79.6937487439011",
"85.6006837571501", "89.9999941872715", "89.9999950129589",
"89.9999958484933", "89.9999966753091", "89.9999966878713",
"89.9999975116924", "89.9999975159082", "89.999997518263",
"89.9999983366149", "89.9999983395774", "89.9999983449896",
"89.9999983460151", "89.999998346186", "89.9999983470975",
"89.9999983477812", "89.9999983479521", "89.9999991689056",
"89.9999991699311", "89.9999991714124", "89.9999991716402",
"89.9999991719821", "89.9999991728936", "89.9999991730076",
"89.9999991731215", "89.9999991732354", "89.9999991735772",
"89.9999991735773", "89.9999991736912", "89.9999991738051",
"89.9999991739191", "89.999999174033", "89.9999991742609",
"98.1312035976929", "99.4635877243328", "99.4635933528321",
"99.4635973787254"), class = "factor"), Relative_Heading_Out = structure(c(84L,
3L, 120L, 273L, 213L, 403L, 38L, 180L, 297L, 73L, 409L, 213L,
74L, 210L, 326L, 166L, 121L, 10L, 162L, 212L), .Label = c("-1.08941367216175e-06",
"-1.27762228885331e-06", "-1.64088351084501e-06", "-1.97529777778982e-06",
"-10.6207232354447", "-107.098299845166", "-108.432605871424",
"-11.3096297403433", "-11.3102336441757", "-11.3102347353711",
"-11.3114558136556", "-11.3114593966472", "-11.6348360542887",
"-110.383710239579", "-111.798690769161", "-116.561876778859",
"-116.568179982698", "-119.048575511772", "-12.5275134113761",
"-125.54290253755", "-126.873674566913", "-134.996083788201",
"-134.99608874744", "-135.003907962816", "-135.003910222254",
"-135.003910440718", "-135.003912920006", "-135.003916226374",
"-135.003917053256", "-135.003920346791", "-14.0343965682479",
"-14.0344008293472", "-14.0381027887613", "-14.9333752469922",
"-15.2563802581807", "-151.923815028848", "-153.431797335435",
"-153.431814787652", "-153.438076376928", "-153.438078534744",
"-153.438081405064", "-155.224175835841", "-16.5895970517398",
"-161.564269704982", "-161.564270216045", "-163.070873427349",
"-168.688559655604", "-173.087896236407", "-178.210035361013",
"-18.4357312990837", "-18.4357320431515", "-18.4357376166744",
"-18.4372944963587", "-18.4373234043367", "-18.4374360606442",
"-180", "-180.00000165239", "-180.000001652846", "-180.000001653188",
"-180.00000165359", "-180.000001653757", "-189.462544556087",
"-19.440191483388", "-191.310236746892", "-198.432598104786",
"-198.435737329084", "-201.804133233895", "-203.197372900173",
"-206.561923646396", "-213.686445206476", "-213.693730144167",
"-216.866121212474", "-22.6226642752191", "-224.996065562981",
"-224.996082946744", "-224.996084848199", "-224.996086776282",
"-224.996090081518", "-225.003911859839", "-225.003915399271",
"-225.003918706729", "-23.2014262201339", "-235.306909073552",
"-236.306305752159", "-24.2299694795691", "-243.438082243148",
"-251.562697978643", "-251.572156808192", "-26.561920485388",
"-26.5619245326763", "-26.5682154538194", "-270.000000825739",
"-270.000000826081", "-270.000000826195", "-270.000000826423",
"-270.00000082722", "-270.00000165051", "-270.000001652902",
"-270.000001663556", "-270.000005782192", "-270.007837978621",
"-278.137789746516", "-296.568183955338", "-296.568186601703",
"-30.9658311909307", "-30.9672302310985", "-315.003904193081",
"-315.003911033758", "-315.003911845393", "-315.003912005172",
"-315.003913746145", "-315.003914572848", "-315.003916515341",
"-315.003917865386", "-315.00391928621", "-322.128861928285",
"-322.129645823558", "-326.313549702075", "-33.6864487776922",
"-33.6936950910497", "-33.6937058033612", "-33.6937221353792",
"-333.438075455662", "-341.567396605941", "-36.8661267834578",
"-36.8698974613882", "-38.6636779175493", "-4.39902960567105",
"-4.63602950075966", "-40.6024036695531", "-44.99604368852",
"-44.9960796387537", "-44.9960812782547", "-44.9960812928333",
"-44.9960821346143", "-44.996083165653", "-44.9960831802316",
"-44.9960837736256", "-44.9960862684335", "-44.9960870799939",
"-44.9960870802999", "-44.9960920371838", "-44.9960945292073",
"-45.0039062965318", "-45.0039099113321", "-45.0039104261803",
"-45.0039137610075", "-45.0039159931442", "-45.0039170385072",
"-45.0039170385603", "-45.0039184592101", "-45.0039195195613",
"-45.0039195342731", "-45.0039244841527", "-45.0039501982616",
"-49.2730539449748", "-50.1905434337506", "-50.5335956897864",
"-56.3063089215069", "-56.3135357050287", "-6.55934059068386e-07",
"-63.4286491352498", "-63.4317895372392", "-63.438071894588",
"-63.4380797638232", "-63.4381002876273", "-63.4381102417655",
"-66.7985762616031", "-69.0799574007877", "-69.2279887677529",
"-69.782098428514", "-7.01324449892127e-06", "-7.76602933812626",
"-71.5579915325294", "-71.5580050437492", "-71.5626782590484",
"-71.5674001952552", "-71.5674035490282", "-71.5720922472722",
"-71.5721163197041", "-71.5721653034134", "-78.6825596495758",
"-8.12994870632622", "-8.13108046273612", "-8.26243820029049e-07",
"-8.26479720217321e-07", "-8.26631662675936e-07", "-8.26992447855446e-07",
"-8.54759605317668e-07", "-81.8643827331808", "-82.8740180768513",
"-89.9921636459231", "-89.9921690352491", "-89.9921952742039",
"-89.999998346186", "-89.9999991719821", "-89.9999991731215",
"-89.9999991732354", "-89.9999991735772", "-89.9999991736912",
"-89.9999991738051", "-89.9999991739191", "-90.000000825967",
"-90.0000008260809", "-90.0000008264228", "-90.0000008267646",
"-90.0000008268785", "-90.0000008280179", "-90.0000033110747",
"-90.0078754520491", "-90.0079289020773", "-94.7688606836447",
"0", "1.21831646993087e-06", "1.6576310599703e-06", "1.66235020060412e-06",
"101.302350452585", "106.496883952078", "108.427537580527",
"108.44197890194", "11.3096332494953", "11.3102394284159",
"11.3113335227112", "11.3114583072521", "116.561916857852",
"116.56192105113", "116.568187269301", "117.402527859642",
"12.2476780128984", "120.957965917544", "125.532495222411",
"132.268196345264", "134.996085645806", "134.996087906417",
"134.996090907599", "135.00390464519", "135.003907948238",
"135.003910222254", "135.003911252625", "135.003911281782",
"135.003911680336", "135.003912905428", "135.003912920006",
"135.003912934585", "135.00391516638", "135.00391928621",
"135.003919548889", "14.0344008293472", "143.130106780739",
"15.2567559249757", "153.433391310084", "153.438081405064",
"153.438085623885", "161.562685384209", "161.564271367631",
"161.564271627437", "161.565836751711", "18.4341646234914",
"18.4341666601178", "18.4341668582031", "18.4341672821188",
"18.4357306931675", "18.4357346476826", "18.4357360052061",
"18.435740353848", "18.4372930648732", "18.8010696166384",
"180", "180.000001651478", "180.00000165239", "180.000001652497",
"180.000001652846", "180.000004968184", "187.12599956293",
"187.695688180681", "19.5660942200254", "191.309633889126",
"194.038104488152", "198.432585234004", "2.20765036829107e-06",
"201.804138766508", "206.561920224708", "206.561923646396",
"206.566618439315", "206.568189420149", "21.1640959090685",
"213.693732308918", "222.278699473017", "224.99608235309",
"224.996086790861", "224.996089777746", "224.996097816515",
"225.00391951929", "225.003920954773", "229.769652363221",
"243.431808769598", "247.626592898806", "251.567401848443",
"251.572173725974", "255.961909409804", "26.561909979624",
"26.5619143761148", "26.5619152872187", "26.561918159163",
"26.561920485388", "26.5619245326763", "26.5634850427263",
"26.5656849738252", "26.5666170276959", "26.5674578635275",
"26.5681908992693", "270.000000825739", "270.000000825967",
"270.000000826081", "270.000000826195", "270.000000826423",
"270.000000826651", "270.000000826765", "270.000000826879",
"270.00000082722", "270.000000827562", "270.00000165051",
"270.000001657631", "270.007821474476", "270.007864984721",
"288.442043790965", "3.5553682664613e-07", "3.96653496181898e-06",
"30.9602599760489", "30.9658300606604", "30.9658510086751",
"30.967256419496", "315.003906325462", "315.003911033758",
"315.003913746145", "315.003916515341", "315.003917865386",
"315.003917879964", "33.6864487776922", "33.6936728303563",
"33.6937173620532", "33.6941930107691", "333.438072719985",
"333.438075455662", "333.438081416726", "348.691571595965",
"350.538959809096", "38.0930036092936", "38.6628898865961",
"4.26803802337022", "40.6024008285175", "44.9960829613228",
"44.9960831802316", "44.9960848481989", "44.9960862684335",
"44.9960870799939", "44.9960870802999", "44.9960870948785",
"44.9960879211313", "44.9960906183937", "44.9960920517623",
"44.9960945292073", "45.0039102076872", "45.0039106205144",
"45.0039107371338", "45.0039110191976", "45.003911048449",
"45.0039137464255", "45.0039137464256", "45.0039165156846",
"45.0039195196753", "45.0039249122307", "45.0039253261187",
"49.579530827573", "58.6778036558414", "6.34009731534894",
"6.34105728056107", "63.4380772030091", "63.4380789368118",
"63.4380805900751", "63.438083494651", "63.4381110097399",
"63.4412765398765", "63.4412905471348", "64.6593257332515",
"66.0346118387025", "68.2013072611666", "7.12550172992948",
"7.76603379364138", "7.96445306150417e-07", "7.97902600879752e-07",
"71.5626800707728", "71.5627008196788", "71.5720922326936",
"74.052516819087", "74.7389307588194", "78.682521535368",
"78.6885588270165", "78.6915931880158", "8.12994364547711",
"8.12994419615723", "8.26137920739711e-07", "8.28036945677013e-07",
"83.0050894051913", "85.2304874382689", "87.5169609528689",
"89.9921898840171", "89.999998346186", "89.9999991716402",
"89.9999991731215", "89.9999991732354", "89.9999991735772",
"89.9999991736912", "89.9999991738051", "89.9999991739191",
"89.999999174033", "89.9999991742609", "9.46359418141975",
"90.0000008260809", "90.0000008261949", "90.0000008263088",
"90.0000008264227", "90.0000008264228", "90.0000008267646",
"90.0000008268785", "90.0000016529025", "93.1724357049647"
), class = "factor"), Movement_Out = structure(c(2L, 1L,
1L, 3L, 3L, 3L, 2L, 3L, 4L, 1L, 3L, 3L, 3L, 2L, 4L, 2L, 1L,
3L, 3L, 2L), .Label = c("forward", "left", "non-moving",
"right"), class = "factor"), Changes_Out = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 1L, 2L), .Label = c("0", "1"), class = "factor"),
AccPosNeg_Out = structure(c(2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("0",
"1"), class = "factor"), AccChange_Out = structure(c(2L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor")), .Names = c("Dist_Out",
"Speed_Out", "Acceleration_Out", "Absolute_Heading_Out", "Relative_Heading_Out",
"Movement_Out", "Changes_Out", "AccPosNeg_Out", "AccChange_Out"
), row.names = c(NA, 20L), class = "data.frame")
and the following function:
JRip <- function (Dist_Out, Speed_Out, Acceleration_Out, Absolute_Heading_Out,
Relative_Heading_Out, Movement_Out, Changes_Out, AccPosNeg_Out,
AccChange_Out)
{
if (max(Speed_Out) >= 2.45) {
Behaviour <- "walking"
}
else if (mean(Acceleration_Out[Acceleration_Out > 0]) >=
0.03 & (max(Dist_Out[Movement_Out != "non-moving"]) <=
2.17 & (mean(Speed_Out) >= 0.23 & (sum(Dist_Out[Movement_Out !=
"non-moving"]) >= 11.92)))) {
Behaviour <- "grazing"
}
else Behaviour <- "resting"
return(Behaviour)
}
I want to analyse the dataset in segments of say 5 instances and then to the next 5 instances and apply the function here again. So for this dataset of 20 instances I would expect 4 predictions from the function. It seemed that the logical thing to do was pass a sliding window over the dataset - after much trial and finally realising that rollapply will not take mixed types, I was wondering if this can be done using a for loop? The function uses mean and max and although I have tried many for loops I cannot get it to work. Does anyone know how to code a for loop to achieve this?
First, you need to convert your data to numeric types, since they are stored as factors:
Out[,-6] <- as.data.frame(sapply(Out[,-6], function(x) as.numeric(as.character(x))))
Now, you can use split-apply-combine to split up your data frame into sets of 5-row chunks, apply your function to each, and grab the result:
sapply(split(Out, rep(1:4, each=5)), do.call, what=JRip)
# 1 2 3 4
# "resting" "resting" "resting" "resting"
For the provided data frame, it looks like each 5-row segment is in the resting state. This is easy to confirm, as max(Out$Speed_Out) is 0.465, meaning no segment is walking, and sum(Out$Dist_Out) is 8.94, meaning no segment is grazing.

rollapply classes each segment the same

I am trying to pass a function over zoo data with the following:
Behaviour <- rollapply(data = as.zoo(Out), width = 32, FUN = function(JRip){
JRip(Dist_Out, Speed_Out, Acceleration_Out, Absolute_Heading_Out, Relative_Heading_Out, Movement_Out, Changes_Out, AccPosNeg_Out, AccChange_Out)},
by = 32, by.column = FALSE, partial = TRUE, align = "center")
This function classes data segments of 32 rows with a behaviour and then goes down to the next 32 rows to predict the behaviour.
This seems to work fine when I only give it data of length 32 rows but when I give it anymore e.g. 64 it classes each Behaviour exactly the same rather than detecting differences. As I say as I feed it individual data of nrow 32 each it gets them all correct. Is there anything obviously wrong that I am missing here before I put up what is a large example?
Thanks.
Ok here is the first 33 lines of the data (or segment).
The function works through a set of if else statements and comes to a behavioural classification. When I run the function over this segment only with 'window = 32' size of 32 and 'by = 32' it correctly classifies the segment. But when I give it a whole dataset e.g. >32 rows it gives every segment the same classification. So if I gave it 66 rows to give 2 classes it will class them both the same when I know they should be different. This happens also when I give it greater datasets.
Out <- structure(list(Dist_Out = structure(c(223L, 224L, 195L, 195L,
195L, 235L, 299L, 64L, 336L, 28L, 191L, 129L, 63L, 303L, 249L,
194L, 222L, 177L, 199L, 309L, 165L, 276L, 298L, 308L, 21L, 297L,
175L, 253L, 316L, 268L, 281L, 251L, 60L), .Label = c("0", "0.110574578321468",
"0.110574578385818", "0.110574578646219", "0.110574578769975",
"0.110574578837889", "0.110574578901783", "0.110574578961973",
"0.110574579093701", "0.110574579157825", "0.11057457934999",
"0.110574579413902", "0.110574579478479", "0.11057457973394",
"0.110574579798528", "0.11057457999076", "0.110574580247396",
"0.11057458031112", "0.110574580503848", "0.110574580567801",
"0.110574580694844", "0.11057458095289", "0.110574581402704",
"0.110574583204168", "0.111304214830553", "0.111304253300095",
"0.111304307772237", "0.111304317308227", "0.111304330093688",
"0.11130434287376", "0.111304358897914", "0.111304361977123",
"0.11130436522592", "0.111304368417738", "0.11130437160922",
"0.111304371665862", "0.111304374743724", "0.111304374800365",
"0.111304374857007", "0.111304377934532", "0.111304384315138",
"0.11130438437178", "0.111304384428421", "0.111304387561577",
"0.111304390694397", "0.11130439080768", "0.111304393883521",
"0.111304393940163", "0.111304397072309", "0.11130439712895",
"0.111304397185592", "0.111304400317401", "0.111304400374043",
"0.111304403505515", "0.111304403562157", "0.111304413067836",
"0.111304413124478", "0.111304425756241", "0.15689285571989",
"0.156892869769221", "0.15689287418468", "0.156892889774207",
"0.156892912024679", "0.156892934292016", "0.156892940915204",
"0.156892943136249", "0.156892949836902", "0.156892951976477",
"0.156892954196025", "0.156892954236129", "0.156892954236208",
"0.156892954276312", "0.156892955820879", "0.156892955820883",
"0.156892955861062", "0.156892955861066", "0.156892958674593",
"0.156892958714776", "0.156892958714863", "0.156892960893118",
"0.156892960893133", "0.156892960933301", "0.156892960933317",
"0.156892962437304", "0.156892962437361", "0.156892962477487",
"0.156892962477544", "0.156892962517727", "0.156892969726529",
"0.156892969766712", "0.15689296976675", "0.156892969806895",
"0.156892971984017", "0.156892971984051", "0.1568929720242",
"0.156892972024234", "0.156892974161436", "0.156892974201513",
"0.156892974201619", "0.156892974241696", "0.156892974241802",
"0.156892975744282", "0.156892975824626", "0.156892978001093",
"0.156892978001154", "0.156892980852422", "0.156892980892616",
"0.156892983068896", "0.156892983109079", "0.156892989757201",
"0.156892991892444", "0.156892991892504", "0.15689299197287",
"0.156892994188015", "0.156892995093831", "0.15689299509388",
"0.156892998578116", "0.221149157095331", "0.221149157735701",
"0.221149158248122", "0.221149158251639", "0.221149158504029",
"0.221149158760615", "0.221149158892054", "0.221149159532802",
"0.221149160045349", "0.221149161071762", "0.221149161712213",
"0.22114916222707", "0.222608333407496", "0.22260837837114",
"0.222608384735182", "0.222608455374887", "0.222608493783746",
"0.222608596296507", "0.222608692192514", "0.222608730508481",
"0.222608736835477", "0.222608736892118", "0.222608768800201",
"0.222608794314542", "0.222608800691444", "0.222608857996604",
"0.247579423347402", "0.247579452035982", "0.247579496248119",
"0.247579500729161", "0.24757950684186", "0.247579510261327",
"0.247579510261392", "0.247579513391942", "0.247579513417395",
"0.247579513417407", "0.247579513442859", "0.247579516860718",
"0.247579518205451", "0.247579519185166", "0.247579519236095",
"0.247579525289911", "0.247579525315375", "0.24757952875664",
"0.247579529784064", "0.248558365959636", "0.248558423080156",
"0.248558474437371", "0.2485585599757", "0.248558577056858",
"0.248558611101923", "0.248558622479214", "0.24855862258067",
"0.248558628217488", "0.248558650962925", "0.248558656698174",
"0.248558662331659", "0.313785772234975", "0.313785790083671",
"0.313785798944933", "0.313785861163643", "0.313785884051598",
"0.313785919567943", "0.313785932204374", "0.313785932284703",
"0.31378593228474", "0.313785941750578", "0.313785949945705",
"0.313785953785589", "0.313785958853473", "0.313785958853573",
"0.331723736573009", "0.331723737017998", "0.331723741058604",
"0.331723747874274", "0.33172374819982", "0.333912827211437",
"0.333912980750125", "0.333913067001972", "0.333913133973521",
"0.349898963305821", "0.349898967518276", "0.349898985849967",
"0.349898989161391", "0.349898989161427", "0.349898997505614",
"0.349898997505615", "0.351744805744357", "0.351744887722199",
"0.351745133189943", "0.351745187980364", "0.3517453055621",
"0.35174531490497", "0.351745323909966", "0.399493757974483",
"0.399493788597454", "0.399493795359845", "0.399493832734277",
"0.399493843627049", "0.399493845554748", "0.400505368728602",
"0.400505423976401", "0.400505448284272", "0.400505527258931",
"0.400505598712409", "0.400505694328303", "0.400505709727528",
"0.400505725573516", "0.400505765671134", "0.40050589965877",
"0.400505923319168", "0.442298314835368", "0.442298317140206",
"0.442298324709909", "0.442298327797584", "0.445216846546918",
"0.445217192536371", "0.445217307591311", "0.445217345994231",
"0.445217512021335", "0.456088216475352", "0.456088223195701",
"0.458742575215647", "0.45874273685115", "0.458742885695134",
"0.458742910728267", "0.458743282342636", "0.470678474711803",
"0.47067880878535", "0.470678942002923", "0.495158931652482",
"0.495158944897039", "0.497116508401871", "0.49711679476651",
"0.49711679476653", "0.497116851569911", "0.497116897580156",
"0.554189163095155", "0.55418920215693", "0.554189262709751",
"0.554189324595007", "0.555210390817594", "0.555210746638391",
"0.556520945842888", "0.556521218492923", "0.556521458662341",
"0.563965519478444", "0.567399619528133", "0.567399713876817",
"0.567399933968277", "0.567399934023851", "0.567400184553882",
"0.596005878229009", "0.59600588239606", "0.598851042068279",
"0.598851279385347", "0.59885148700117", "0.627571384004223",
"0.627571616364075", "0.645883965371449", "0.645884171696778",
"0.64588421378803", "0.6478858784372", "0.647886220479167", "0.647886397912576",
"0.663447495427633", "0.667825269848697", "0.667825558298464",
"0.676917586632606", "0.676917795416644", "0.676918079344979",
"0.676918476303909", "0.703489456836576", "0.703489456890362",
"0.703490602009756", "0.709849619231796", "0.709850031391416",
"0.710875054488227", "0.745675302791038", "0.745675354954646",
"0.74567545723098", "0.745675662288711", "0.774022055439241",
"0.779129683432762", "0.779130802335089", "0.784464537327535",
"0.784464615110459", "0.786936919065818", "0.801010975018698",
"0.809907498350714", "0.809907713628001", "0.846808290999277",
"0.846808722035823", "0.866982968100186", "0.866983288894348",
"0.897273124467317", "0.897273302678894", "0.897273328167299",
"0.897273404335645", "0.917485187536906", "0.917485560660896",
"0.917485808994169", "0.955359158283399", "0.955359809834417",
"0.994234273564382", "1.0017384527634", "1.00782274966041", "1.0233303727427",
"1.09503832959811", "1.09503864342042", "1.09825019601709", "1.12793099939348",
"1.14021123855837", "1.24279165758788", "1.2441584622863", "1.25514264374951",
"1.2638862424017", "1.34543853012779", "1.44555679702924", "1.45117535398978",
"1.59180424399744", "1.66256690191307", "1.7271568926302", "1.79454617273675",
"1.83497057428696", "3.07757525439021"), class = "factor"), Speed_Out = structure(c(2L,
8L, 8L, 4L, 15L, 15L, 6L, 15L, 17L, 8L, 8L, 10L, 23L, 25L, 17L,
11L, 8L, 8L, 9L, 8L, 9L, 11L, 8L, 10L, 14L, 24L, 26L, 26L, 23L,
18L, 22L, 13L, 15L), .Label = c("0", "0.03", "0.06", "0.09",
"0.095", "0.12", "0.125", "0.155", "0.185", "0.19", "0.215",
"0.22", "0.245", "0.25", "0.275", "0.28", "0.31", "0.34", "0.345",
"0.37", "0.375", "0.4", "0.405", "0.435", "0.465", "0.495", "0.5",
"0.525", "0.555", "0.62", "0.715", "0.745", "0.775", "0.965",
"1.085", "1.12"), class = "factor"), Acceleration_Out = structure(c(20L,
25L, 6L, 20L, 28L, 9L, 19L, 28L, 7L, 2L, 21L, 19L, 31L, 9L, 19L,
6L, 21L, 2L, 23L, 6L, 27L, 6L, 21L, 19L, 22L, 27L, 4L, 23L, 9L,
24L, 1L, 8L, 28L), .Label = c("-0.012", "-0.014", "-0.024", "-0.026",
"-0.036", "-0.038", "-0.048", "-0.05", "-0.062", "-0.074", "-0.076",
"-0.088", "-0.1", "-0.112", "-0.138", "-0.162", "-0.222", "-0.286",
"0", "0.012", "0.014", "0.024", "0.026", "0.036", "0.038", "0.048",
"0.05", "0.062", "0.074", "0.076", "0.086", "0.088", "0.1", "0.112",
"0.124", "0.162", "0.222", "0.286"), class = "factor"), Absolute_Heading_Out = structure(c(113L,
114L, 166L, 275L, 275L, 273L, 121L, 211L, 260L, 288L, 1L, 1L,
92L, 213L, 134L, 274L, 22L, 54L, 74L, 183L, 257L, 91L, 256L,
263L, 1L, 255L, 214L, 189L, 143L, 178L, 252L, 254L, 57L), .Label = c("0",
"104.038094380956", "104.038099175016", "104.038100287972", "104.038104254506",
"104.933371095486", "108.437291746436", "108.437292238906", "108.437292594443",
"108.437321740952", "111.804138048148", "113.201423738397", "113.965388161297",
"116.568179327572", "116.568179982698", "116.568185226911", "116.568202500823",
"116.568207149774", "123.693672004617", "123.693687366444", "123.693691078493",
"123.693703315054", "123.693711549325", "126.873674566913", "130.605207664786",
"135.003902542311", "135.00390464519", "135.003904674347", "135.003905470793",
"135.003907948238", "135.003907962816", "135.003908259113", "135.003908774132",
"135.003908968467", "135.003910222254", "135.003911252625", "135.003911281782",
"135.003911680336", "135.003912078869", "135.003912905428", "135.003912920006",
"135.003912934585", "135.00391516638", "135.003915180958", "135.00391581295",
"135.003916226374", "135.003917038677", "135.003917053256", "135.003918707167",
"135.003919548889", "135.003920346791", "135.003922843585", "135.003923671108",
"135.003938050425", "135.003939340192", "135.003948537839", "135.003950183691",
"135.0039588283", "14.0343965682479", "14.0344008293472", "141.344008995685",
"146.313535791806", "146.313537999457", "153.438076376928", "153.438078534744",
"153.438079763631", "153.438085623885", "153.43812488595", "161.567394954315",
"161.567399368491", "180", "191.30841097092", "198.432585234004",
"198.432587185674", "198.432598104786", "201.798689939092", "206.561897223892",
"206.561914376115", "206.561918159163", "206.561920224708", "206.561923646396",
"210.960297150126", "213.686445206476", "213.686465120369", "216.866121212474",
"216.866126783458", "218.655948156729", "224.99604368852", "224.996057271547",
"224.9960602985", "224.99606521108", "224.996065562981", "224.996081278255",
"224.99608235309", "224.996082367668", "224.996082946744", "224.996083788204",
"224.996084848199", "224.996086776282", "224.996086790861", "224.996087079994",
"224.996087906553", "224.996088732797", "224.996089777746", "224.996090081518",
"224.996090618394", "224.996094529207", "224.996097816515", "230.190571318522",
"231.336326241386", "234.458609341892", "236.306289792777", "236.306305752159",
"236.306307393042", "236.306325713134", "239.032745245313", "239.032773084164",
"243.431791522707", "243.431797625395", "243.431808769598", "243.431811475523",
"245.221858182135", "251.562680070773", "251.56268455414", "251.562697978643",
"251.562700819679", "254.052524049611", "255.961907894589", "255.961909409804",
"255.961917967213", "257.469536195034", "258.68855355037", "258.688558827016",
"26.561897224065", "26.561909979624", "26.5619152872187", "26.5619165055352",
"26.561920485388", "26.5619245326763", "26.5619257724073", "260.536399738673",
"262.874018076851", "262.87401868698", "263.658950185096", "270.000000825397",
"270.000000825739", "270.000000825967", "270.000000826081", "270.000000826195",
"270.000000826309", "270.000000826423", "270.000000826651", "270.000000826765",
"270.000000826878", "270.000000826879", "270.00000082722", "270.000000827562",
"270.00000165051", "270.000001652902", "270.000001653814", "270.000001657631",
"270.000001661505", "270.000001663556", "270.000001664809", "270.000002479914",
"270.000002484092", "270.000003306546", "270.000003311075", "270.000003315262",
"270.000004144966", "270.000004158936", "270.000004979198", "270.000005782192",
"270.000007465657", "277.125995411424", "277.125996790413", "277.126003803657",
"281.311454747624", "284.038107767959", "288.437300278551", "29.0512461969053",
"291.804137106086", "293.201447869319", "296.568183955338", "296.568186601703",
"296.568187257637", "296.568187269301", "296.568188593043", "296.568211697749",
"296.568217115325", "296.568231666689", "30.9602599760489", "30.9602987561062",
"303.693725157126", "315.003904193081", "315.003905470703", "315.003906325462",
"315.003910440718", "315.003911033758", "315.003911563215", "315.003911845393",
"315.003912005172", "315.003913746145", "315.003914572848", "315.003916515341",
"315.003917865386", "315.003917879964", "315.00391928621", "315.003919300789",
"315.003925739793", "315.003927795227", "315.003929770525", "315.00394101503",
"315.003946345431", "315.003956610649", "322.128861928285", "323.133860711499",
"323.13386938527", "323.133916640514", "326.313549702075", "33.6864487776922",
"333.438072719985", "333.438075455662", "333.438081405064", "333.438081416726",
"333.438112671245", "338.201312240365", "341.567396605941", "341.567401021678",
"348.691571595965", "350.538959809096", "44.9960299262225", "44.9960433893507",
"44.9960647858047", "44.9960714405142", "44.9960796387537", "44.9960796533323",
"44.9960813075886", "44.9960821346143", "44.996083165653", "44.9960831802316",
"44.9960854271517", "44.9960862684335", "44.9960870802999", "44.9960870948785",
"44.9960889662424", "50.1905434337506", "53.1262908803736", "54.4585694603986",
"56.3062693546384", "56.3062770335264", "59.032755274607", "63.4317723208886",
"63.4317848802781", "63.4317895372392", "63.4317935836423", "63.4317955060977",
"63.4318074457203", "66.0345895371247", "67.3773348964212", "68.1958709110713",
"71.562668281165", "74.052516819087", "75.9618996568146", "78.6885389399677",
"78.6885408616536", "79.6937487439011", "85.6006837571501", "89.9999941872715",
"89.9999950129589", "89.9999958484933", "89.9999966753091", "89.9999966878713",
"89.9999975116924", "89.9999975159082", "89.999997518263", "89.9999983366149",
"89.9999983395774", "89.9999983449896", "89.9999983460151", "89.999998346186",
"89.9999983470975", "89.9999983477812", "89.9999983479521", "89.9999991689056",
"89.9999991699311", "89.9999991714124", "89.9999991716402", "89.9999991719821",
"89.9999991728936", "89.9999991730076", "89.9999991731215", "89.9999991732354",
"89.9999991735772", "89.9999991735773", "89.9999991736912", "89.9999991738051",
"89.9999991739191", "89.999999174033", "89.9999991742609", "98.1312035976929",
"99.4635877243328", "99.4635933528321", "99.4635973787254"), class = "factor"),
Relative_Heading_Out = structure(c(84L, 3L, 120L, 273L, 213L,
403L, 38L, 180L, 297L, 73L, 409L, 213L, 74L, 210L, 326L,
166L, 121L, 10L, 162L, 212L, 295L, 44L, 256L, 5L, 395L, 163L,
88L, 263L, 342L, 55L, 288L, 128L, 181L), .Label = c("-1.08941367216175e-06",
"-1.27762228885331e-06", "-1.64088351084501e-06", "-1.97529777778982e-06",
"-10.6207232354447", "-107.098299845166", "-108.432605871424",
"-11.3096297403433", "-11.3102336441757", "-11.3102347353711",
"-11.3114558136556", "-11.3114593966472", "-11.6348360542887",
"-110.383710239579", "-111.798690769161", "-116.561876778859",
"-116.568179982698", "-119.048575511772", "-12.5275134113761",
"-125.54290253755", "-126.873674566913", "-134.996083788201",
"-134.99608874744", "-135.003907962816", "-135.003910222254",
"-135.003910440718", "-135.003912920006", "-135.003916226374",
"-135.003917053256", "-135.003920346791", "-14.0343965682479",
"-14.0344008293472", "-14.0381027887613", "-14.9333752469922",
"-15.2563802581807", "-151.923815028848", "-153.431797335435",
"-153.431814787652", "-153.438076376928", "-153.438078534744",
"-153.438081405064", "-155.224175835841", "-16.5895970517398",
"-161.564269704982", "-161.564270216045", "-163.070873427349",
"-168.688559655604", "-173.087896236407", "-178.210035361013",
"-18.4357312990837", "-18.4357320431515", "-18.4357376166744",
"-18.4372944963587", "-18.4373234043367", "-18.4374360606442",
"-180", "-180.00000165239", "-180.000001652846", "-180.000001653188",
"-180.00000165359", "-180.000001653757", "-189.462544556087",
"-19.440191483388", "-191.310236746892", "-198.432598104786",
"-198.435737329084", "-201.804133233895", "-203.197372900173",
"-206.561923646396", "-213.686445206476", "-213.693730144167",
"-216.866121212474", "-22.6226642752191", "-224.996065562981",
"-224.996082946744", "-224.996084848199", "-224.996086776282",
"-224.996090081518", "-225.003911859839", "-225.003915399271",
"-225.003918706729", "-23.2014262201339", "-235.306909073552",
"-236.306305752159", "-24.2299694795691", "-243.438082243148",
"-251.562697978643", "-251.572156808192", "-26.561920485388",
"-26.5619245326763", "-26.5682154538194", "-270.000000825739",
"-270.000000826081", "-270.000000826195", "-270.000000826423",
"-270.00000082722", "-270.00000165051", "-270.000001652902",
"-270.000001663556", "-270.000005782192", "-270.007837978621",
"-278.137789746516", "-296.568183955338", "-296.568186601703",
"-30.9658311909307", "-30.9672302310985", "-315.003904193081",
"-315.003911033758", "-315.003911845393", "-315.003912005172",
"-315.003913746145", "-315.003914572848", "-315.003916515341",
"-315.003917865386", "-315.00391928621", "-322.128861928285",
"-322.129645823558", "-326.313549702075", "-33.6864487776922",
"-33.6936950910497", "-33.6937058033612", "-33.6937221353792",
"-333.438075455662", "-341.567396605941", "-36.8661267834578",
"-36.8698974613882", "-38.6636779175493", "-4.39902960567105",
"-4.63602950075966", "-40.6024036695531", "-44.99604368852",
"-44.9960796387537", "-44.9960812782547", "-44.9960812928333",
"-44.9960821346143", "-44.996083165653", "-44.9960831802316",
"-44.9960837736256", "-44.9960862684335", "-44.9960870799939",
"-44.9960870802999", "-44.9960920371838", "-44.9960945292073",
"-45.0039062965318", "-45.0039099113321", "-45.0039104261803",
"-45.0039137610075", "-45.0039159931442", "-45.0039170385072",
"-45.0039170385603", "-45.0039184592101", "-45.0039195195613",
"-45.0039195342731", "-45.0039244841527", "-45.0039501982616",
"-49.2730539449748", "-50.1905434337506", "-50.5335956897864",
"-56.3063089215069", "-56.3135357050287", "-6.55934059068386e-07",
"-63.4286491352498", "-63.4317895372392", "-63.438071894588",
"-63.4380797638232", "-63.4381002876273", "-63.4381102417655",
"-66.7985762616031", "-69.0799574007877", "-69.2279887677529",
"-69.782098428514", "-7.01324449892127e-06", "-7.76602933812626",
"-71.5579915325294", "-71.5580050437492", "-71.5626782590484",
"-71.5674001952552", "-71.5674035490282", "-71.5720922472722",
"-71.5721163197041", "-71.5721653034134", "-78.6825596495758",
"-8.12994870632622", "-8.13108046273612", "-8.26243820029049e-07",
"-8.26479720217321e-07", "-8.26631662675936e-07", "-8.26992447855446e-07",
"-8.54759605317668e-07", "-81.8643827331808", "-82.8740180768513",
"-89.9921636459231", "-89.9921690352491", "-89.9921952742039",
"-89.999998346186", "-89.9999991719821", "-89.9999991731215",
"-89.9999991732354", "-89.9999991735772", "-89.9999991736912",
"-89.9999991738051", "-89.9999991739191", "-90.000000825967",
"-90.0000008260809", "-90.0000008264228", "-90.0000008267646",
"-90.0000008268785", "-90.0000008280179", "-90.0000033110747",
"-90.0078754520491", "-90.0079289020773", "-94.7688606836447",
"0", "1.21831646993087e-06", "1.6576310599703e-06", "1.66235020060412e-06",
"101.302350452585", "106.496883952078", "108.427537580527",
"108.44197890194", "11.3096332494953", "11.3102394284159",
"11.3113335227112", "11.3114583072521", "116.561916857852",
"116.56192105113", "116.568187269301", "117.402527859642",
"12.2476780128984", "120.957965917544", "125.532495222411",
"132.268196345264", "134.996085645806", "134.996087906417",
"134.996090907599", "135.00390464519", "135.003907948238",
"135.003910222254", "135.003911252625", "135.003911281782",
"135.003911680336", "135.003912905428", "135.003912920006",
"135.003912934585", "135.00391516638", "135.00391928621",
"135.003919548889", "14.0344008293472", "143.130106780739",
"15.2567559249757", "153.433391310084", "153.438081405064",
"153.438085623885", "161.562685384209", "161.564271367631",
"161.564271627437", "161.565836751711", "18.4341646234914",
"18.4341666601178", "18.4341668582031", "18.4341672821188",
"18.4357306931675", "18.4357346476826", "18.4357360052061",
"18.435740353848", "18.4372930648732", "18.8010696166384",
"180", "180.000001651478", "180.00000165239", "180.000001652497",
"180.000001652846", "180.000004968184", "187.12599956293",
"187.695688180681", "19.5660942200254", "191.309633889126",
"194.038104488152", "198.432585234004", "2.20765036829107e-06",
"201.804138766508", "206.561920224708", "206.561923646396",
"206.566618439315", "206.568189420149", "21.1640959090685",
"213.693732308918", "222.278699473017", "224.99608235309",
"224.996086790861", "224.996089777746", "224.996097816515",
"225.00391951929", "225.003920954773", "229.769652363221",
"243.431808769598", "247.626592898806", "251.567401848443",
"251.572173725974", "255.961909409804", "26.561909979624",
"26.5619143761148", "26.5619152872187", "26.561918159163",
"26.561920485388", "26.5619245326763", "26.5634850427263",
"26.5656849738252", "26.5666170276959", "26.5674578635275",
"26.5681908992693", "270.000000825739", "270.000000825967",
"270.000000826081", "270.000000826195", "270.000000826423",
"270.000000826651", "270.000000826765", "270.000000826879",
"270.00000082722", "270.000000827562", "270.00000165051",
"270.000001657631", "270.007821474476", "270.007864984721",
"288.442043790965", "3.5553682664613e-07", "3.96653496181898e-06",
"30.9602599760489", "30.9658300606604", "30.9658510086751",
"30.967256419496", "315.003906325462", "315.003911033758",
"315.003913746145", "315.003916515341", "315.003917865386",
"315.003917879964", "33.6864487776922", "33.6936728303563",
"33.6937173620532", "33.6941930107691", "333.438072719985",
"333.438075455662", "333.438081416726", "348.691571595965",
"350.538959809096", "38.0930036092936", "38.6628898865961",
"4.26803802337022", "40.6024008285175", "44.9960829613228",
"44.9960831802316", "44.9960848481989", "44.9960862684335",
"44.9960870799939", "44.9960870802999", "44.9960870948785",
"44.9960879211313", "44.9960906183937", "44.9960920517623",
"44.9960945292073", "45.0039102076872", "45.0039106205144",
"45.0039107371338", "45.0039110191976", "45.003911048449",
"45.0039137464255", "45.0039137464256", "45.0039165156846",
"45.0039195196753", "45.0039249122307", "45.0039253261187",
"49.579530827573", "58.6778036558414", "6.34009731534894",
"6.34105728056107", "63.4380772030091", "63.4380789368118",
"63.4380805900751", "63.438083494651", "63.4381110097399",
"63.4412765398765", "63.4412905471348", "64.6593257332515",
"66.0346118387025", "68.2013072611666", "7.12550172992948",
"7.76603379364138", "7.96445306150417e-07", "7.97902600879752e-07",
"71.5626800707728", "71.5627008196788", "71.5720922326936",
"74.052516819087", "74.7389307588194", "78.682521535368",
"78.6885588270165", "78.6915931880158", "8.12994364547711",
"8.12994419615723", "8.26137920739711e-07", "8.28036945677013e-07",
"83.0050894051913", "85.2304874382689", "87.5169609528689",
"89.9921898840171", "89.999998346186", "89.9999991716402",
"89.9999991731215", "89.9999991732354", "89.9999991735772",
"89.9999991736912", "89.9999991738051", "89.9999991739191",
"89.999999174033", "89.9999991742609", "9.46359418141975",
"90.0000008260809", "90.0000008261949", "90.0000008263088",
"90.0000008264227", "90.0000008264228", "90.0000008267646",
"90.0000008268785", "90.0000016529025", "93.1724357049647"
), class = "factor"), Movement_Out = structure(c(2L, 1L,
1L, 3L, 3L, 3L, 2L, 3L, 4L, 1L, 3L, 3L, 3L, 2L, 4L, 2L, 1L,
3L, 3L, 2L, 3L, 2L, 4L, 1L, 3L, 2L, 2L, 1L, 1L, 1L, 4L, 1L,
2L), .Label = c("forward", "left", "non-moving", "right"), class = "factor"),
Changes_Out = structure(c(1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L), .Label = c("0",
"1"), class = "factor"), AccPosNeg_Out = structure(c(2L,
2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 2L), .Label = c("0", "1"), class = "factor"), AccChange_Out = structure(c(2L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
1L, 2L), .Label = c("0", "1"), class = "factor")), .Names = c("Dist_Out",
"Speed_Out", "Acceleration_Out", "Absolute_Heading_Out", "Relative_Heading_Out",
"Movement_Out", "Changes_Out", "AccPosNeg_Out", "AccChange_Out"
), row.names = c(NA, 33L), class = "data.frame")
This is the function in rollapply:
function (Dist_Out, Speed_Out, Acceleration_Out, Absolute_Heading_Out,
Relative_Heading_Out, Movement_Out, Changes_Out, AccPosNeg_Out,
AccChange_Out)
{
if (max(Speed_Out) >= 2.45) {
Behaviour <- "walking"
}
else if (mean(Acceleration_Out[Acceleration_Out > 0]) >=
0.03 & (max(Dist_Out[Movement_Out != "non-moving"]) <=
2.17 & (mean(Speed_Out) >= 0.23 & (sum(Dist_Out[Movement_Out !=
"non-moving"]) >= 11.92)))) {
Behaviour <- "grazing"
} else Behaviour <- "resting"
return (Behaviour)
}
This is a shortened version of the function. Would appreciate any help with this. Thanks.
The first argument of rollapply must have columns all of the same type, e.g. all numeric so the data frame of the question which has columns of different types cannot be used. Also even if that were fixed and a data.framne with numeric columns only were used, the rollapply function in the question would attempt to pass a matrix to it but the function then uses the matrix as if it were a function.
The example in the question is really too large for SO (examples should be cut down to make them minimal yet still illustrate the problem) so here is a smaller example using the built in data frame BOD to illustrate rollapplying a function divide over a data.frame all of whose columns are numeric.
library(zoo)
divide <- function(demand, Time) sum(demand) / sum(Time)
rollapply(BOD, 3, function(m) divide(m[, "demand"], m[, "Time"]), by.column = FALSE)
## [1] 6.266667 5.033333 4.216667 3.212500
The above the same as:
c(divide(BOD[1:3, "demand"], BOD[1:3, "Time"]),
divide(BOD[2:4, "demand"], BOD[2:4, "Time"]),
divide(BOD[3:5, "demand"], BOD[3:5, "Time"]),
divide(BOD[4:6, "demand"], BOD[4:6, "Time"]))
The rollapply command could alternately be written like this using with
rollapply(BOD, 3, function(m) with(as.data.frame(m), divide(demand, Time)),
by.column = FALSE)

R: prediction of stock price by neural-network [duplicate]

This question already has an answer here:
stock price prediction by using nnet
(1 answer)
Closed 9 years ago.
stock<-structure(list(week = c(1L, 2L, 5L, 2L, 3L, 4L, 3L, 2L, 1L, 5L,
1L, 3L, 2L, 4L, 3L, 4L, 2L, 3L, 1L, 4L, 3L), close_price = c(774000L,
852000L, 906000L, 870000L, 1049000L, 941000L, 876000L, 874000L,
909000L, 966000L, 977000L, 950000L, 990000L, 948000L, 1079000L,
NA, 913000L, 932000L, 1020000L, 872000L, 916000L), vol = c(669L,
872L, 3115L, 2693L, 575L, 619L, 646L, 1760L, 419L, 587L, 8922L,
366L, 764L, 6628L, 1116L, NA, 572L, 592L, 971L, 1181L, 1148L),
obv = c(1344430L, 1304600L, 1325188L, 1322764L, 1365797L,
1355525L, 1308385L, 1308738L, 1353999L, 1364475L, 1326557L,
1357572L, 1362492L, 1322403L, 1364273L, NA, 1354571L, 1354804L,
1363256L, 1315441L, 1327927L)), .Names = c("week", "close_price",
"vol", "obv"), row.names = c(16L, 337L, 245L, 277L, 193L, 109L,
323L, 342L, 106L, 170L, 226L, 133L, 72L, 234L, 208L, 329L, 107L,
103L, 71L, 284L, 253L), class = "data.frame")
This is subset of data I have. I split the data, one for training and the other for testing.
obs<- sample(1:21, 21*0.5, replace=F)
tr.Nam<- stock[obs,]; st.Nam<- stock[-obs,]
library(nnet)
Nam_nnet<-nnet(close_price~., data=tr.Nam, size=4, decay=5e-4)
summary(Nam_nnet)
y<-tr.Nam$close_price
p<-predict(Nam_nnet, st.Nam, type="raw")
p
tt<-table(y,p)
summary(tt)
tt
By this nnet procedure, I expect "p" to predict close_price. However, the values of "p" are only "1"s or "Na"s.
What should I do to predict the close_price properly, with nnet?
By default, nnet uses logistic output units, i.e., tries to predict a binary variable.
You want linear output units.
Nam_nnet <- nnet(
close_price ~ .,
data = tr.Nam,
size = 4, decay = 5e-4,
linout = TRUE
)
p <- predict(Nam_nnet, st.Nam, type="raw")
plot( p, st.Nam$close_price )
However, the internal nodes are still logistic
(and you probably want that, if you are using a neural network in the first place):
since the values of the variables are very large, the nodes saturate,
output a constant value, and the optimizer is stuck on a plateau...

Resources