ddply dropping rows with zero sum - r

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")

Related

Calculate random variables from grouped dataframe

I have a data frame called stats. I'd like to group by month_name and item and generate a random variable drawn from a normal distribution in a new column called rv.
This is the code I tried but it repeats the generation of 1 random variable in the rv column:
stats %>%
group_by(month_name, item) %>%
mutate(rv = rnorm(1, mean = mean, sd = sd))
The goal is to eventually replicate the rv output 10,000 times. How can I modify my code to generate the random variable for every row once and 10,000 times?
This is my data:
structure(list(month_name = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L), .Label = c("January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"), class = c("ordered",
"factor")), item = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L), .Label = c("a", "b", "e", "d", "c"), class = "factor"),
min = c(39853.3676768762, 11963.7336771844, 28475.0829411315,
36241.9007031999, 35743.7750504859, 16092.552892924, 12528.9369640133,
28363.8516762228, 29557.1911907891, 20577.9268503088, 26012.6643332399,
43743.1386573406, 33757.0104608081, 24012.3644652027, 29987.8232384625,
26663.1671529956, 50724.1357441692, 33156.7203254077, 36604.0975795671,
32448.5497811945, 47818.2983698804, 25173.5205474241, 29648.7882981325,
39034.0329768052, 15083.5548610647, 41560.8896893507, 40563.2944650284,
48794.4526055819, 35895.1783353774, 30085.4271923688, 39675.7305889162,
33628.9557047603, 36950.5993766457, 30593.5091646214, 28957.5398056329,
37080.7178800747, 45454.3924797489, 28755.6280571895, 34733.1340290652,
37227.9361452194, 21493.809533368, 33292.9944106622, 28137.6372068055,
25582.8046285949, 23073.0637573296, 28846.9082264882, 19454.182866794,
33869.2858697563, 19016.7538627489, 30647.6876387916, 35123.8965500988,
34146.2780735908, 40593.6508043686, 26908.3734089905, 47178.2458120079,
24665.5899193578, 22701.4906439165, 50735.1331088719, 36108.7624278488,
21415.5715318508), lower = c(54524.7101912146, 26928.6804993352,
25119.8847919585, 45942.5372327181, 52100.762800828, 23399.2712234262,
14178.7907654734, 71366.6268933559, 49209.2124037853, 54643.7588467776,
48369.7944054794, 29515.3335011807, 41577.635577101, 25357.3837384686,
43253.4733925982, 43401.4748829102, 37741.3586860236, 52294.4029786582,
58136.6122795486, 43617.5523486807, 46648.1777348884, 47822.6060157009,
37122.0182632065, 65447.4620274838, 29544.1919272749, 54822.3562275875,
64814.4174753617, 65538.2587526896, 39975.4034746898, 59117.6049731313,
49024.4324422717, 25273.7368374795, 56946.7596272533, 50660.5745923196,
37221.8185672126, 30508.2772838287, 47172.6674212663, 52956.1465111511,
45488.8349086128, 52660.1832157037, 37406.8854102724, 25601.012749268,
41414.610113642, 41145.7009104373, 26879.9690641376, 69323.7347440924,
59453.3099916568, 19260.9187209561, 14090.2250971317, 41778.9038974128,
35013.9160392596, 39672.0871995261, 57517.2881078087, 52765.3573599843,
57267.2271717807, 54869.720268229, 58525.9231470629, 44610.285805162,
47317.3995094377, 17599.590085043), mean = c(58549.8098049081,
56374.4327553941, 39864.1715264267, 85333.1530921059, 64454.2358008729,
63343.4098283811, 69838.6859070403, 41935.3881398536, 40239.4399412696,
70073.2291007902, 57535.295477502, 76197.4454180647, 60836.2074195693,
64601.7379215889, 51599.3556004457, 49092.0124309883, 47319.767991988,
63121.0872241636, 43048.0322965586, 77405.4987695189, 64320.8901918307,
53059.7915920758, 63712.4934804165, 37248.933469329, 48285.12302248,
60352.1030623367, 67648.010113929, 52282.8579266665, 63868.4373429784,
71370.1455147326, 59275.2217698193, 74524.7831867724, 62464.1935824186,
50255.8945012446, 31094.1686136834, 75833.6439248775, 32190.7391406323,
77010.5148506178, 69635.0888164364, 65885.8987213858, 54022.7135642953,
35801.3865465657, 60637.9983665307, 90783.7721781328, 57264.0603250172,
59977.2976696403, 71712.656969139, 76705.4011709067, 89462.5059367925,
76714.0458753254, 56859.5782454854, 66820.0053236744, 58243.7435076688,
52843.8704599132, 77247.3384533588, 55515.7748808548, 75004.3165800858,
88370.1869726297, 68628.9281194796, 53895.0496305422), median = c(42352.1610450345,
57330.3183802072, 55273.2047201131, 82351.3852530883, 46370.4898234873,
52386.0432388715, 47943.0683307536, 53897.781347776, 67858.0064600009,
73013.024717384, 83116.7356352266, 44401.5903576421, 69025.6068023045,
81625.3403276092, 43344.4404418446, 49701.9746204065, 44889.5603216509,
86449.7649043697, 52150.9769065634, 58675.8138647348, 55665.7047792249,
44566.4888204713, 50517.7492643733, 73778.9515308994, 60652.1631558926,
87345.0069311662, 68268.9807235179, 41356.3226356087, 41585.1763113502,
75144.8373297139, 81967.7788670882, 66041.6207332688, 55103.8870449834,
77301.4195253735, 54130.4774678618, 65176.7990367632, 46834.9652749994,
65134.3889325556, 76621.5018669346, 89066.7483257445, 79344.8597627239,
50867.4889878177, 51326.3717332736, 74843.6262595514, 66235.6184875188,
98300.5112442494, 51378.9240605971, 61277.8214283028, 48915.1245226839,
52765.9194941648, 47028.8412992194, 74841.2039136489, 70896.5761749783,
67414.0877191645, 60655.1682545525, 42707.2850070942, 51244.6187187212,
70889.9732948709, 82834.1260629236, 56029.4540887989), upper = c(96808.9361470916,
72722.9262056796, 89079.513341868, 84709.1878768955, 87694.368834914,
87860.8548839792, 80996.3827453218, 84247.9259137302, 95585.6388675179,
57338.746606262, 88681.3926853573, 87957.989278465, 87360.6574510974,
92664.4254709955, 73493.0826366849, 84230.5990186054, 81442.2517006442,
87801.9592453634, 107883.319372054, 101919.939543795, 78090.4252899963,
70239.1417329303, 100675.767786787, 99806.9236049608, 71452.5071326737,
73879.3479602876, 106131.22309752, 125238.035074805, 76731.6350473027,
105563.285669622, 98604.105083167, 88657.8428176833, 81133.2031578456,
92495.2957986084, 104836.803460225, 102419.6178137, 86160.3548401189,
87287.9179449312, 72987.3973022452, 73185.0732579627, 90916.179982239,
111282.33982277, 142168.512194455, 100479.774695548, 118375.00968986,
116099.107730658, 105747.461541425, 106715.198136428, 128585.197217447,
87996.5319472346, 67831.1501517932, 109713.080164634, 78535.3157822644,
128602.704986898, 82213.8086826659, 118591.773718681, 66518.2467960131,
91250.5061727746, 117072.914540123, 114524.034290364), max = c(137612.711045413,
142519.370905613, 137456.124250483, 149209.014602568, 158745.717583772,
144886.189765236, 168837.723206789, 148308.890270968, 158590.65413993,
152288.303209753, 154042.306686713, 143922.848061827, 147477.579594905,
147438.066965268, 141502.628117831, 150285.096748915, 148713.594899874,
156656.255445038, 151517.357942321, 146177.731181398, 130056.291991729,
150991.849546995, 150476.190905448, 140149.802748207, 162573.574139209,
124218.878401843, 140313.610415297, 156852.359228369, 147676.550419975,
139922.178103581, 131822.195549853, 143008.968758112, 142237.425864494,
148756.818388612, 123905.560034301, 157126.60664862, 132868.19652461,
137884.902850549, 142164.212835827, 144616.429331364, 154277.663061656,
156870.781144851, 170948.478868233, 154970.297432983, 144661.430142095,
151193.528913062, 136056.623739965, 132695.069145067, 144366.408646971,
154456.483407293, 143518.023088591, 145811.265404348, 139900.024678788,
127547.709882734, 149995.24047052, 145400.958382574, 159524.480570906,
118905.663549293, 161631.72583606, 147524.546274058), sd = c(9989.37951375166,
9906.50689980405, 9903.6852849217, 10008.3321579478, 10075.4653993515,
10063.7122293343, 10053.0016932606, 9826.1129055558, 9855.88655389009,
10028.7176055065, 10070.3833732403, 9941.07465801432, 10094.2667749602,
9910.53181242413, 10104.5889493016, 9851.70104229335, 9972.91821342281,
10080.4485086333, 10044.5102818099, 10037.3707232711, 10025.1107006076,
10022.3659427419, 9941.51637265177, 9873.12826319285, 10027.9036424549,
10033.6518983864, 9970.47127759776, 9937.3319252128, 10013.3439414305,
10030.3125017708, 10168.5115559098, 10213.3568382367, 9990.24289183087,
9968.82189362707, 10048.7504375345, 10015.8411633632, 10037.6851291425,
9925.92765463682, 9835.81447415085, 9782.6505066721, 10033.5360418173,
9991.76186224687, 9924.86818104305, 9970.41809893224, 9980.55197551292,
9886.97032019385, 9925.73912143071, 9971.01687402101, 9858.19281102242,
9969.19466304141, 9955.12658457894, 10139.5950943687, 9967.09479735319,
10168.1650679826, 10023.9501235604, 9821.41776472295, 10064.1149573067,
10134.8532916488, 9943.57024828908, 9833.93164357077)), row.names = c(NA,
-60L), groups = structure(list(month_name = structure(1:12, .Label = c("January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"), class = c("ordered",
"factor")), .rows = structure(list(1:5, 6:10, 11:15, 16:20, 21:25,
26:30, 31:35, 36:40, 41:45, 46:50, 51:55, 56:60), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 12L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
you can try data.table:
library(data.table) # load data.table
setDT(stats) # convert stats to data.table
stats[, rv := rnorm(.N, mean = mean, sd = sd), by = .(month_name, item)]
In your example there's only one record of each combination month_name and item, but I can imagine your real data has more.
The := is an assignment operator. You'll assign the result of rnorm into a new column called rv.
The by = part serves for grouping, see ?data.table.
EDIT TO ADD:
If you want 10,000 random variables, then:
stats[, new_rv := .(list(rnorm(1e4, mean, sd))), by = .(month_name, item)]
You already know the := and the by = parts, so let's dive into the expression in the middle:
The .(list()) bit will assign the resulting list (vector of 10,000 random numbers, in our case) to the variable (because we are using the assignment operator :=).
The very interesting thing is that with this .(list()) "combo" you can store complex things in a variable (column) of a data.table. I use it often to store things such as forecasts, plots or linear models, etc. by group: it is very useful!
Now, if you want to operate on your new variable, please keep in mind that it is a list, so you need to subset it accordingly:
If you want to check that the standard deviation of new_rv of row 1 is close to what you expect, the following code will throw an error:
stats[1, sd(new_rv)]
> Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
> is.atomic(x) is not TRUE
It is complaining that you are attempting to calculate sd() on a list. The correct code is:
stats[1, sd(new_rv[[1]])]
[1] 9926.439
The [[1]]] part is correctly subsetting the first element of the list.

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.

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)

How to add multiple data series to a scatterplot and how to format numbers to appear in standard form on y axis

My data set:
structure(list(Site = c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L), Average.worm.weight..g. = c(0.1934,
0.249, 0.263, 0.262, 0.4186, 0.204, 0.311, 0.481, 0.326, 0.657,
0.347, 0.311, 0.239, 0.4156, 0.31, 0.3136, 0.4033, 0.302, 0.277
), Average.total.immune.cell.count = structure(c(8L, 16L, 11L,
12L, 10L, 1L, 4L, 15L, 4L, 3L, 17L, 13L, 18L, 7L, 5L, 6L, 9L,
14L, 2L), .Label = c("0", "168750", "18650000", "200,000", "21,600,000",
"226666.6", "22683333.33", "2533333.33", "283333.333", "291666.6",
"335833.3", "435800", "474816666.7", "500000", "6450000", "729166.667",
"7433333.3", "9916667"), class = "factor"), Average.eleocyte.number = structure(c(2L,
5L, 14L, 10L, 1L, 1L, 6L, 1L, 6L, 7L, 1L, 9L, 15L, 8L, 12L, 3L,
11L, 13L, 4L), .Label = c("0", "1266666.67", "153333.3", "168740",
"17", "200,000", "2266666.667", "22683333.33", "23116666.67",
"264000", "283333.333", "442", "500000", "7.3", "9916667"), class = "factor")), .Names = c("Site",
"Average.worm.weight..g.", "Average.total.immune.cell.count",
"Average.eleocyte.number"), class = "data.frame", row.names = c(NA,
-19L))
This is my R script so far:
Plotting multiple data series on a graph
y1<-dframe1$"Average.total.immune.cell.count"
y2<-dframe1$"Average.eleocyte.number"
x<-dframe1$"Average.worm.weight..g."
plot.default(y1~x,type="p" )
points(y2~x)
I am trying to add to y series to the same scatterplot and I am struggling to do so, I want to have different symbols for the points so as to tell apart the two different data series. Also I would like the axes to meet on the bottom left hand side and would appreciate being informed as to how I can do that? I would also like the y axis to be in standard form, but do not know how to get R to do that.
Best regards.
K.
So this is an object lesson is getting your data in the correct format to begin with. Your numbers have commas, which R does not like. Hence the numbers get converted to character and imported as factors (which your structure(...) clearly shows. You need to fix that, or better yet get rid of the commas prior to exporting.
Something like this will work
colnames(dframe) <- c("Site","x","y1","y2")
dframe$y1 <- as.numeric(as.character(gsub(",","",dframe$y1,fixed=TRUE)))
dframe$y2 <- as.numeric(as.character(gsub(",","",dframe$y2,fixed=TRUE)))
plot(y1~x,dframe, col="red", pch=20)
points(y2~x,dframe, col="blue", pch=20)
But there are additional problems. One of the numbers (in row 12) is a factor of 10 larger than all the others, so the plot above is not very informative. It's hard to know if this is a data input error, or a genuine outlier in your data.
EDIT: Response to OP's comment
dframe <- dframe[-12,] # remove row 12
dframe <- dframe[order(dframe$x),] # order by increasing x
plot(y1~x,dframe, col="red", pch=20, type="b")
points(y2~x,dframe, col="blue", pch=20, type="b")
legend("topleft",legend=c("y1","y2"),col=c("red","blue"),pch=20)

Resources