Adding points only to selected levels of a boxplot - r

I have the following data frame:
> glimpse(pd)
Observations: 340
Variables: 4
$ gene_id <fctr> T03F1.6, T01B11.2, F40D4.13, F38B6.4, F10F2....
$ inter <fctr> K9me3, K9me3, K9me3, K9me3, K9me3, K9me3, K9...
$ genotype <fctr> 641, 641, 641, 641, 641, 641, 641, 641, 641,...
$ value <dbl> 0.88733425, -0.47734512, 0.16116906, -0.40425...
and plotting code:
p4 <- ggplot(pd) +
geom_point(aes(x=inter,
y=value,
col=inter, shape=genotype, alpha=genotype), size = 3) +
scale_color_manual(values = cc, name = ' ') +
scale_alpha_discrete(range=c(0.6,1)) +
myggplot.y0 +
theme(legend.position="bottom") +
myggplot.blankXtext +
facet_wrap(~gene_id, ncol=2) +
guides(color=guide_legend(title=''))
p4
I'd like to summarize the round shaped points by a boxplot (at each dodging level), but keep the triangle shaped points (which only appear in the last two 'levels') as separate 'points' overlayed on the boxplot.
Does anyone know how to do this?
Here is the data to reproduce the data.frame:
> dput(pd)
structure(list(gene_id = structure(c(8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L,
2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L,
6L, 4L, 3L, 1L, 5L, 2L, 10L, 9L, 7L, 8L, 6L, 4L, 3L, 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,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), .Label = c("K27me0",
"K27me1", "K27me2", "K27me3", "K36me0", "K36me1", "K36me2", "K36me3",
"K4me0", "K4me1", "K4me2", "K4me3", "K9me0", "K9me1", "K9me2",
"K9me3"), class = "factor"), genotype = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("N2", "641"), class = "factor"), value = c(0.887334249026434,
-0.47734511528238, 0.161169059129204, -0.40425962740708, -0.448772664830352,
-2.54043183911831, 0.147633437904588, -0.331999887972573, 0.168821284679471,
0.627281852456115, 0.0785458811781217, -0.635417116680447, 1.95039267129571,
-0.461389508271182, -0.332495133573183, -1.07629215381253, -0.715262085650234,
0.380580191285256, -0.384548303669202, -0.492327117463121, -0.0635811095948338,
-0.137169496659571, 0.869104975820394, -0.124274416739264, -0.00837899314070789,
-0.531344335277651, -0.526039923426319, 0.157981596945951, -0.0613955127076382,
-0.389835314108788, 2.69392711479386, 0.126789590130756, 0.467843320254945,
0.119956248487539, -0.932684036187982, 1.06163513421864, -1.15599025769053,
0.501998795474814, 0.547724207589878, 0.208660744829548, 2.39652396988934,
-0.113536659142323, 0.617602997781084, -0.0243661090708898, -0.931766154859557,
0.84708053705325, -0.302694166168958, 0.242295516669482, 0.362144368539548,
0.143166134234178, 2.10369156882084, 0.460380422671629, -0.0118030947996877,
-1.30190938551855, -0.944990285797225, 1.78280710295825, -0.634879160361434,
0.633411617712061, -0.323643001568312, -0.352447999186893, 0.27261554500361,
-0.184793596205417, -0.785654613201974, -0.380434850649155, -0.116175416888516,
0.391864521424546, 0.288960168023483, -0.0808022493276015, -0.624387637961664,
-0.652297254014572, 1.75147794216705, -0.439685382191905, 0.150488781634707,
-0.0336437864682964, -0.802955390112682, 1.06183416852385, -0.923263618312843,
0.468046260016009, 0.286851381130517, 0.17305984039931, 1.75158852119529,
-0.592564500094055, 0.248498177746989, -0.222638458602893, -0.720265703696414,
0.72750611824884, -0.336818721832683, 0.511031230869627, 0.0430587972307137,
0.0290598947165659, 0.886443959972121, -0.303364064568797, -0.277915654881935,
-0.462931588140646, -0.512912057394122, 0.130398107823421, -0.410559505055534,
-0.543074481188406, 0.223303098899384, -0.35597446101497, 0.584846557204577,
-0.328552338525071, 0.39166741782988, -0.419624587301293, -0.417042458658473,
-0.0500348090171157, 0.440292934053895, -0.0389439842100643,
-0.192935439273113, -0.195280031711113, -0.330623439443579, 0.406193213631403,
1.60465863614407, 0.488558950927488, 0.306422606850451, 0.0259842742803569,
0.464781609520434, 0.382521939171359, -0.697256999098308, 0.74469109948364,
0.0564480245933332, 0.451572695093123, 1.65543142016647, 0.471407535866445,
0.287994086588806, -0.589245829633368, 0.369581847988354, 0.597812561310243,
-0.607302119427235, 0.9696199804852, 0.153586315079381, -0.619187466367118,
0.656675901432987, -0.871392537978112, -0.311214917388622, -0.923682063590928,
-0.323604082561573, -0.693266286639908, 0.0290631195420703, -0.291795800840029,
-0.0916009854020086, -0.529683872226366, 0.341292466699543, -0.964841093192905,
-0.349467794977152, -0.502029626725142, -0.254774675707687, -1.20345690995063,
0.220507237168474, -0.954023439798044, 0.321770602015399, -0.726063502895515,
0.28288258484054, -1.55107383696037, -0.366037696773723, -0.587999636824452,
-1.35916629238201, -1.42792505502871, 1.05596729613068, -1.4153269555524,
-0.0959965268426171, -0.581692078188237, 0.0216148704828045,
-0.967560147159656, -0.406889554155961, -0.29013462836955, -0.272629044721009,
-1.74574575300093, 0.409694897073192, -0.945248012942512, 0.488667747235731,
-0.130769790765262, -0.245054023224077, -1.22641996252332, -0.42550379283728,
-0.330755471294788, -1.49812536880092, -0.687381177317297, 0.506910130730609,
-0.769620490644054, 0.0531265417354891, -0.665086454118599, 0.136193457296536,
-0.62216089856077, -0.367848687570944, -0.164342714173533, -0.382590305354972,
-0.874808163574018, 0.493411933475686, -0.37887729896755, -0.574238871100946,
0.127741068021188, 3.86039295680539, 0.406874973642696, 0.018180549517699,
-0.73164426806948, 0.10916295082713, 0.489171565128614, -0.63809359131097,
0.012317883603111, -0.182079038293229, 0.0227150325950261, 3.68710015302125,
0.455122847588301, 0.0235041737883517, -0.024393097564638, 0.41345288614306,
0.969811866964937, -0.672585115335576, 0.331895771644743, -0.285694918186591,
-0.711647452923045, 1.38434450822266, -0.523103532001491, -0.0893223046296763,
-1.01365895485872, 0.0657806542743673, -1.39534058124217, -0.923222605651799,
-1.39887741940789, -0.0952580333454192, -0.546574104718356, 1.46477855989728,
-0.460227934172416, -0.240045678851763, -0.0111436043830646,
0.0627986215438661, -0.251396685372123, -0.934093688281673, -1.05820277806975,
0.556102701675237, -0.506831613088146, 0.134996075420438, -0.304509562226102,
-0.530606844852739, -0.576258795363492, 0.558240796235196, 0.00141004667762168,
-0.219444435445798, -0.537347914917884, 0.923884368998904, -0.425823765099706,
0.402984847066973, -0.0138900208495514, -0.466549078530587, -0.0299175806841818,
-0.198688733963097, 0.419444078428615, -0.416005826002221, -0.0406133025559159,
-0.711562592297027, -0.00565350152856414, 4.54871285677085, 0.118779124267025,
0.0139761953422664, -0.664583716722071, -0.743919010180514, 0.706218216909635,
-0.683249460883233, 0.0605499462979795, -0.158429916510739, -0.222706230740269,
4.71119851589582, 0.25500719760156, -0.0752734072712284, -0.795844146522265,
0.178040897179368, 0.895173312761832, -1.03093515891618, -0.588056522862408,
-0.281825764719025, -0.056228940433579, -0.313824928780573, -0.0917469172753451,
-0.122912334871057, 0.712942661491444, -0.0416925666113199, -0.103626376180252,
-0.123906792244526, -0.338488229262714, -0.285202171459416, -0.012465219280215,
-0.140276363711298, -0.116007895520325, -0.111935190660002, 0.225067028104865,
0.435777323306682, -0.107111364797968, -0.0547435542980068, -0.245852927399465,
-0.646765570905925, -0.574523578850278, 1.23916851484567, -0.756916946637266,
-0.442286710512833, -0.653558388340836, -0.104749483441292, -0.0656026501027611,
-0.611101876494586, -1.24462228141138, -1.08065682436839, -0.11380843108228,
4.76147315049708, 0.0164152091429139, -0.0182056738105381, -0.385790482896067,
1.20308626895301, -0.241046175728772, 0.0412353010350772, -1.37886106242171,
-0.652945846253202, 0.0284072616942028, 0.780335178644036, -0.644783275438296,
-0.319045839781824, 1.57554487548949, -0.428706034693799, -0.350808314840347,
-0.24066856927377, -1.04530746723789, -0.240526163518468, -0.437546091131759,
3.36251366149142, -0.101287227327982, -0.149730531058178, -2.05718920626846,
0.316516294262123, 0.798192960479417, -1.0788450246926, -0.0806608802523838,
-0.670068360692003, -0.419448747866297, 6.36279926782848, -0.0378872162137345,
-0.692973015966929, -1.31677539848766, -0.576391545143803, 1.42209640226752,
-1.13065080356991, 0.26979963818752)), .Names = c("gene_id",
"inter", "genotype", "value"), class = "data.frame", row.names = c(NA,
-340L))

Use subsets of the data for the separate geoms:
ggplot(mapping = aes(x=inter, y=value)) +
geom_boxplot(data = subset(pd, genotype == "N2")) +
geom_point(data = subset(pd, genotype == "641"), aes(col=inter), size = 3) +
theme_bw() +
theme(legend.position="bottom",
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
facet_wrap(~gene_id, ncol=2) +
guides(color=guide_legend(title=''))
Note that a bunch of you plotting code isn't actually reproducible since it relies on objects such as myggplot.y0.

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)

Add a column and repeat every 16 dplyr

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)

Editing a Row Value in R

I have a data frame that looks like this
Pick Name Team Round Player Position Position..
1 1 Javi Texans 1 Patrick Mahomes QB QB1
2 2 Justin Chiefs 1 Russell Wilson QB QB2
3 3 Blake Titans 1 Lamar Jackson QB QB3
4 4 Connor Dolphins 1 Deshaun Watson QB QB4
5 5 Isaac Jaguars 1 Carson Wentz QB QB5
6 6 Fitz Rams 1 Dak Prescott QB QB6
with more rows of course and some of the rows in the Player, Position and Position... Column are empty because they haven't been drafted yet. Is there a way to just manually insert the names, pos, pos... of the newly drafted players.
I tried
Redraft[112, "Player"] <- "Calvin Ridley"; Redraft
Since the empty cells start on row 112, but it just came up as N/A
When I do that I also get an error message:
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "Calvin Ridley") :
invalid factor level, NA generated
and the data frame looks like
08 108 Jack Packers 4 TE3 Darren Waller TE
109 109 Justin Saints 4 LT6 Taylor Lewan LT
110 110 Sam Steelers 4 FS5 Kevin Byard FS
111 111 Jeremy Falcons 4 LB7 Isaiah Simmons LB
112 112 Will Bills 4 1 <NA>
113 113 Jeremy Colts 4 1
And heres the whole data frame:
structure(list(Pick = 1:384, Name = structure(c(12L, 14L, 1L,
2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L, 13L, 20L,
13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L, 4L, 18L,
15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L, 8L, 14L,
17L, 13L, 20L, 13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L, 6L, 8L, 5L,
7L, 2L, 1L, 14L, 12L, 12L, 14L, 1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L,
9L, 2L, 10L, 16L, 11L, 13L, 20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L,
5L, 19L, 7L, 1L, 6L, 4L, 18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L,
19L, 5L, 19L, 3L, 3L, 8L, 14L, 17L, 13L, 20L, 13L, 11L, 16L,
10L, 2L, 9L, 12L, 9L, 6L, 8L, 5L, 7L, 2L, 1L, 14L, 12L, 12L,
14L, 1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L,
13L, 20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L,
4L, 18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L,
8L, 14L, 17L, 13L, 20L, 13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L,
6L, 8L, 5L, 7L, 2L, 1L, 14L, 12L, 12L, 14L, 1L, 2L, 7L, 5L, 8L,
6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L, 13L, 20L, 13L, 17L, 14L,
8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L, 4L, 18L, 15L, 15L, 18L,
4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L, 8L, 14L, 17L, 13L, 20L,
13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L, 6L, 8L, 5L, 7L, 2L, 1L,
14L, 12L, 12L, 14L, 1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L,
10L, 16L, 11L, 13L, 20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L,
19L, 7L, 1L, 6L, 4L, 18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L,
5L, 19L, 3L, 3L, 8L, 14L, 17L, 13L, 20L, 13L, 11L, 16L, 10L,
2L, 9L, 12L, 9L, 6L, 8L, 5L, 7L, 2L, 1L, 14L, 12L, 12L, 14L,
1L, 2L, 7L, 5L, 8L, 6L, 9L, 12L, 9L, 2L, 10L, 16L, 11L, 13L,
20L, 13L, 17L, 14L, 8L, 3L, 3L, 19L, 5L, 19L, 7L, 1L, 6L, 4L,
18L, 15L, 15L, 18L, 4L, 6L, 1L, 7L, 19L, 5L, 19L, 3L, 3L, 8L,
14L, 17L, 13L, 20L, 13L, 11L, 16L, 10L, 2L, 9L, 12L, 9L, 6L,
8L, 5L, 7L, 2L, 1L, 14L, 12L), .Label = c("Blake", "Connor",
"Dakota", "FFB", "Fitz", "Haydon", "Isaac", "Jack", "Jackson",
"Jacob", "Jacob H", "Javi", "Jeremy", "Justin", "Nick", "Pete",
"Sam", "Simon", "Tucker", "Will"), class = "factor"), Team = structure(c(30L,
10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L,
11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L,
12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L,
25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L,
8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L, 30L, 30L, 10L, 31L,
13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L, 11L, 4L,
15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L, 12L, 16L,
22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L, 25L, 1L,
20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L, 8L, 3L, 6L,
18L, 24L, 17L, 13L, 31L, 10L, 30L, 30L, 10L, 31L, 13L, 17L, 24L,
18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L, 11L, 4L, 15L, 29L, 27L,
20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L, 12L, 16L, 22L, 2L, 2L,
22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L, 25L, 1L, 20L, 27L, 29L,
15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L, 8L, 3L, 6L, 18L, 24L, 17L,
13L, 31L, 10L, 30L, 30L, 10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L,
8L, 7L, 28L, 9L, 21L, 14L, 11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L,
5L, 23L, 26L, 32L, 19L, 12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L,
19L, 32L, 26L, 23L, 5L, 25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L,
14L, 21L, 9L, 28L, 7L, 8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L,
30L, 30L, 10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L,
9L, 21L, 14L, 11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L,
26L, 32L, 19L, 12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L,
26L, 23L, 5L, 25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L,
9L, 28L, 7L, 8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L, 30L, 30L,
10L, 31L, 13L, 17L, 24L, 18L, 6L, 3L, 8L, 7L, 28L, 9L, 21L, 14L,
11L, 4L, 15L, 29L, 27L, 20L, 1L, 25L, 5L, 23L, 26L, 32L, 19L,
12L, 16L, 22L, 2L, 2L, 22L, 16L, 12L, 19L, 32L, 26L, 23L, 5L,
25L, 1L, 20L, 27L, 29L, 15L, 4L, 11L, 14L, 21L, 9L, 28L, 7L,
8L, 3L, 6L, 18L, 24L, 17L, 13L, 31L, 10L, 30L), .Label = c("49ers",
"Bears", "Bengals", "Bills", "Broncos", "Browns", "Buccaneers",
"Cardinals", "Chargers", "Chiefs", "Colts", "Cowboys", "Dolphins",
"Eagles", "Falcons", "Giants", "Jaguars", "Jets", "Lions", "Packers",
"Panthers", "Patriots", "Raiders", "Rams", "Ravens", "Redskins",
"Saints", "Seahawks", "Steelers", "Texans", "Titans", "Vikings"
), class = "factor"), Round = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L),
Pos.. = structure(c(49L, 60L, 70L, 71L, 72L, 73L, 74L, 75L,
27L, 76L, 43L, 50L, 51L, 92L, 52L, 53L, 54L, 55L, 77L, 56L,
57L, 58L, 89L, 59L, 85L, 61L, 103L, 3L, 106L, 35L, 62L, 36L,
63L, 42L, 10L, 107L, 18L, 64L, 108L, 65L, 109L, 19L, 11L,
110L, 86L, 37L, 111L, 12L, 20L, 112L, 66L, 21L, 38L, 13L,
90L, 78L, 81L, 30L, 14L, 15L, 82L, 39L, 16L, 17L, 93L, 94L,
4L, 22L, 95L, 96L, 2L, 97L, 67L, 5L, 68L, 87L, 83L, 84L,
6L, 31L, 44L, 98L, 99L, 100L, 7L, 28L, 101L, 32L, 29L, 8L,
33L, 88L, 69L, 79L, 102L, 9L, 104L, 40L, 23L, 24L, 105L,
25L, 45L, 80L, 46L, 26L, 47L, 91L, 48L, 34L, 41L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1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"CB7", "CB8", "CB9", "DE1", "DE2", "DE3", "DE4", "DE5", "DE6",
"DE7", "DE8", "DE9", "DT1", "DT2", "DT3", "FS1", "FS2", "FS3",
"FS4", "FS5", "LB1", "LB2", "LB3", "LB4", "LB5", "LB6", "LB7",
"LG1", "LT1", "LT2", "LT3", "LT4", "LT5", "LT6", "QB1", "QB10",
"QB11", "QB12", "QB13", "QB14", "QB15", "QB16", "QB17", "QB18",
"QB19", "QB2", "QB20", "QB21", "QB22", "QB23", "QB24", "QB25",
"QB26", "QB27", "QB28", "QB3", "QB4", "QB5", "QB6", "QB7",
"QB8", "QB9", "RB1", "RB2", "RB3", "RB4", "RG1", "RT1", "RT2",
"RT3", "SS1", "SS2", "SS3", "SS4", "TE1", "TE2", "TE3", "WR1",
"WR10", "WR11", "WR12", "WR13", "WR14", "WR15", "WR16", "WR17",
"WR18", "WR19", "WR2", "WR20", "WR21", "WR3", "WR4", "WR5",
"WR6", "WR7", "WR8", "WR9"), class = "factor"), Player = structure(c(87L,
91L, 72L, 38L, 14L, 24L, 79L, 78L, 3L, 57L, 90L, 70L, 107L,
31L, 39L, 10L, 56L, 68L, 20L, 4L, 94L, 93L, 45L, 52L, 51L,
44L, 80L, 97L, 62L, 67L, 40L, 98L, 101L, 89L, 50L, 9L, 85L,
104L, 19L, 41L, 109L, 58L, 106L, 81L, 37L, 26L, 29L, 27L,
18L, 86L, 60L, 84L, 17L, 74L, 105L, 95L, 111L, 63L, 76L,
55L, 92L, 110L, 12L, 15L, 64L, 96L, 34L, 25L, 61L, 103L,
54L, 21L, 53L, 49L, 59L, 108L, 71L, 83L, 77L, 82L, 102L,
7L, 65L, 2L, 69L, 32L, 22L, 75L, 43L, 5L, 8L, 46L, 35L, 42L,
23L, 88L, 6L, 11L, 60L, 48L, 16L, 13L, 30L, 36L, 73L, 33L,
99L, 28L, 100L, 66L, 47L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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1L), .Label = c("", "A.J. Brown",
"Aaron Donald", "Aaron Rodgers", "Adoree' Jackson", "Allen Robinson",
"Amari Cooper", "Anthony Harris", "Antonio Brown", "Baker Mayfield",
"Bobby Wagner", "Byron Jones ", "Cameron Jordan", "Carson Wentz",
"Casey Hayward", "CeDee Lamb", "Chandler Jones", "Chase Young",
"Chris Godwin", "Christian McCaffrey", "Cooper Kupp", "Courtland Sutton",
"D.J. Moore", "Dak Prescott", "Danielle Hunter", "Darius Leonard",
"Darius Slay", "Darren Waller", "DaVante Adams", "David Bakhtiari",
"DeAndre Hopkins", "Deforest Buckner", "Demarcus Lawrence",
"Denzel Ward", "Derek Carr", "Derrick Henry", "Derwin James",
"Deshaun Watson", "Drew Brees", "Drew Lock", "Dwayne Haskins",
"Ezekiel Elliott", "Fletcher Cox", "Gardner Minshew", "George Kittle",
"Harrison Smith", "Isaiah Simmons", "J.J. Watt", "Jaire Alexander",
"Jalen Ramsey", "Jamal Adams", "Jared Goff", "Jarrett Stidham",
"Jason Kelce", "Jeffrey Okudah", "Jimmy Garappolo", "Joe Burrow",
"Joey Bosa", "Jordan Love", "Josh Allen", "Juju Smith-Schuster",
"Julio Jones", "Justin Simmons", "Keenan Allen", "Kenny Golladay",
"Kevin Byard", "Khalil Mack", "Kirk Cousins", "Kyle Fuller",
"Kyler Murray ", "La'el Collins", "Lamar Jackson ", "Laremy Tunsil",
"Marcus Peters", "Marcus Williams", "Marlon Humphrey", "Marshon Lattimore",
"Matt Ryan", "Matthew Stafford", "Michael Thomas", "Mike Evans",
"Minkah Fitzpatrick", "Mitchell Schwartz ", "Myles Garrett",
"Nick Bosa", "Odell Beckham Jr.", "Patrick Mahomes ", "Patrick Peterson",
"Quenton Nelson", "Ronnie Stanley", "Russell Wilson ", "Ryan Ramczyk",
"Ryan Tannehill", "Sam Darnold", "Saquon Barkley", "Stefon Diggs",
"Stephon Gilmore", "T.J. Watt", "Taylor Decker", "Taylor Lewan",
"Teddy Bridgewater", "Terron Armstead", "Terry McLaurin",
"Tom Brady", "Travis Kelce", "Tre White", "Tua Tagovailoa",
"Tyrann Mathieu", "Tyreek Hill", "Von Miller", "Zack Martin"
), class = "factor"), Position = structure(c(10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 5L, 10L, 9L, 10L, 10L, 16L, 10L,
10L, 10L, 10L, 11L, 10L, 10L, 10L, 15L, 10L, 14L, 10L, 16L,
3L, 16L, 7L, 10L, 7L, 10L, 8L, 3L, 16L, 4L, 10L, 16L, 10L,
16L, 4L, 3L, 16L, 14L, 7L, 16L, 3L, 4L, 16L, 10L, 4L, 7L,
3L, 15L, 11L, 12L, 6L, 3L, 3L, 13L, 7L, 3L, 3L, 16L, 16L,
3L, 4L, 16L, 16L, 2L, 16L, 10L, 3L, 10L, 14L, 13L, 13L, 3L,
6L, 9L, 16L, 16L, 16L, 3L, 5L, 16L, 6L, 5L, 3L, 6L, 14L,
10L, 11L, 16L, 3L, 16L, 7L, 4L, 4L, 16L, 4L, 9L, 11L, 9L,
4L, 9L, 15L, 9L, 6L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "C",
"CB", "DE", "DT", "FS", "LB", "LG", "LT", "QB", "RB", "RG",
"RT", "SS", "TE", "WR"), class = "factor")), row.names = c(NA,
384L), class = "data.frame")
You're dealing with a factor column. "Calvin Ridley" isn't yet a level of the factor. After adding it you can rename the cell.
class(Redraft$Player)
# [1] "factor"
levels(Redraft$Player) <- c(levels(Redraft$Player), "Calvin Ridley")
Redraft[112, "Player"] <- "Calvin Ridley"
Redraft[112, "Player"]
# [1] Calvin Ridley
# 112 Levels: A.J. Brown Aaron Donald Aaron Rodgers Adoree' Jackson Allen Robinson ... Calvin Ridley
jay.sf's answer is correct, of course, but I'd add my 2ยข since I think it's missing the point.
The reason you have factors instead of plain strings here in the first place, is kind of a historical accident with R being a statistical language. In practice, you rarely want to be dealing with factors in a dataframe of this kind. You probably want your player names to be plain-old strings.
Typically when you read a dataframe from a file, e.g. via read.csv, you have the option to pass the argument stringsAsFactors = TRUE, to ensure that strings are kept as strings rather than converted to factors. Some people (e.g. this guy) feel so strongly against this bizzare default behaviour, that they include a line in their .Rprofile to make importing data with stringsAsFactors=T as their default. (but this is dangerous for writing code that works the same across users with different .Rprofile initializations!)
If you already have the dataset, you can convert your factors to strings instead:
df[ , 'Player'] <- as.character( df[ , 'Player' ] )
You can now continue with your analysis without worrying about factors and their annoyances.
E.g. setting a new name is as simple as you'd expect:
df[112,'Player'] <- 'Calvin Ridley'

R diff lead difference between two columns?

Here is my dataframe:
structure(list(replicate = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L), press_id = c(1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), start_time = c(164429106370979,
164429411618825, 164429837271940, 164430399454285, 164429106370980,
164429411618826, 164429837271941, 164430399454286, 164429106370981,
164429411618827, 164429837271942, 164430399454287, 164429106370982,
164429411618828, 164429837271943, 164430399454288, 164429106370983,
164429411618829, 164429837271944, 164430399454289, 164429106370984,
164429411618830, 164429837271945, 164430399454290, 164429106370985,
164429411618831, 164429837271946, 164430399454291, 164429106370986,
164429411618832, 164429837271947, 164430399454292, 164429106370987,
164429411618833, 164429837271948, 164430399454293, 164429106370988,
164429411618834, 164429837271949, 164430399454294, 164429106370989,
164429411618835, 164429837271950, 164430399454295, 164429106370990,
164429411618836, 164429837271951, 164430399454296, 164429106370991,
164429411618837, 164429837271952, 164430399454297, 164429106370992,
164429411618838, 164429837271953, 164430399454298, 164429106370993,
164429411618839, 164429837271954, 164430399454299), end_time = c(164429182443825,
164429512525748, 164429903243170, 164430465927555, 164429182443826,
164429512525749, 164429903243171, 164430465927556, 164429182443827,
164429512525750, 164429903243172, 164430465927557, 164429182443828,
164429512525751, 164429903243173, 164430465927558, 164429182443829,
164429512525752, 164429903243174, 164430465927559, 164429182443830,
164429512525753, 164429903243175, 164430465927560, 164429182443831,
164429512525754, 164429903243176, 164430465927561, 164429182443832,
164429512525755, 164429903243177, 164430465927562, 164429182443833,
164429512525756, 164429903243178, 164430465927563, 164429182443834,
164429512525757, 164429903243179, 164430465927564, 164429182443835,
164429512525758, 164429903243180, 164430465927565, 164429182443836,
164429512525759, 164429903243181, 164430465927566, 164429182443837,
164429512525760, 164429903243182, 164430465927567, 164429182443838,
164429512525761, 164429903243183, 164430465927568, 164429182443839,
164429512525762, 164429903243184, 164430465927569)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -60L), vars = c("replicate",
"press_id"), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L), group_sizes = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
replicate = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L), press_id = c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L)), class = "data.frame", row.names = c(NA,
-60L), vars = c("replicate", "press_id"), drop = TRUE, indices = list(
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L,
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L,
50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L), group_sizes = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
replicate = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L), press_id = c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L)), class = "data.frame", row.names = c(NA,
-60L), vars = c("replicate", "press_id"), drop = TRUE, .Names = c("replicate",
"press_id")), .Names = c("replicate", "press_id")), .Names = c("replicate",
"press_id", "start_time", "end_time"))
I want to get the inter press_id time diff for example:
replicate press_id start_time end_time time_diff
1 1 1.644291e+14 1.644292e+14 0 (it's a first row)
1 2 1.644294e+14 1.644295e+14 1.644294e+14 - 1.644292e+14
1 3 1.644298e+14 1.644299e+14 1.644298e+14 - 1.644295e+14
1 4 1.644304e+14 1.644305e+14 .....
2 1 1.644291e+14 1.644292e+14
2 2 1.644294e+14 1.644295e+14
2 3 1.644298e+14 1.644299e+14
2 4 1.644304e+14 1.644305e+14
I am trying to do this using mutate, lag, lead and diff but without any luck. I have grouped, and ungrouped the dataset, nothing helped me.
df %>%
group_by(replicate) %>%
mutate(d = ifelse(row_number() == 1, 0, lead(start_time) - end_time))
df %>%
group_by(replicate) %>%
mutate(d = start_time - lag(end_time))
And if you want zeroes except NAs for the first row of each unique value in the replicate column, you could do:
df %>%
group_by(replicate) %>%
mutate(d = start_time - lag(end_time),
d = ifelse(is.na(d), 0, d))
Or just:
df %>%
group_by(replicate) %>%
mutate(d = ifelse(row_number() == 1, 0, start_time - lag(end_time)))

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

Resources