rollapply classes each segment the same - r
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)
Related
How to make scatterplot with colors based on a column and add a mean line through stats_summary with grouping based on another column?
I have a data.frame (see below) and I would like to build a scatterplot, where colours of dots is based on a factor column (replicate). I simultaneously want to add a line that represents the mean of y, for each x. The problem is that when I define the stat_summary it uses the colours I requested for groupingand hence I get three mean lines (for each color) instead of one. Trying to redefine groups either in ggplot() or stat_summary() function did not work. if I disable colors I get what I want (a single mean line). How do I have colors (plot # 1), yet still have a single mean line (plot # 2)? structure(list(conc = c(10L, 10L, 10L, 25L, 25L, 25L, 50L, 50L, 50L, 75L, 75L, 75L, 100L, 100L, 100L, 200L, 200L, 200L, 300L, 300L, 300L, 400L, 400L, 400L, 500L, 500L, 500L, 750L, 750L, 750L, 1000L, 1000L, 1000L), citric_acid = c(484009.63, 409245.09, 303193.26, 426427.47, 332657.35, 330875.96, 447093.71, 344837.39, 302873.98, 435321.69, 359146.09, 341760.28, 378298.37, 342970.87, 323146.92, 362396.98, 361246.41, 290638.14, 417357.82, 351927.66, 323611.37, 416280.3, 359430.65, 327950.99, 431167.14, 361429.91, 291901.43, 340166.41, 353640.91, 341839.08, 393392.69, 311375.19, 342103.54 ), MICIT = c(20771.28, 18041.97, 12924.35, 49814.13, 38683.32, 38384.72, 106812.16, 82143.12, 72342.43, 156535.39, 128672.12, 119397.14, 187208.46, 167814.92, 159418.62, 350813.47, 357227.48, 295948.31, 505553.77, 523282.46, 489652.3, 803544.84, 704431.61, 654753.29, 1030485.41, 895451.64, 717698.52, 1246839.19, 1309712.63, 1212111.53, 1930503.38, 1499838.89, 1642091.64), replicate = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L ), .Label = c("1", "2", "3"), class = "factor"), MICITNorm = c(0.0429150139016862, 0.0440859779160698, 0.0426274317575529, 0.116817357005636, 0.116285781751102, 0.116009395182412, 0.238903293897827, 0.238208275500519, 0.238853235263062, 0.359585551549246, 0.358272367659634, 0.34935932285636, 0.494869856298879, 0.489297881187402, 0.493331701877276, 0.968036405822146, 0.98887482369721, 1.01827072661558, 1.21131974956166, 1.48690347328766, 1.51308744189056, 1.93029754230503, 1.95985403582026, 1.99649737297637, 2.38999059622215, 2.47752500616233, 2.45870162403795, 3.6653801002868, 3.70350995307641, 3.54585417793659, 4.90731889298706, 4.81682207885606, 4.79998435561351 )), class = "data.frame", row.names = c(NA, -33L)) ggplot(xx, aes (conc, MICIT, colour = replicate)) + geom_point () + stat_summary(geom = "line", fun = mean)
Use aes(group = 1): ggplot(xx, aes(conc, MICIT, colour = replicate)) + geom_point() + geom_line() + stat_summary(aes(group = 1), geom = "line", fun = mean)
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")
Rotating y axis labels with mosaic plots WITHOUT overlap
This question is extremely similar to this one yet from another point of view which has not been responded. Following the proposed code, I am able to generate mosaic plots and rotate the labels so that they are legible. The problem comes when (it seems) the mosaic() function from vcd package does not recognise the rotation and so it does not adapt the graph to fit the labels, yielding results like the following: Is there any way to change the margins between the labels and the titles? I would be surprised if I am the first one that has encountered this issue. I am open to using other packages to get mosaic graphs if applicable as well. Code aux = structure(c(0L, 0L, 3L, 46L, 107L, 14L, 0L, 0L, 4L, 0L, 0L, 2L, 9L, 0L, 23L, 2L, 1L, 3L, 14L, 1L, 8L, 26L, 6L, 11L, 6L, 1L, 6L, 0L, 1L, 1L, 29L, 10L, 62L, 1L, 3L, 1L, 1L, 3L, 1L), .Dim = c(3L, 13L), .Dimnames = list(abcdefghi = c("Madrid", "Valencia", "Granada" ), jklmnopqr = c("roknbjftxcwl", "mfchldbxuyig", "gtyoxeduijpw", "akbcefymvsiw", "ucbfxplietqk", "mzeykauprfdh", "piermgawyjht", "chjvatqbylxo", "merhcogjflbd", "wiyrugvmhjlq", "glszdqmjhkov", "giowaxrtsknm", "pxucytzvljqw")), class = "table") library(vcd) colours = c("brown","darkgreen","darkgrey","orange","darkred","gold","blue","red", "white","pink","purple","navy","lightblue","green","peachpuff","violet","yellow","yellow4") aux_names = names(attr(aux,"dimnames")) mosaic(aux,main=paste(aux_names,collapse=" vs. "), gp=gpar(fill=matrix(sample(colours,max(nrow(aux),ncol(aux))),1,max(nrow(aux),ncol(aux)))), pop = FALSE,labeling = labeling_border(rot_labels=c(90,0,0,0), just_labels=c("left","right")))
This code should do what i think you're after. mosaic(aux,main=paste(aux_names,collapse=" vs. "), gp=gpar(fill=matrix(sample(colours,max(nrow(aux),ncol(aux))),1,max(nrow(aux),ncol(aux)))), pop = FALSE,labeling = labeling_border(rot_labels=c(90,0,0,0), just_labels=c("left","right"), offset_varnames = c(8,8,8,8)), margins = c(10, 10, 10, 10))
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.