Add a column and repeat every 16 dplyr - r

I need to add a column, column name "type", and for every 16 rows, change the row name to "type1, type 2 etc".
I tried book1$ID %/% 16 but not quite right.
This is the original data:
book1 <- structure(list(ID = 1:34, per_section = c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L,
2L)), class = "data.frame", row.names = c(NA, -34L))
This is my desired output:
output <- structure(list(ID = 1:34, Type = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L), per_section = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 1L, 2L)), class = "data.frame", row.names = c(NA,
-34L))

With %/% we may need to adjust by subtracting 1 and adding 1
book1$Type <- with(book1, (ID-1) %/% 16 + 1)
Or maybe more easier with gl
library(dplyr)
book1 <- book1 %>%
mutate(Type = as.integer(gl(n(), 16, n())), .after = 1)

Try this
book1$type <-ifelse(1:nrow(book1) %% 16 != 0 ,
1:nrow(book1) %% 16 , 16)

Related

warning when running clmm model

I am trying to run a clmm to examine the effects of average_Mg, average_Mn, and average_ZN on the response variable (Spawn_ID)
I have two random effects that are added into the model "(1|Time/ID)" (time is nested into ID and each ID has a different number of time because some ID's have more observations than other IDs) (see picture)
BLA14 has time 1-8 while BLA2 has time 1-11
here is my data:
data1 <- structure(list(Spawn_ID = structure(c(1L, 1L, 1L, 2L, 2L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L,
1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L), .Label = c("1",
"2", "3"), class = c("ordered", "factor")), average_Mg = c(0.0841567034686979,
0.00262726492114602, 0.353259164624795, -0.0364882169624394,
0.355390763410209, -0.151476304441742, -0.0936982567875358, 0.162382425722223,
-1.42681140542971, -1.07244649331608, -0.781118702245109, -1.21952099438996,
-1.38524008095868, -0.898983285649544, -1.01088138645042, -1.56153946055641,
-1.21949786511762, -0.887755361220044, -1.28496187594515, -1.82368718501736,
-1.44293956646485, -1.55766693069354, -1.58965315500885, -1.44731801677922,
-1.90014469879122, -1.39433781118039, -1.3954911611769, -0.866262522744268,
-0.893780676281797, -1.13725619637354, -1.50364564206296, -0.845596282408769,
-0.911535321390588, -1.41202503488084, -1.16477711028459, -0.928588557438047,
-1.0717825099406, -1.27071094552779, -0.102981887484371, -0.419552015986426,
0.207549199784127, -0.26688619247063, -0.0285388879140084, -0.104483586190019,
-0.374343912017509, -0.079286617457435, 0.502081027554914, 0.617397464912897,
0.658216645632926, 0.426442596169416, 0.437015933595451, -1.09407187513834,
-0.289722984650423, 0.189585827711078, 0.388488397202216, -0.814972632964376,
0.0191088328270176, -0.0788536189757766, -1.01469763732677, -0.532896761096259,
-0.319455285766885, 0.32814415425849, -1.10320849618017, -0.802227061565149,
-0.540984260212337, -1.14703007008055, -0.482835715257794, -1.23114539323637,
-0.881874890913403, -0.479104993907372, 0.722375571708869, 0.890893182481924,
1.1899980340988, 0.784338248057351, 0.6979419698913, 1.10833467622332,
0.749554401158789, 0.761841531876783, 1.38243732271989, 1.38537086804658,
0.686397638698291, 1.38660240844063, 1.90358175564482, 1.30513882860368,
0.9706833889022, 2.02786238069347, 0.633278771850179, 0.773554195390702,
1.26407209402018, 0.711780226990467, 1.12404623953355, 0.772411304871684,
0.746907192859431, 1.26546029276059, 0.754582077531832, 0.97865102792368,
1.25739249978455, 1.00030371910859, 1.20251423376581, 0.508886239812359,
1.0614400765502, 1.15560112394629, 0.810899892639843, 0.864356170995008,
1.0722853284304, 0.459017471399662, 0.622305015414975, 0.654778017554924,
0.630012469467092, 1.66357555743707, 1.51486425579301, 0.468256912570358,
1.46986769298999, 0.842853031161864, 0.527443923164085, 0.878231897515369,
1.01564664723517, 0.548373164352724, -1.54705977070176, -1.7628880927376,
-1.93886600741023, -1.75280825324115, -1.06329603003556, -1.76583856532739,
-2.03620478132805, -1.37852741943318, 0.491445103158986, 0.579237889782203,
0.581147814257234, 0.587993370694159, 0.673660535135936, 0.773224639425602,
0.472056000685565, 0.803037596940575, 0.686349802316703, 0.67297390357697,
0.877084884098423, 0.116853127650954, 0.633207695175741, 0.475407810726902,
0.410454398351338, 0.761825383439366, 0.049981065597767, 0.352528868363907,
1.08494544768163), average_Mn = c(0.550041395336084, 0.106013801445048,
0.195501740474326, 0.443055327801251, 0.166039412117922, 0.306826485641131,
0.0779488541952551, 0.253041943200378, -0.398175664467298, -0.248653116953824,
1.62701418452763, 2.12294900204613, -0.653505561079324, 1.18649551151282,
1.73706605332464, -0.663237964024895, -0.502022515338005, 1.37488148179862,
1.52730443891077, -1.02791508823558, -0.912000741847277, -1.13836183212639,
-1.0203769070181, -0.944729930532721, -1.08022080193656, -0.798637523498975,
-0.892966674264699, -0.106867747759299, -0.446077807686831, 0.260985129778798,
0.34207999245625, -0.256052324398952, -0.455901687059591, -0.124589605852459,
-0.330899017708168, -0.155941754430574, 0.179724557145222, -0.13333037309261,
-0.467033378092841, -1.02354293597083, -0.324678510862249, -0.780338971498965,
-0.638323663037884, -0.833876759864611, -1.23290374823043, -0.791651926017789,
-0.507461130613381, -0.8368428812325, -0.0557361418099642, -0.804531913988396,
-0.704346938462444, -0.483778753666885, 0.393712753506288, 0.427715184816051,
0.479775271348421, -0.28130811769396, 0.485295306906789, 0.29873238753337,
-0.32311305698219, -0.153688760742991, 0.566586154995381, 0.432231338528875,
1.06596225482086, 1.35634853919191, 3.80694326707882, 0.810475117293976,
3.0387608847462, 0.633718464176746, 1.1751890651466, 3.9845446592986,
-1.37297875557724, -1.09420268685371, -0.962614525736854, -1.29805138281804,
-1.31055976157738, -0.925719147858883, -1.23243978183704, -1.33945462492742,
-0.733417598434015, -0.930363022530595, -1.27774840900127, -0.551645520494324,
1.07748304807901, -0.396816101914801, -0.619403529151145, 0.904015824338447,
-0.287220128038401, -0.413048098445259, 0.612345039773056, 0.12520419272066,
0.144889119165393, -0.376872327860885, -0.371321461123647, 0.713414294431202,
-0.220319649249486, 1.91457944140036, 1.96765430981412, -0.121347747943895,
1.83594114293694, -0.874846076775421, 1.57970089137696, 1.93625153606136,
0.346364402583701, 1.77363591575721, 0.715776292044604, -0.11504600156397,
0.957194866002839, -0.887101387136546, -0.780049232064872, -0.336706490132965,
-0.70438883179631, -0.873817482659086, -0.284434200328209, -0.568305044660584,
-0.775993306095371, -0.771770658609636, -0.383838373137703, -0.811970593682299,
-0.73465457432939, 0.0911351344017551, 1.2707682140586, -0.777318831552788,
1.11134230637355, -0.783796841885501, 0.259370669933754, 1.73241147006831,
-0.725970777900951, -0.438118569726494, -0.278337133147783, -0.493758812846972,
-0.518179622331699, -0.389789488287258, -0.298381997017347, -0.612423478602631,
-0.429659970678406, -0.292999247051666, -0.435183828919258, -0.897633859213429,
0.507974413224703, 1.43201049921323, -0.66047577071763, 0.786340467145023,
-0.95656045144662, -0.132752976801546, 1.18466694854915), average_Zn = c(0.199618761426643,
0.591792310386536, 1.3751346661716, 0.332582639073901, 0.560402369414685,
0.11570820588524, 0.192150397195304, 1.39288706671957, -0.394083581015067,
-0.0798076017654593, 1.17669193020793, 0.607211261125105, -0.452359206037806,
0.771410079352111, 0.494544784665782, -0.625267178823271, -0.39226745167263,
1.16595745311139, 0.395578830413474, -1.34293822466577, -0.742961944779728,
-1.55476909975203, -1.21117698813619, -0.705958753206111, -1.37118754349972,
-0.917928244368175, -0.974434964905969, -1.01929394953207, -1.12532371386981,
-1.22700631035692, -1.46133511017614, -0.952449170432, -1.21027641358059,
-1.3226625940125, -1.16430485224588, -1.08770522025381, -1.18121144700268,
-1.40033431712296, -0.618587673725525, -0.91706750820117, 0.41088121270189,
-0.78430313474907, -0.409059258341318, -0.975516971079657, -1.27140585793008,
-0.198796133331597, -0.429336908924874, -0.0174222425394045,
-0.161004481467468, -0.405810552877399, -0.370285632509627, -0.89866639987887,
1.10645284167993, 1.8809570525936, 1.92254222169752, -0.0950224222539316,
1.58853988090432, 1.78386607934181, -0.817728723571643, 0.459412598781715,
1.423493404365, 1.7768774695448, -1.12006569075839, -0.751966514798299,
-0.488043380249217, -1.11596293893295, 0.273653921626916, -1.30713710349688,
-0.886375551803829, 0.0239199838971528, -1.3593629685693, -0.643843217883837,
-0.281272478019681, -1.30622537314287, -1.33112371265123, -0.909101703993645,
-1.12755493783614, -1.40003719660867, -0.290337728601771, -0.28987232858238,
-1.19056584307422, -0.193624949117038, 0.975332436429838, -0.218160046225909,
-0.132462974128803, 1.39468641578899, -0.302150834825655, -0.107400685186729,
1.125970810338, -0.256092735038377, 0.158506693687128, -0.378339394736207,
-0.349357413758689, 0.803679188551612, -0.146048595417096, -0.286845588476566,
0.158259418849209, -0.323113138916157, 0.108646496285904, -0.771878347558138,
0.0273033684358348, 0.0453235133932348, 1.28361673646552, 1.29073622845612,
1.24630790251793, 0.944986804400661, 0.981991016697687, 1.3473053430083,
0.827378725150942, 2.48208427225106, 2.69830294706435, 1.03039606923181,
2.27492560926846, 1.10744827135352, 1.35324680735362, 1.84388638700427,
1.52873550347441, 1.40071898822651, 0.260928795100606, -1.16734754082697,
-0.878145608518404, -0.00331768573206932, -0.910939824808822,
0.125144947309071, -1.67327192782442, -0.531813491778335, -0.546522958404239,
-0.0196439100156154, 0.0745476695275746, 0.794370508556172, -0.210218336601184,
0.445705743628815, 0.0718198426307307, -0.122444182892431, 0.184211311538213,
0.183194361394073, 1.12410771197491, -0.741883323524288, 0.716760255429898,
1.76025647136123, -0.00419702451761512, 1.25254232235371, -0.880576082155036,
-0.00114985682377106, 1.78721959263295), ID = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), .Label = c("BLA14",
"BLA2", "BLA20", "BLA209", "BLA21", "BLA211", "BLA238", "BLA24",
"BLA25", "BLA283", "BLA307", "BLA31", "BLA42", "BLA47", "BLA5",
"BLA79", "BLA80"), class = "factor"), Time = structure(c(2L,
5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L,
4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L,
3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L,
2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 11L, 3L, 6L, 8L, 1L, 4L, 7L,
10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 5L, 8L, 11L, 2L, 3L, 6L,
9L, 4L, 7L, 10L, 1L, 2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 3L, 6L,
1L, 4L, 7L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 3L, 1L, 4L,
2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L,
6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L,
2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11"), class = c("ordered", "factor"
))), class = "data.frame", row.names = c(NA, -145L))
here is my model:
#model
clmm(Spawn_ID ~ average_Mg + average_Mn + average_Zn + (1|ID/Time), data = data1)
warning message
Warning messages:
1: Using formula(x) is deprecated when x is a character vector of length > 1.
Consider formula(paste(x, collapse = " ")) instead.
2: no. random effects (=161) >= no. observations (=145)

Putting nested random effects in polr function

I am trying to run an ordinal logistic regression mixed model with nested random effects using the polr function from package MASS.
average_Mg, average_Mn, and average_Zn are the predictor variables. the response variable is (Spawn_ID)
I have two random effects that are added into the model "(1|Time/ID)" (time is nested into ID and each ID has a different number of time because some ID's have more observations than other IDs) (see picture)
enter image description here
data:
data1 <- structure(list(Spawn_ID = structure(c(1L, 1L, 1L, 2L, 2L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L,
1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L), .Label = c("1",
"2", "3"), class = c("ordered", "factor")), average_Mg = c(0.0841567034686979,
0.00262726492114602, 0.353259164624795, -0.0364882169624394,
0.355390763410209, -0.151476304441742, -0.0936982567875358, 0.162382425722223,
-1.42681140542971, -1.07244649331608, -0.781118702245109, -1.21952099438996,
-1.38524008095868, -0.898983285649544, -1.01088138645042, -1.56153946055641,
-1.21949786511762, -0.887755361220044, -1.28496187594515, -1.82368718501736,
-1.44293956646485, -1.55766693069354, -1.58965315500885, -1.44731801677922,
-1.90014469879122, -1.39433781118039, -1.3954911611769, -0.866262522744268,
-0.893780676281797, -1.13725619637354, -1.50364564206296, -0.845596282408769,
-0.911535321390588, -1.41202503488084, -1.16477711028459, -0.928588557438047,
-1.0717825099406, -1.27071094552779, -0.102981887484371, -0.419552015986426,
0.207549199784127, -0.26688619247063, -0.0285388879140084, -0.104483586190019,
-0.374343912017509, -0.079286617457435, 0.502081027554914, 0.617397464912897,
0.658216645632926, 0.426442596169416, 0.437015933595451, -1.09407187513834,
-0.289722984650423, 0.189585827711078, 0.388488397202216, -0.814972632964376,
0.0191088328270176, -0.0788536189757766, -1.01469763732677, -0.532896761096259,
-0.319455285766885, 0.32814415425849, -1.10320849618017, -0.802227061565149,
-0.540984260212337, -1.14703007008055, -0.482835715257794, -1.23114539323637,
-0.881874890913403, -0.479104993907372, 0.722375571708869, 0.890893182481924,
1.1899980340988, 0.784338248057351, 0.6979419698913, 1.10833467622332,
0.749554401158789, 0.761841531876783, 1.38243732271989, 1.38537086804658,
0.686397638698291, 1.38660240844063, 1.90358175564482, 1.30513882860368,
0.9706833889022, 2.02786238069347, 0.633278771850179, 0.773554195390702,
1.26407209402018, 0.711780226990467, 1.12404623953355, 0.772411304871684,
0.746907192859431, 1.26546029276059, 0.754582077531832, 0.97865102792368,
1.25739249978455, 1.00030371910859, 1.20251423376581, 0.508886239812359,
1.0614400765502, 1.15560112394629, 0.810899892639843, 0.864356170995008,
1.0722853284304, 0.459017471399662, 0.622305015414975, 0.654778017554924,
0.630012469467092, 1.66357555743707, 1.51486425579301, 0.468256912570358,
1.46986769298999, 0.842853031161864, 0.527443923164085, 0.878231897515369,
1.01564664723517, 0.548373164352724, -1.54705977070176, -1.7628880927376,
-1.93886600741023, -1.75280825324115, -1.06329603003556, -1.76583856532739,
-2.03620478132805, -1.37852741943318, 0.491445103158986, 0.579237889782203,
0.581147814257234, 0.587993370694159, 0.673660535135936, 0.773224639425602,
0.472056000685565, 0.803037596940575, 0.686349802316703, 0.67297390357697,
0.877084884098423, 0.116853127650954, 0.633207695175741, 0.475407810726902,
0.410454398351338, 0.761825383439366, 0.049981065597767, 0.352528868363907,
1.08494544768163), average_Mn = c(0.550041395336084, 0.106013801445048,
0.195501740474326, 0.443055327801251, 0.166039412117922, 0.306826485641131,
0.0779488541952551, 0.253041943200378, -0.398175664467298, -0.248653116953824,
1.62701418452763, 2.12294900204613, -0.653505561079324, 1.18649551151282,
1.73706605332464, -0.663237964024895, -0.502022515338005, 1.37488148179862,
1.52730443891077, -1.02791508823558, -0.912000741847277, -1.13836183212639,
-1.0203769070181, -0.944729930532721, -1.08022080193656, -0.798637523498975,
-0.892966674264699, -0.106867747759299, -0.446077807686831, 0.260985129778798,
0.34207999245625, -0.256052324398952, -0.455901687059591, -0.124589605852459,
-0.330899017708168, -0.155941754430574, 0.179724557145222, -0.13333037309261,
-0.467033378092841, -1.02354293597083, -0.324678510862249, -0.780338971498965,
-0.638323663037884, -0.833876759864611, -1.23290374823043, -0.791651926017789,
-0.507461130613381, -0.8368428812325, -0.0557361418099642, -0.804531913988396,
-0.704346938462444, -0.483778753666885, 0.393712753506288, 0.427715184816051,
0.479775271348421, -0.28130811769396, 0.485295306906789, 0.29873238753337,
-0.32311305698219, -0.153688760742991, 0.566586154995381, 0.432231338528875,
1.06596225482086, 1.35634853919191, 3.80694326707882, 0.810475117293976,
3.0387608847462, 0.633718464176746, 1.1751890651466, 3.9845446592986,
-1.37297875557724, -1.09420268685371, -0.962614525736854, -1.29805138281804,
-1.31055976157738, -0.925719147858883, -1.23243978183704, -1.33945462492742,
-0.733417598434015, -0.930363022530595, -1.27774840900127, -0.551645520494324,
1.07748304807901, -0.396816101914801, -0.619403529151145, 0.904015824338447,
-0.287220128038401, -0.413048098445259, 0.612345039773056, 0.12520419272066,
0.144889119165393, -0.376872327860885, -0.371321461123647, 0.713414294431202,
-0.220319649249486, 1.91457944140036, 1.96765430981412, -0.121347747943895,
1.83594114293694, -0.874846076775421, 1.57970089137696, 1.93625153606136,
0.346364402583701, 1.77363591575721, 0.715776292044604, -0.11504600156397,
0.957194866002839, -0.887101387136546, -0.780049232064872, -0.336706490132965,
-0.70438883179631, -0.873817482659086, -0.284434200328209, -0.568305044660584,
-0.775993306095371, -0.771770658609636, -0.383838373137703, -0.811970593682299,
-0.73465457432939, 0.0911351344017551, 1.2707682140586, -0.777318831552788,
1.11134230637355, -0.783796841885501, 0.259370669933754, 1.73241147006831,
-0.725970777900951, -0.438118569726494, -0.278337133147783, -0.493758812846972,
-0.518179622331699, -0.389789488287258, -0.298381997017347, -0.612423478602631,
-0.429659970678406, -0.292999247051666, -0.435183828919258, -0.897633859213429,
0.507974413224703, 1.43201049921323, -0.66047577071763, 0.786340467145023,
-0.95656045144662, -0.132752976801546, 1.18466694854915), average_Zn = c(0.199618761426643,
0.591792310386536, 1.3751346661716, 0.332582639073901, 0.560402369414685,
0.11570820588524, 0.192150397195304, 1.39288706671957, -0.394083581015067,
-0.0798076017654593, 1.17669193020793, 0.607211261125105, -0.452359206037806,
0.771410079352111, 0.494544784665782, -0.625267178823271, -0.39226745167263,
1.16595745311139, 0.395578830413474, -1.34293822466577, -0.742961944779728,
-1.55476909975203, -1.21117698813619, -0.705958753206111, -1.37118754349972,
-0.917928244368175, -0.974434964905969, -1.01929394953207, -1.12532371386981,
-1.22700631035692, -1.46133511017614, -0.952449170432, -1.21027641358059,
-1.3226625940125, -1.16430485224588, -1.08770522025381, -1.18121144700268,
-1.40033431712296, -0.618587673725525, -0.91706750820117, 0.41088121270189,
-0.78430313474907, -0.409059258341318, -0.975516971079657, -1.27140585793008,
-0.198796133331597, -0.429336908924874, -0.0174222425394045,
-0.161004481467468, -0.405810552877399, -0.370285632509627, -0.89866639987887,
1.10645284167993, 1.8809570525936, 1.92254222169752, -0.0950224222539316,
1.58853988090432, 1.78386607934181, -0.817728723571643, 0.459412598781715,
1.423493404365, 1.7768774695448, -1.12006569075839, -0.751966514798299,
-0.488043380249217, -1.11596293893295, 0.273653921626916, -1.30713710349688,
-0.886375551803829, 0.0239199838971528, -1.3593629685693, -0.643843217883837,
-0.281272478019681, -1.30622537314287, -1.33112371265123, -0.909101703993645,
-1.12755493783614, -1.40003719660867, -0.290337728601771, -0.28987232858238,
-1.19056584307422, -0.193624949117038, 0.975332436429838, -0.218160046225909,
-0.132462974128803, 1.39468641578899, -0.302150834825655, -0.107400685186729,
1.125970810338, -0.256092735038377, 0.158506693687128, -0.378339394736207,
-0.349357413758689, 0.803679188551612, -0.146048595417096, -0.286845588476566,
0.158259418849209, -0.323113138916157, 0.108646496285904, -0.771878347558138,
0.0273033684358348, 0.0453235133932348, 1.28361673646552, 1.29073622845612,
1.24630790251793, 0.944986804400661, 0.981991016697687, 1.3473053430083,
0.827378725150942, 2.48208427225106, 2.69830294706435, 1.03039606923181,
2.27492560926846, 1.10744827135352, 1.35324680735362, 1.84388638700427,
1.52873550347441, 1.40071898822651, 0.260928795100606, -1.16734754082697,
-0.878145608518404, -0.00331768573206932, -0.910939824808822,
0.125144947309071, -1.67327192782442, -0.531813491778335, -0.546522958404239,
-0.0196439100156154, 0.0745476695275746, 0.794370508556172, -0.210218336601184,
0.445705743628815, 0.0718198426307307, -0.122444182892431, 0.184211311538213,
0.183194361394073, 1.12410771197491, -0.741883323524288, 0.716760255429898,
1.76025647136123, -0.00419702451761512, 1.25254232235371, -0.880576082155036,
-0.00114985682377106, 1.78721959263295), ID = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), .Label = c("BLA14",
"BLA2", "BLA20", "BLA209", "BLA21", "BLA211", "BLA238", "BLA24",
"BLA25", "BLA283", "BLA307", "BLA31", "BLA42", "BLA47", "BLA5",
"BLA79", "BLA80"), class = "factor"), Time = structure(c(2L,
5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L,
4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L,
3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L,
2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 11L, 3L, 6L, 8L, 1L, 4L, 7L,
10L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 5L, 8L, 11L, 2L, 3L, 6L,
9L, 4L, 7L, 10L, 1L, 2L, 5L, 3L, 1L, 4L, 2L, 5L, 8L, 3L, 6L,
1L, 4L, 7L, 2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L, 2L, 5L, 3L, 1L, 4L,
2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L, 2L, 5L, 8L, 3L,
6L, 1L, 4L, 7L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 1L, 4L, 7L, 10L,
2L, 5L, 8L, 3L, 6L, 1L, 4L, 7L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11"), class = c("ordered", "factor"
))), class = "data.frame", row.names = c(NA, -145L))
Current code for model
model2<- polr(Spawn_ID ~ average_Mg + average_Mn + average_Zn + (1| ID/Time), data = data1, method = "logistic", Hess = TRUE)
error message
Error in family$linkfun(mustart) :
Argument mu must be a nonempty numeric vector
In addition: Warning messages:
1: In eval(predvars, data, env) :
Incompatible methods ("Ops.factor", "Ops.ordered") for "/"
2: In Ops.factor(1, ID/Time) : ‘|’ not meaningful for factors

R:Error in `[.data.frame` undefined columns selected

I have this data sample
dput()
timeseries=structure(list(sales_point_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), calendar_id_operday = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L), line_fact_amt = c(55767L,
59913L, 36363L, 48558L, 505L, 76344L, 22533L, 11965L, 78944L,
36754L, 30621L, 55716L, 32470L, 62165L, 57986L, 2652L, 16487L,
72849L, 73715L, 65656L, 64411L, 47460L, 61866L, 10877L, 72392L,
53011L, 23544L, 76692L, 10388L, 24255L, 56684L, 59329L, 6655L,
65612L, 17495L, 10389L, 63702L, 47407L, 78782L, 22898L, 21151L,
32587L)), class = "data.frame", row.names = c(NA, -42L))
calendar_id_operday=1 its mean range of week 20210101-20210108(ymd) but here there is no date format only week, just such a specificity of these data . I try transform my data.
library(reshape)
df <- cast(melt(timeseries, id=c("calendar_id_operday"), na.rm=TRUE),
line_fact_amt + calendar_id_operday)[, c("line_fact_amt", "calendar_id_operday", substring(month.name, 1, 3))]
colnames(df)[1] <- "sales_point_id"
df[, substring(month.name, 1, 3)] <- lapply(timeseries[, substring(month.name, 1, 3)],
function(x) as.numeric(as.character(x)))
But something goes wrong
Error in `[.data.frame`(timeseries, , substring(month.name, 1, 3)) :
undefined columns selected
I want that as the result i got this data.frame
sales_point_id year jan-1 jan-2 jan-3 jan-4 feb1
1 1 2021 8034.843 7485.725 8238.493 8446.994 134
2 1 2021 7810.315 7261.198 8013.965 8222.466 346
3 1 2021 7585.788 7036.670 7789.438 7997.938 54364
4 1 2021 7361.260 6812.142 7564.910 7773.411 34546
5 1 2021 7136.733 6587.615 7340.382 7548.883 46436
jan-1 is data for firts week of jan. jan2- is the second week of jan and so on.
What should i do to get desired result?
Thanks for your valuable help

Facets: organising their order and organising the levels within facets

I would like to please organise the following plots so that facets are printed out from most to least busy (i.e. Hemiptera, Coleoptera, Hymenoptera, Siphonaptera, Lepidoptera, etc.)
I would also like to order the levels within each facet like in Coleoptera. I realise that the X-labels will change order too so I need each facet to print out its own X-label according the level order.
I have already read many threads and that's how I was able to organise Coleoptera. But now I want it to be more tidy.
This is the data (let me know if this format is ok, if not I can try another way):
structure(list(Order = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("Coleoptera",
"Dermaptera", "Dictyoptera", "Diptera", "Hemiptera", "Hymenoptera",
"Lepidoptera", "Phthiraptera", "Psocoptera", "Siphonaptera",
"Thysanoptera"), class = "factor"), Nrange = structure(c(1L,
3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 14L, 14L, 1L, 10L, 1L,
3L, 4L, 6L, 7L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L, 10L, 11L,
12L, 14L, 15L, NA, 1L, 2L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L,
13L, 14L, 4L, 10L, 11L, 12L, 14L, 1L, 4L, 10L, 11L, 12L, 13L,
14L, 1L, 5L, 10L, 1L, 4L, 6L, 7L, 10L, 11L, 12L, 14L), .Label = c("Africa",
"Africa, Asia", "Americas", "Asia", "Asia-Temp", "Asia-Trop",
"Australasia", "C&S America", "Cosmopolitan", "Cryptogenic",
"N America", "S America", "Trop", "Trop, SubTrop", "Unknown"), class = "factor"),
Records = c(16L, 1L, 9L, 7L, 11L, 17L, 1L, 15L, 8L, 8L, 5L,
1L, 2L, 1L, 5L, 1L, 1L, 1L, 1L, 9L, 9L, 2L, 1L, 4L, 11L,
10L, 30L, 15L, 9L, 2L, 2L, 2L, 34L, 11L, 21L, 1L, 21L, 16L,
8L, 1L, 14L, 3L, 5L, 25L, 4L, 2L, 1L, 1L, 8L, 1L, 10L, 1L,
2L, 1L, 1L, 8L, 5L, 2L, 1L, 2L, 2L, 9L, 1L, 2L, 1L, 3L, 1L,
12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L,
3L, 2L)), .Names = c("Order", "Nrange", "Records"), row.names = c(NA,
-83L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Order", drop = TRUE)
This is the reordering that I guess is affecting only Coleoptera.
xy<-x%>%
mutate(Nrange=reorder(Nrange,-Records,sum))
This is the plot:
to_plot<-xy %>%
filter(!is.na(Nrange))
ggplot(to_plot,aes(x=Nrange,y=Records,fill=Nrange))+
geom_col()+
theme(axis.text.x = element_text(angle=90, vjust=0.7), legend.position = "none") +
facet_wrap(~Order,ncol=3)+
labs(title="Insects recorded as alien-invasive to mainland Spain",
subtitle="Native ranges vs number of records",
caption="Data source: DAISIE (http://www.europe-aliens.org/)")
And this is the plot:
enter image description here
Assuming you're using the tidyverse (based on your code):
library(tidyverse)
xy <- x %>%
ungroup() %>%
mutate(
Order = fct_reorder(Order, Records, sum, .desc = TRUE)
)
xy %>%
filter(!is.na(Nrange)) %>%
ggplot() +
aes(x = Nrange, y = Records, fill = Nrange) +
geom_col() +
facet_wrap(~Order, ncol = 3)
fct_reorder comes from the forcats package, which I believe is now a part of the tidyverse.
Or, using base R, something like this:
xy <- x
record_sums <- tapply(xy$Records, xy$Order, sum)
levels(xy$Order) <- levels(xy$Order)[order(record_sums, decreasing = TRUE)]

How to read multiple line formula for systemfit in R

I am using systemfit to run system of equations using SUR method. I need to read long (multiple line) formula.My simple reproducible dataset can be accessed using following codes.
dat<-structure(list(Time = structure(c(9L, 7L, 15L, 1L, 17L, 13L,
11L, 3L, 23L, 21L, 19L, 5L, 10L, 8L, 16L, 2L, 18L, 14L, 12L,
4L, 24L, 22L, 20L, 6L), .Label = c("Apr-00", "Apr-01", "Aug-00",
"Aug-01", "Dec-00", "Dec-01", "Feb-00", "Feb-01", "Jan-00", "Jan-01",
"Jul-00", "Jul-01", "Jun-00", "Jun-01", "Mar-00", "Mar-01", "May-00",
"May-01", "Nov-00", "Nov-01", "Oct-00", "Oct-01", "Sep-00", "Sep-01"
), class = "factor"), ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("A", "B"), class = "factor"), y1 = c(25L,
14L, 45L, 15L, 24L, 17L, 18L, 19L, 14L, 15L, 25L, 14L, 45L, 15L,
24L, 17L, 18L, 19L, 14L, 15L, 25L, 14L, 45L, 15L), y2 = c(4L,
3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L, 4L, 3L, 4L, 5L, 1L,
4L, 5L, 3L, 6L, 4L, 2L, 5L), x1 = c(3L, 4L, 1L, 8L, 6L, 7L, 9L,
7L, 3L, 1L, 2L, 5L, 6L, 3L, 4L, 1L, 8L, 6L, 7L, 9L, 7L, 3L, 1L,
2L), x2 = c(4L, 3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L, 4L,
3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L), x3 = c(3L, 4L, 2L,
8L, 6L, 7L, 9L, 7L, 3L, 1L, 2L, 5L, 6L, 3L, 4L, 2L, 8L, 6L, 7L,
9L, 7L, 3L, 1L, 2L), x4 = c(4L, 3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L,
4L, 2L, 5L, 4L, 3L, 4L, 5L, 1L, 4L, 5L, 3L, 6L, 4L, 2L, 5L),
x5 = c(3L, 4L, 3L, 8L, 6L, 7L, 9L, 7L, 3L, 1L, 2L, 5L, 6L,
3L, 4L, 3L, 8L, 6L, 7L, 9L, 7L, 3L, 1L, 2L)), .Names = c("Time",
"ID", "y1", "y2", "x1", "x2", "x3", "x4", "x5"), class = "data.frame", row.names = c(NA,
-24L))
My example formula is like this,
model1<- y1 ~ x1 + x2 + x3
+ x4 +x5
eqSystem <- list(model1)
library(systemfit)
fit_prod_SUR <- systemfit(eqSystem, method= "SUR", data=dat)
print(fit_prod_SUR)
I have to include several very long formulas into eqSystem. But my problem is since my formula (e.g. model1) are very long it has got multiple lines. When I run the eqSystem with systemfit, it reads only the variables in the very first line of each formula. I tried with following code, but it does not work.
model1<- (get(paste("y1 ~ x1 + x2 + x3",
" + x4 + x5", sep="")))
But it does not take as a formula. Please could anyone help me to how to read all variables (in multiple lines) of formula in R.

Resources