For loop and unexpected results - r

I'm trying to write a for loop but I couldn't finish it. If I ran them out of for loop it works well but I didn't understand where is the problem.
output100 <- structure(list(row = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), col = c(17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L), cell = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), xcoord = c(783750L,
783750L, 783750L, 783750L, 783750L, 783750L, 783750L, 783750L,
783750L, 783750L, 783725L, 783725L, 783725L, 783725L, 783725L,
783725L, 783725L, 783725L, 783725L, 783725L), ycoord = c(187050L,
187050L, 187050L, 187050L, 187050L, 187050L, 187050L, 187050L,
187050L, 187050L, 187025L, 187025L, 187025L, 187025L, 187025L,
187025L, 187025L, 187025L, 187025L, 187025L), species = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("abiealba", "alnuviri", "larideci", "piceabie",
"pinucemb", "pinusilv", "popunigr", "poputrem", "salicapr", "sorbaucu"
), class = "factor"), age = c(590L, 250L, 230L, 210L, 200L, 190L,
180L, 110L, 100L, 90L, 720L, 320L, 300L, 230L, 170L, 160L, 150L,
140L, 130L, 80L), biomass = c(6.3836, 1.2988, 0.9683, 0.6574,
0.5083, 0.3398, 0.2163, 0.0863, 0.0591, 0.0418, 6.6135, 1.7666,
1.214, 0.7032, 0.3422, 0.2571, 0.1601, 0.0846, 0.0592, 0.0323
), stems = c(1L, 1L, 3L, 1L, 2L, 6L, 5L, 8L, 3L, 5L, 1L, 3L,
1L, 1L, 2L, 5L, 7L, 4L, 6L, 5L), slowGrowth = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), DBH = c(104.9563, 50.7341, 44.7226, 37.9815, 34.1311, 28.9447,
24.1329, 16.8379, 14.5727, 12.7875, 106.7731, 58.0343, 49.2757,
39.0663, 29.027, 25.8599, 21.4205, 16.7129, 14.5803, 11.6105),
height = c(45.999, 30.659, 28.1508, 25.0823, 23.1987, 20.5,
17.8196, 13.4049, 11.9423, 10.7572, 46.3418, 33.4408, 30.0693,
25.5954, 20.5444, 18.802, 16.2293, 13.3254, 11.9472, 9.96
), availableLight = c(0.8129, 0.4994, 0.3701, 0.2541, 0.217,
0.1588, 0.102, 0.075, 0.06, 0.0545, 0.8083, 0.4101, 0.2332,
0.196, 0.1694, 0.1347, 0.0941, 0.0702, 0.0602, 0.0519), light_rf = c(0.9832,
0.8951, 0.8029, 0.6577, 0.592, 0.463, 0.2972, 0.2003, 0.1407,
0.1174, 0.9826, 0.8371, 0.6213, 0.5487, 0.4885, 0.3973, 0.2696,
0.181, 0.1409, 0.1056), LeafArea = c(5.9777, 5.9777, 5.9777,
5.9777, 5.9777, 5.9777, 5.9777, 5.9777, 5.9777, 5.9777, 6.218,
6.218, 6.218, 6.218, 6.218, 6.218, 6.218, 6.218, 6.218, 6.218
), nitorgen_rf = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0), droughtIndex = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), moisture_rf = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
degreeDay_rf = c(0.4405, 0.4405, 0.4405, 0.4405, 0.4405,
0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405,
0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405),
foliageWght = c(0.1471, 0.0473, 0.0389, 0.0301, 0.0255, 0.0197,
0.0149, 0.0085, 0.0068, 0.0055, 0.1511, 0.0584, 0.0452, 0.0315,
0.0198, 0.0165, 0.0123, 0.0084, 0.0068, 0.0047), twigWght = c(0.6236,
0.1251, 0.0929, 0.0627, 0.0483, 0.0455, 0.0674, 0.0488, 0.0376,
0.0286, 0.6462, 0.1708, 0.1169, 0.0672, 0.0448, 0.0639, 0.0655,
0.0482, 0.0376, 0.023), boleWght = c(5.6128, 1.1263, 0.8365,
0.5646, 0.4345, 0.2746, 0.134, 0.0291, 0.0148, 0.0077, 5.8161,
1.5374, 1.0519, 0.6045, 0.2776, 0.1766, 0.0823, 0.0281, 0.0149,
0.0045), deadFoliage = c(0.446, 0.446, 0.446, 0.446, 0.446,
0.446, 0.446, 0.446, 0.446, 0.446, 0.4278, 0.4278, 0.4278,
0.4278, 0.4278, 0.4278, 0.4278, 0.4278, 0.4278, 0.4278),
deadTwig = c(0.7874, 0.7874, 0.7874, 0.7874, 0.7874, 0.7874,
0.7874, 0.7874, 0.7874, 0.7874, 0.7322, 0.7322, 0.7322, 0.7322,
0.7322, 0.7322, 0.7322, 0.7322, 0.7322, 0.7322), deadbole = c(3.4762,
3.4762, 3.4762, 3.4762, 3.4762, 3.4762, 3.4762, 3.4762, 3.4762,
3.4762, 3.1449, 3.1449, 3.1449, 3.1449, 3.1449, 3.1449, 3.1449,
3.1449, 3.1449, 3.1449)), .Names = c("row", "col", "cell",
"xcoord", "ycoord", "species", "age", "biomass", "stems", "slowGrowth",
"DBH", "height", "availableLight", "light_rf", "LeafArea", "nitorgen_rf",
"droughtIndex", "moisture_rf", "degreeDay_rf", "foliageWght",
"twigWght", "boleWght", "deadFoliage", "deadTwig", "deadbole"
), row.names = c(NA, 20L), class = "data.frame")
Here is my code.
for (i in 0:1) {
t <- which(output100$cell == i)
a <-max(output100[c(t),8])
dom <- c(a, dom)
}
I want to get the maximum cell for the "t". Of course here it's just a small example (0:1), I have bigger dataset in real.

not sure what your problem is, your for loop runs for me. Did you forget to initialise dom?
dom = NULL
for (i in 0:1) {
t <- which(output100$cell == i)
a <-max(output100[c(t),8])
dom <- c(a, dom)
}
dom
## [1] 6.6135 6.3836
works for me? What answers are you expecting?
Incidentally this may not be the most efficient way to do this as you are growing the results vector in the for loop. If you had lots of unique cell values this would be slow. You could achieve similar by using dplyr which also would not require you to know how many different cell values there were:
library(dplyr)
output100 %>%
group_by(cell) %>%
summarise(max(biomass))
## # A tibble: 2 × 2
## cell `max(biomass)`
## <int> <dbl>
## 1 0 6.3836
## 2 1 6.6135

Related

Incorrect type of matrix

I'm running the MOVICS wrapper, which runs oncoPrint under the hood.
library(MOVICS)
mut.kirp.3 <- MOVICS::compMut(moic.res = iClusterBayes.res.3,
mut.matrix = as.matrix(cn), # binary somatic mutation matrix
doWord = TRUE, # generate table in .docx format
doPlot = TRUE, # draw OncoPrint
freq.cutoff = 0.05, # keep those genes that mutated in at least 5% of samples
p.adj.cutoff = 0.05, # keep those genes with adjusted p value < 0.05 to draw OncoPrint
innerclust = TRUE, # perform clustering within each subtype
annCol = annCol, # same annotation for heatmap
annColors = annColors, # same annotation color for heatmap
width = 6,
height = 2,
fig.name = "oncoprint_for_significant_mutations_3",
tab.name = "independent_test_between_subtype_and_mutation_3")
Traceback:
--all samples matched.
Error: Incorrect type of 'mat'
When I check the repo of OncoPrint, it mentioned "convert mat to mat_list". However, I still get the error even after coercing cn to matrix using as.matrix(cn).
Data:
cn
> dput(cn[1:10,1:10])
structure(list(TCGA.2K.A9WE.01A = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 0L), TCGA.2Z.A9J1.01A = c(1L, 1L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), TCGA.2Z.A9J3.01A = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), TCGA.2Z.A9J5.01A = c(0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L), TCGA.2Z.A9J7.01A = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), TCGA.2Z.A9J8.01A = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), TCGA.2Z.A9JD.01A = c(0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L), TCGA.2Z.A9JI.01A = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), TCGA.2Z.A9JJ.01A = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), TCGA.2Z.A9JO.01A = c(0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L)), row.names = c("DNMT3A", "IGF2R", "NBEA", "KMT2D",
"HERC2", "NEB", "TTN", "SF3B1", "DNAH5", "MDN1"), class = "data.frame")
iClusterBayes.res.3
> dput(iClusterBayes.res.3[1:2])
list(fit = list(alpha = list(c(-2.18345601746894, -2.92297963287402,
-1.52290116383105, -2.4710323329626, -2.18344242578026, -2.29201514343234,
-2.64808335017899, -2.87812071477877, -2.59406786525291, -2.56239622285371,
-2.75683161543007, -2.53820363471547, -2.71905324026935, -2.69893705428845,
-2.63983335581697, -2.71530534133162, -2.69925027535128, -3.09997972105142,
-2.68305518613159, -2.43321550985185, -2.69637296881716, -2.29089773727038,
-2.55849902866697, -2.57307908929987, -2.61818818995495, -2.07163972537099,
-2.34444215183964, -2.240232729226, -2.66182315740647, -2.54839234251289,
-2.37952570411958, -2.51429966330099, -2.51534041745121, -2.70529529402683,
-2.60773486408041, -2.95722445380988, -2.50501350738858, -2.81321984167971,
-2.22819464465135, -2.71154271563541, -2.66097963178881, -2.57046426043033,
-2.41168077072523, -2.49433238201652, -2.81666226181247, -2.75960597913093,
-2.73973106302299, -2.37468655323986, -2.44295675261412, -2.8202823433676,
-2.23672144950424), c(3610.99898073411, 2284.82323082175, 2181.03304991924,
845.01067607232, 955.510502793925), c(0.0399631699444046, 0.0522425180238726,
0.0307330025607688, 0.0444722333275495, 0.0536225842571486, 0.0233084676566811,
0.0380938568049614, 0.0376016109020193, 0.0372863386783329, 0.0287391633748148,
0.0387863594768836, 0.0353552478334894, 0.0537397281396524, 0.042085947093825,
0.0177214453792971, 0.0317487666917355, 0.042275710536733, 0.0666984681989372,
0.0254904179467174, 0.0625274018125971, 0.0477943663044489, 0.0334360347389645,
0.0569006743713939, 0.0367003207661641, 0.0153813591090115, 0.0623038961319309,
0.0273504788696269, 0.0348536725911234, 0.0215343857898484, 0.0416401384031003,
0.0209649403701854, 0.0602654360265637, 0.0323817708269443, 0.0182402488983038,
0.0466805716700717, 0.0437701103961682, 0.0507308717903958, 0.0613019949371114,
0.0616383050926993, 0.026382561418477), c(0.00903147195865062,
0.00586574443574702, 0.00980722968446402, 0.0212102066020019,
0.0131101335998724, -0.0171935698163193, 0.0131245375787987,
0.00519758452157967, 0.00120176164077142, 0.0134491140636551,
0.020634738911388, 0.00766021522695135, 0.000100150185762583,
0.0103875147209092, 0.0051266428679422, -0.0026981161599314,
0.0077236312864678, 0.00582785654031523, 0.00748316644252542,
-0.0107345186319592, -0.0100417233598139, 0.00357184348857985,
0.0181084054770186, 0.00275246111764039, 0.00864483583297387,
0.0112886573897725, 0.00295524360698826, 0.0047683061135053,
0.00702523823513744, 0.0106366726950608, 0.019008065028443, 0.0161908519699539,
0.0639925743507981, -0.00557518219004061, 0.0114588576557359,
0.0337145882301542, 0.00430009882064043)), beta = list(structure(c(-0.534683622110491,
0.169834662530963, -0.394736573173161, -0.736838485214897, 1.15585063175635,
-0.316798997138939, 0.596972852601736, -0.867412725228024, 0.642971281207242,
-0.0374631057853529, 0.0756351290836021, 0.401896206441697, -0.061229777231713,
0.474712233504059, 0.203246957899389, 0.188843850859799, 0.403836918070985,
-1.14233798508883, -0.479750454120197, 0.25778023010751, -0.209604733143481,
0.563210545145113, 0.574260964080719, 0.467418034788203, 1.48641622354926,
-0.483899426824387, -0.00209955658658113, 0.312645021884478,
-0.0452149031643873, 1.06014052563648, 0.526168298527776, 0.803935939887862,
0.594858256460905, -0.335122863909401, 0.45581029892202, 0.332114881112544,
0.245335294234405, 0.385574571674263, 0.254551477807301, 0.315665162518328,
0.0276469343566221, 0.311824201350491, 0.246772436430859, -0.0431942114563012,
0.412771452889935, 0.279830854826215, 0.00384512863134666, 0.953491990898459,
0.194367621949978, -0.124593233668976, 0.120843096654134, -0.316205930955046,
-0.281198823567983, -0.178748803167822, 0.948093776361363, 0.127366530632928,
0.213187238815027, 0.131734514246174, -0.381692258470129, -0.852581167326492,
-0.543248800810241, -0.567842207489419, -0.22209185176496, -0.502879741220382,
0.0255982150892638, 0.23048729317497, -0.775244780066522, -0.580700118934658,
-0.257888467581026, -0.410186553065455, -0.727075298813465, 0.313197034596598,
-1.40280270568278, -0.541995367117983, -0.0894840221754408, -0.23907079280482,
-0.0536499927294419, -0.182242507859354, -0.187611567553561,
-0.281819140042277, -0.60816348757828, -1.13121672954216, -0.253331469501323,
-0.249896319382511, -0.173937717676861, -0.49455000291008, -0.339637824887528,
-0.60840219794946, -0.101124629162083, -0.300845744542676, -0.102909682631551,
-0.288757399062554, -0.935126986257833, -0.598328312570878, 1.28488218461535,
0.153873803942656, 0.0936869542964011, -0.103735539838223, -0.64199112011241,
-0.214642182425156, -0.0284746539840866, 1.0452564519842), .Dim = c(51L,
2L)), structure(c(-4941.60860360474, -1283.79156227777, -448.037971357535,
-258.444883860848, 16.7876599987221, 4435.95910158312, 2562.98870218302,
317.64489122838, 466.423375326826, 497.8372774654), .Dim = c(5L,
2L)), structure(c(-0.022765468249156, 0.126239714291302, 0.0525177462807316,
-0.0222334847825758, 0.0329362386291049, 0.0468649473050463,
0.00656364869316663, 0.00593804632173995, -0.0656130819487922,
-0.0336974575585796, -0.0156578171738786, -0.0315802461329241,
0.0430553167874821, 0.0258445363079873, -0.0742140228663887,
0.00655798059394588, 0.0343443690248313, 0.0208713630643261,
0.0242565693675012, 0.0177142351831581, -0.0717406533086423,
-0.00237606533455619, -0.0508032783413915, -0.00026265182562006,
0.0629408519927583, 0.0449176811622647, 0.0039285472693163, 0.0167442839808041,
-0.0361811239993663, 0.0643516364328004, -0.0284986538239614,
0.00183418446392669, 0.0298731894919278, -0.0403830074692247,
0.0512476007388372, -0.0427801074495486, 0.0871505831147494,
0.0660139120548555, 0.0198810327959057, -0.0163258509984147,
-0.0647292390248117, -0.106314366025982, 0.021144534932677, -0.00686864237410266,
0.133267766687993, -0.0231082674706835, -0.0399221478587697,
-0.0493038263996928, 0.00403483859562252, -0.00583514468753377,
0.0385956059494588, 0.0445362352065695, -0.0666422283359932,
0.0507487573822867, 0.0362770992125005, 0.028543951578269, -0.0274780430982683,
-0.0989012228883707, 0.0384785055899797, -0.0204389092090038,
0.0380408292876272, 0.00985900058966174, 0.00862611022434112,
-0.0401529553195138, -0.0422256597366415, -0.0125984497338687,
0.00984621236882537, 0.100595575663213, -0.0384731397534353,
0.0311777767664603, -0.0365984367004682, 0.00951665036678657,
-0.101312029360852, 0.0320385269688485, -0.0625854040655537,
-0.0886685142066525, -0.0449257744196068, -0.0186711219808167,
0.0206051463323009, 0.0208382955702922), .Dim = c(40L, 2L)),
structure(c(0.0266655278284896, -0.0108193720999694, 0.0523078348864536,
-0.0383830745627788, 0.017468957658332, -0.0243234683206489,
0.0103740201140409, -0.0302366866109403, -0.000779580396192751,
-0.0611599164487188, -0.0127975469812426, 0.131931401762197,
-0.0770184044615842, 0.0483672670548741, 0.04580401320921,
0.0762638897120695, 0.0432320392755619, -0.0301196183100641,
0.0193928823210318, -0.0028456972006877, 0.0955346852038122,
-0.0524477378795355, 0.0222225306232621, -0.00251392088266702,
0.0496529815530794, 0.013616351190375, -0.0995071933817372,
-0.0355472628973064, 0.0660479434828083, -0.0237923838257249,
0.0243383683274929, 0.00592973287666157, 0.00462027287558529,
0.0535877569277535, -0.0344470896823196, 0.0139346813762236,
-0.0365772453067367, 0.0569270908180773, -0.0207289911497573,
-0.05627383346955, 0.0607425230620258, 0.00287832252477577,
0.189994869849123, -0.00144154149218259, 0.0146191755903187,
0.102538735904607, -0.00686711944035888, -0.0459206203270761,
-0.0128214978892839, -0.022315430552142, -0.116717658685067,
0.0366787366164101, 0.0474049062786799, -0.0493673961936388,
0.072761275738234, 0.00663422534034371, 0.00398894817576174,
-0.0262727790981866, -0.042089015380806, -0.0869204125668451,
-0.0551465173435108, -0.0508247723086034, -0.0554999574792746,
-0.0388904362908972, -0.0573175104181961, -0.0089998237619551,
0.049011457391633, -0.0240797536454846, 0.0397854259516219,
0.00202739769778886, 0.024025504936303, 0.0231782534953956,
-0.0294153051396557, 0.0552002083177096), .Dim = c(37L, 2L
))), beta.pp = list(c(0.7475, 0.215, 0.190833333333333, 0.984166666666667,
0.0358333333333333, 0.200833333333333, 0.4325, 0.0591666666666667,
0.620833333333333, 0.5075, 0.426666666666667, 0.334166666666667,
0.346666666666667, 0.455833333333333, 0.23, 0.418333333333333,
0.5025, 0, 0.736666666666667, 0.283333333333333, 0.471666666666667,
0.943333333333333, 0.389166666666667, 0.3575, 0.0908333333333333,
0.24, 0.238333333333333, 0.373333333333333, 0.359166666666667,
0.230833333333333, 0.3725, 0.296666666666667, 0.334166666666667,
0.161666666666667, 0.236666666666667, 0.4, 0.145, 0.284166666666667,
0.360833333333333, 0.259166666666667, 0.239166666666667, 0.729166666666667,
0.479166666666667, 0.000833333333333333, 0.3825, 0.213333333333333,
0.179166666666667, 0.336666666666667, 0.299166666666667, 0.199166666666667,
0), c(1, 1, 1, 1, 1), c(0.045, 0.0766666666666667, 0.0525, 0.065,
0.055, 0.0375, 0.0325, 0.04, 0.0416666666666667, 0.0716666666666667,
0.0408333333333333, 0.0483333333333333, 0.0891666666666667, 0.055,
0.0358333333333333, 0.0283333333333333, 0.085, 0.07, 0.0425,
0.0633333333333333, 0.0525, 0.0516666666666667, 0.0575, 0.0816666666666667,
0.0483333333333333, 0.06, 0.0625, 0.0433333333333333, 0.05, 0.04,
0.0441666666666667, 0.0425, 0.0408333333333333, 0.0333333333333333,
0.0883333333333333, 0.0466666666666667, 0.075, 0.0458333333333333,
0.0708333333333333, 0.04), c(0.05, 0.00416666666666667, 0.0366666666666667,
0.0691666666666667, 0.035, 0.374166666666667, 0.0858333333333333,
0.01, 0.0375, 0.0566666666666667, 0.03, 0.0525, 0.0458333333333333,
0.0566666666666667, 0.0391666666666667, 0.0341666666666667, 0.0791666666666667,
0.0908333333333333, 0.0225, 0.125833333333333, 0.0358333333333333,
0.0483333333333333, 0.0491666666666667, 0.0191666666666667, 0.03,
0.0366666666666667, 0.0425, 0.0575, 0.0533333333333333, 0.0683333333333333,
0.0441666666666667, 0.0483333333333333, 0.0975, 0.0425, 0.154166666666667,
0.0475, 0.1025)), gamma.ar = list(c(0.3875, 0.393333333333333,
0.348333333333333, 0.0316666666666667, 0.0716666666666667, 0.325,
0.525833333333333, 0.118333333333333, 0.538333333333333, 0.580833333333333,
0.646666666666667, 0.5975, 0.55, 0.628333333333333, 0.441666666666667,
0.650833333333333, 0.583333333333333, 0, 0.381666666666667, 0.498333333333333,
0.405833333333333, 0.0883333333333333, 0.539166666666667, 0.6075,
0.13, 0.319166666666667, 0.446666666666667, 0.623333333333333,
0.565, 0.435, 0.4875, 0.480833333333333, 0.5575, 0.293333333333333,
0.386666666666667, 0.616666666666667, 0.275, 0.4975, 0.25, 0.378333333333333,
0.426666666666667, 0.356666666666667, 0.621666666666667, 0.00166666666666667,
0.556666666666667, 0.41, 0.345, 0.475, 0.573333333333333, 0.394166666666667,
0), c(1, 1, 1, 1, 1), c(0.045, 0.0766666666666667, 0.0525, 0.065,
0.055, 0.0375, 0.0325, 0.04, 0.0416666666666667, 0.0716666666666667,
0.0408333333333333, 0.0483333333333333, 0.0891666666666667, 0.055,
0.0358333333333333, 0.0283333333333333, 0.085, 0.07, 0.0425,
0.0633333333333333, 0.0525, 0.0516666666666667, 0.0575, 0.0816666666666667,
0.0483333333333333, 0.06, 0.0625, 0.0433333333333333, 0.05, 0.04,
0.0441666666666667, 0.0425, 0.0408333333333333, 0.0333333333333333,
0.0883333333333333, 0.0466666666666667, 0.075, 0.0458333333333333,
0.0708333333333333, 0.04), c(0.05, 0.00416666666666667, 0.0366666666666667,
0.0691666666666667, 0.035, 0.374166666666667, 0.0858333333333333,
0.01, 0.0375, 0.0566666666666667, 0.03, 0.0525, 0.0458333333333333,
0.0566666666666667, 0.0391666666666667, 0.0341666666666667, 0.0791666666666667,
0.0908333333333333, 0.0225, 0.125833333333333, 0.0358333333333333,
0.0483333333333333, 0.0491666666666667, 0.0191666666666667, 0.03,
0.0366666666666667, 0.0425, 0.0575, 0.0533333333333333, 0.0683333333333333,
0.0441666666666667, 0.0483333333333333, 0.0975, 0.0425, 0.154166666666667,
0.0475, 0.1025)), beta.ar = list(c(0.786666666666667, 0.368333333333333,
0.318333333333333, 0.7825, 0.0641666666666667, 0.318333333333333,
0.635, 0.106666666666667, 0.778333333333333, 0.713333333333333,
0.675833333333333, 0.575, 0.568333333333333, 0.680833333333333,
0.4025, 0.683333333333333, 0.713333333333333, 0, 0.790833333333333,
0.469166666666667, 0.588333333333333, 0.7975, 0.6025, 0.5975,
0.1375, 0.343333333333333, 0.416666666666667, 0.6175, 0.585,
0.410833333333333, 0.546666666666667, 0.483333333333333, 0.554166666666667,
0.280833333333333, 0.385833333333333, 0.6425, 0.254166666666667,
0.484166666666667, 0.416666666666667, 0.411666666666667, 0.405,
0.783333333333333, 0.709166666666667, 0.000833333333333333, 0.5875,
0.379166666666667, 0.315833333333333, 0.520833333333333, 0.53,
0.360833333333333, 0), c(1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1)), Z.ar = c(0.158333333333333, 0.0908333333333333,
0.0466666666666667, 0.18, 0.0466666666666667, 0.176666666666667,
0.138333333333333, 0.186666666666667, 0.100833333333333, 0.140833333333333,
0.150833333333333, 0.175, 0.0941666666666667, 0.113333333333333,
0.0525, 0.128333333333333, 0.1, 0.0933333333333333, 0.2175, 0.0825,
0.100833333333333, 0.175, 0.1, 0.225833333333333, 0.1175, 0.0808333333333333,
0.0683333333333333, 0.0791666666666667, 0.123333333333333, 0.2675,
0.283333333333333, 0.208333333333333, 0.164166666666667, 0.181666666666667,
0.198333333333333, 0.09, 0.0491666666666667, 0.11, 0.256666666666667,
0.0691666666666667, 0.0975, 0.0458333333333333, 0.155833333333333,
0.191666666666667, 0.110833333333333, 0.0783333333333333, 0.075,
0.151666666666667, 0.188333333333333, 0.185833333333333, 0.276666666666667,
0.106666666666667, 0.0991666666666667, 0.0816666666666667, 0.0933333333333333,
0.0516666666666667, 0.131666666666667, 0.166666666666667, 0.175,
0.173333333333333, 0.0658333333333333, 0.2175, 0.22, 0.140833333333333,
0.145, 0.178333333333333, 0.0866666666666667, 0.248333333333333,
0.12, 0.0616666666666667, 0.0533333333333333, 0.266666666666667,
0.08, 0.0841666666666667, 0.146666666666667, 0.12, 0.0975, 0.165,
0.3225, 0.315, 0.285833333333333), clusters = c(3L, 2L, 3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 2L, 1L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L,
2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 3L, 3L), centers = structure(c(-5.50886138842214,
-0.220886746915253, 0.882913307448672, -0.875122782460201, -0.479023125604422,
1.38749939014862), .Dim = 3:2, .Dimnames = list(c("1", "2", "3"
), NULL)), meanZ = structure(c(0.494181443653601, 0.438050348517208,
0.660569789589561, 0.508219913528236, 0.60606520265243, 0.160085737819968,
0.173953915860567, -0.819178664251336, -2.0806524807611, 0.541077319257875,
0.0365834562216291, 0.648513332561144, -0.684235085424069, 1.43566743683047,
-0.261167096755661, -0.285217336889319, -0.360852563021473, -0.539198063566911,
-0.262156009869876, -0.590517212728142, 0.057026428501238, -1.41739893420573,
-0.342493280740047, -5.50886138842214, -0.528886740941811, 0.73274216184247,
-0.234178723262989, -0.171937345674686, -0.357747030391204, 1.55251055782134,
-0.236948410844573, 0.896092485022859, 0.975621490178399, 0.951444849183857,
0.670856045599832, 1.09136761022807, 0.361847120114293, 0.2126478791438,
0.412435667117699, -0.970566968295807, -0.392513003890142, 0.967002773596491,
0.203195761164649, -0.788828998801107, 0.559199909905801, -1.34443956964321,
-0.202571501756023, -0.303335503505311, 1.04700126731225, 0.181317912654619,
-0.0697302655229523, -0.140043937883014, 0.0960034244576039,
0.654614672177901, 0.150138663956637, 0.0408894074390708, 0.671218551798681,
0.63664028637526, 0.645910375621099, -0.167118831883868, -0.657965969114808,
-0.147281545851043, 1.11385103971091, -1.25489855390633, -0.794449043140217,
-0.616253118882751, 0.168657900341219, -1.09581744203958, -0.0760210874399024,
-0.217562488825655, -0.453753270386682, 0.481546505812506, -0.251550495592596,
-0.500619392203117, 0.448637774172844, -0.721769318761974, -0.496575628634869,
1.43028326049526, -1.06864809648222, 1.84814091465517, 2.45212980729972,
0.463599651401371, -0.0240815559777358, 0.876802323368068, 0.0494744704931259,
0.141249591625423, -0.289894544862299, -0.470046616686636, 0.663922041202651,
-0.668921136007252, -0.052153522346312, -0.265959981346212, 1.53345230412858,
-1.27731888681497, 1.05614809474987, -0.934398384059854, -0.764217293724456,
-0.913582703994721, -1.13593892096714, -0.801173978547484, -0.222422181355782,
-0.422926221550532, 2.59515294487812, -0.712603600711804, -0.875122782460201,
-0.895798781681253, 0.691828416326667, -0.728484838149463, -0.866830438953423,
0.507315225717686, 2.42971822364247, -0.349588696933559, 0.983987830039285,
0.645387830084804, 0.731749508135172, 0.0994795508454144, 1.48056473228741,
-0.0436210652189921, 0.123074301089618, 0.202116996656446, -0.886494336482045,
-0.659918632864047, 0.692693739891549, -0.0523124448240981, -1.15830816790105,
-0.0952643051969345, -0.622225700016697, -0.92056149105158, -0.950920477367661,
0.877154708597068, -0.273881400259781, 2.57669108688605, -0.700248640640389,
-0.544481773361721, 0.148721063301554, -0.422595984423306, -0.370921120014286,
0.0948223500819084, 2.04378760019609, 1.27360005264687, -0.703338807284177,
-1.16306469928925, -0.770888788613565, 1.40213631793113, -0.552850466836924,
-1.13235227734841, -0.831837696244257, -0.353483649794922, 0.672795146157629,
-0.706837402697785, -0.881977732694554, -0.969247532876248, 0.095628318578326,
-0.702685710867547, -1.15677553863763, 0.678698588005371, -1.0998513264007,
-0.843477375673034, 1.25885675405847, 0.305803363141811, 2.11171714208335,
2.73375934378334), .Dim = c(81L, 2L)), BIC = 39061533854982.4,
dev.ratio = 0.868502952470997), clust.res = structure(list(
samID = c("TCGA.2K.A9WE.01A", "TCGA.2Z.A9J1.01A", "TCGA.2Z.A9J3.01A",
"TCGA.2Z.A9J5.01A", "TCGA.2Z.A9J7.01A", "TCGA.2Z.A9J8.01A",
"TCGA.2Z.A9JD.01A", "TCGA.2Z.A9JI.01A", "TCGA.2Z.A9JJ.01A",
"TCGA.2Z.A9JO.01A", "TCGA.2Z.A9JQ.01A", "TCGA.4A.A93W.01A",
"TCGA.4A.A93X.01A", "TCGA.4A.A93Y.01A", "TCGA.5P.A9JU.01A",
"TCGA.5P.A9JY.01A", "TCGA.5P.A9KE.01A", "TCGA.A4.7288.01A",
"TCGA.A4.7583.01A", "TCGA.A4.7584.01A", "TCGA.A4.7585.01A",
"TCGA.A4.7734.01A", "TCGA.A4.7996.01A", "TCGA.A4.7997.01A",
"TCGA.A4.8098.01A", "TCGA.A4.8311.01A", "TCGA.A4.8517.01A",
"TCGA.A4.A57E.01A", "TCGA.AL.7173.01A", "TCGA.AL.A5DJ.01A",
"TCGA.B1.5398.01A", "TCGA.B1.7332.01A", "TCGA.B1.A47M.01A",
"TCGA.B1.A47N.01A", "TCGA.B1.A47O.01A", "TCGA.B1.A654.01A",
"TCGA.B1.A657.01A", "TCGA.B3.8121.01A", "TCGA.B3.A6W5.01A",
"TCGA.B9.5156.01A", "TCGA.B9.7268.01A", "TCGA.B9.A5W7.01A",
"TCGA.B9.A5W8.01A", "TCGA.B9.A5W9.01A", "TCGA.B9.A69E.01A",
"TCGA.B9.A8YH.01A", "TCGA.B9.A8YI.01A", "TCGA.BQ.5875.01A",
"TCGA.BQ.5877.01A", "TCGA.BQ.5878.01A", "TCGA.BQ.5879.01A",
"TCGA.BQ.5880.01A", "TCGA.BQ.5881.01A", "TCGA.BQ.5882.01A",
"TCGA.BQ.5883.01A", "TCGA.BQ.5885.01A", "TCGA.BQ.5887.01A",
"TCGA.BQ.5888.01A", "TCGA.BQ.5889.01A", "TCGA.BQ.5890.01A",
"TCGA.BQ.5891.01A", "TCGA.BQ.5892.01A", "TCGA.BQ.5893.01A",
"TCGA.BQ.5894.01A", "TCGA.BQ.7044.01A", "TCGA.BQ.7046.01A",
"TCGA.BQ.7048.01A", "TCGA.BQ.7049.01A", "TCGA.BQ.7050.01A",
"TCGA.BQ.7051.01A", "TCGA.BQ.7053.01A", "TCGA.BQ.7055.01A",
"TCGA.BQ.7056.01A", "TCGA.BQ.7058.01A", "TCGA.BQ.7059.01A",
"TCGA.BQ.7060.01A", "TCGA.BQ.7061.01A", "TCGA.BQ.7062.01A",
"TCGA.DW.5560.01A", "TCGA.DW.5561.01A", "TCGA.DW.7834.01A"
), clust = c(3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 1L, 2L, 3L,
2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L,
3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 3L, 3L)), class = "data.frame", row.names = c("TCGA.2K.A9WE.01A",
"TCGA.2Z.A9J1.01A", "TCGA.2Z.A9J3.01A", "TCGA.2Z.A9J5.01A", "TCGA.2Z.A9J7.01A",
"TCGA.2Z.A9J8.01A", "TCGA.2Z.A9JD.01A", "TCGA.2Z.A9JI.01A", "TCGA.2Z.A9JJ.01A",
"TCGA.2Z.A9JO.01A", "TCGA.2Z.A9JQ.01A", "TCGA.4A.A93W.01A", "TCGA.4A.A93X.01A",
"TCGA.4A.A93Y.01A", "TCGA.5P.A9JU.01A", "TCGA.5P.A9JY.01A", "TCGA.5P.A9KE.01A",
"TCGA.A4.7288.01A", "TCGA.A4.7583.01A", "TCGA.A4.7584.01A", "TCGA.A4.7585.01A",
"TCGA.A4.7734.01A", "TCGA.A4.7996.01A", "TCGA.A4.7997.01A", "TCGA.A4.8098.01A",
"TCGA.A4.8311.01A", "TCGA.A4.8517.01A", "TCGA.A4.A57E.01A", "TCGA.AL.7173.01A",
"TCGA.AL.A5DJ.01A", "TCGA.B1.5398.01A", "TCGA.B1.7332.01A", "TCGA.B1.A47M.01A",
"TCGA.B1.A47N.01A", "TCGA.B1.A47O.01A", "TCGA.B1.A654.01A", "TCGA.B1.A657.01A",
"TCGA.B3.8121.01A", "TCGA.B3.A6W5.01A", "TCGA.B9.5156.01A", "TCGA.B9.7268.01A",
"TCGA.B9.A5W7.01A", "TCGA.B9.A5W8.01A", "TCGA.B9.A5W9.01A", "TCGA.B9.A69E.01A",
"TCGA.B9.A8YH.01A", "TCGA.B9.A8YI.01A", "TCGA.BQ.5875.01A", "TCGA.BQ.5877.01A",
"TCGA.BQ.5878.01A", "TCGA.BQ.5879.01A", "TCGA.BQ.5880.01A", "TCGA.BQ.5881.01A",
"TCGA.BQ.5882.01A", "TCGA.BQ.5883.01A", "TCGA.BQ.5885.01A", "TCGA.BQ.5887.01A",
"TCGA.BQ.5888.01A", "TCGA.BQ.5889.01A", "TCGA.BQ.5890.01A", "TCGA.BQ.5891.01A",
"TCGA.BQ.5892.01A", "TCGA.BQ.5893.01A", "TCGA.BQ.5894.01A", "TCGA.BQ.7044.01A",
"TCGA.BQ.7046.01A", "TCGA.BQ.7048.01A", "TCGA.BQ.7049.01A", "TCGA.BQ.7050.01A",
"TCGA.BQ.7051.01A", "TCGA.BQ.7053.01A", "TCGA.BQ.7055.01A", "TCGA.BQ.7056.01A",
"TCGA.BQ.7058.01A", "TCGA.BQ.7059.01A", "TCGA.BQ.7060.01A", "TCGA.BQ.7061.01A",
"TCGA.BQ.7062.01A", "TCGA.DW.5560.01A", "TCGA.DW.5561.01A", "TCGA.DW.7834.01A"
)))

I am trying to fit an exponential function to my graph in R

In R, I have a large dataframe of 1000 simulations with an exponential distribution.
When I use gg_plot I get a graph which looks like this:
I am trying to estimate the values for the exponential function for this graph and then plot a line using those values.
I am quite new to stack-overflow. Returning to the graph, I have first converted the y and x values (q and t respectively) to logarithmic form and performed a linear regression. That looks like this:
surscript$logq<-log(surscript$q)
surscript$logt<-log(surscript$t)
linearmod<-lm(surscript$logq~surscript$logt)
That gave me a y intercept at -14.273, and a growth rate of 1.717.
To convert that back to exponential I performed a exponential function on these values.
expmodint<-exp(-14.273)
expmodgrowth<-exp(1.717)
I then used the exponential equation, y=a*e^t, to describe a q value:
temp<-expmodint*(expmodgrowth^surscript$t)
I then created a dataframe containing this exponential equation and the t from the original dataframe:
temp1<-data.frame(temp,surscript$t)
I then tried to add this line to my existing graph as seen above by using geom_line:
p+geom_line(temp1,aes(x=temp,y=surscript$t))
However I am returning the error "mapping must be created by "aes"".
Could anyone advice on why this error is occurring, and possibly a better way of calculating and fitting the exponential curve?
EDIT: Here I include the dataframe for two simulations, therefore, you will be able to work with some data.
structure(list(sim = structure(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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("simulation 1",
"simulation 2"), class = "factor"), m = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), q = c(0.001,
0.001, 0.002, 0.002, 0.005, 0.005, 0.006, 0.007, 0.011, 0.016,
0.02, 0.026, 0.033, 0.035, 0.041, 0.045, 0.05, 0.052, 0.054,
0.058, 0.064, 0.066, 0.071, 0.075, 0.08, 0.087, 0.097, 0.111,
0.124, 0.132, 0.143, 0.154, 0.162, 0.176, 0.188, 0.205, 0.216,
0.225, 0.231, 0.241, 0.252, 0.269, 0.287, 0.301, 0.32, 0.345,
0.358, 0.383, 0.406, 0.429, 0.454, 0.479, 0.493, 0.521, 0.541,
0.565, 0.587, 0.606, 0.635, 0.654, 0.677, 0.694, 0.712, 0.729,
0.744, 0.766, 0.783, 0.805, 0.815, 0.824, 0.835, 0.849, 0.859,
0.866, 0.881, 0.888, 0.9, 0.912, 0.92, 0.925, 0.934, 0.941, 0.944,
0.949, 0.954, 0.959, 0.963, 0.965, 0.97, 0.974, 0.977, 0.978,
0.98, 0.981, 0.984, 0.986, 0.987, 0.988, 0.989, 0.989, 0.99,
0.99, 0.99, 0.99, 0.991, 0.992, 0.992, 0.992, 0.992, 0.993, 0.995,
0.996, 0.997, 0.998, 0.998, 0.998, 0.998, 0.998, 0.998, 0.998,
0.998, 0.998, 0.998, 0.998, 0.998, 0.998, 0.998, 0.998, 0.998,
0.999, 0.999, 0.999, 0.999, 0.999, 0.999, 1, 0.001, 0.002, 0.003,
0.003, 0.005, 0.007, 0.011, 0.016, 0.022, 0.026, 0.028, 0.031,
0.036, 0.043, 0.044, 0.051, 0.056, 0.059, 0.064, 0.072, 0.076,
0.083, 0.083, 0.088, 0.096, 0.099, 0.103, 0.111, 0.118, 0.125,
0.129, 0.139, 0.145, 0.148, 0.156, 0.166, 0.175, 0.184, 0.191,
0.197, 0.2, 0.207, 0.211, 0.219, 0.224, 0.238, 0.25, 0.265, 0.273,
0.289, 0.3, 0.313, 0.328, 0.344, 0.354, 0.368, 0.378, 0.387,
0.399, 0.411, 0.424, 0.439, 0.455, 0.468, 0.48, 0.497, 0.503,
0.513, 0.52, 0.53, 0.539, 0.552, 0.56, 0.569, 0.581, 0.588, 0.596,
0.604, 0.618, 0.626, 0.64, 0.654, 0.672, 0.688, 0.705, 0.723,
0.733, 0.747, 0.765, 0.781, 0.795, 0.807, 0.818, 0.825, 0.836,
0.843, 0.854, 0.863, 0.872, 0.875, 0.883, 0.889, 0.901, 0.906,
0.914, 0.92, 0.928, 0.936, 0.943, 0.953, 0.958, 0.96, 0.965,
0.967, 0.968, 0.97, 0.973, 0.975, 0.979, 0.981, 0.982, 0.984,
0.984, 0.988, 0.99, 0.99, 0.992, 0.992, 0.992, 0.994, 0.994,
0.994, 0.995, 0.996, 0.998, 0.998, 0.998, 0.998, 0.998, 0.998,
0.998, 0.998, 0.999, 0.999, 0.999, 0.999, 0.999, 0.999, 0.999,
0.999, 0.999, 0.999, 1), t = c(21, 51, 81, 111, 141, 171, 201,
231, 261, 291, 321, 351, 381, 411, 441, 471, 501, 531, 561, 591,
621, 651, 681, 711, 741, 771, 801, 831, 861, 891, 921, 951, 981,
1011, 1041, 1071, 1101, 1131, 1161, 1191, 1221, 1251, 1281, 1311,
1341, 1371, 1401, 1431, 1461, 1491, 1521, 1551, 1581, 1611, 1641,
1671, 1701, 1731, 1761, 1791, 1821, 1851, 1881, 1911, 1941, 1971,
2001, 2031, 2061, 2091, 2121, 2151, 2181, 2211, 2241, 2271, 2301,
2331, 2361, 2391, 2421, 2451, 2481, 2511, 2541, 2571, 2601, 2631,
2661, 2691, 2721, 2751, 2781, 2811, 2841, 2871, 2901, 2931, 2961,
2991, 3021, 3051, 3081, 3111, 3141, 3171, 3201, 3231, 3261, 3291,
3321, 3351, 3381, 3411, 3441, 3471, 3501, 3531, 3561, 3591, 3621,
3651, 3681, 3711, 3741, 3771, 3801, 3831, 3861, 3891, 3921, 3951,
3981, 4011, 4041, 4071, 4, 34, 64, 94, 124, 154, 184, 214, 244,
274, 304, 334, 364, 394, 424, 454, 484, 514, 544, 574, 604, 634,
664, 694, 724, 754, 784, 814, 844, 874, 904, 934, 964, 994, 1024,
1054, 1084, 1114, 1144, 1174, 1204, 1234, 1264, 1294, 1324, 1354,
1384, 1414, 1444, 1474, 1504, 1534, 1564, 1594, 1624, 1654, 1684,
1714, 1744, 1774, 1804, 1834, 1864, 1894, 1924, 1954, 1984, 2014,
2044, 2074, 2104, 2134, 2164, 2194, 2224, 2254, 2284, 2314, 2344,
2374, 2404, 2434, 2464, 2494, 2524, 2554, 2584, 2614, 2644, 2674,
2704, 2734, 2764, 2794, 2824, 2854, 2884, 2914, 2944, 2974, 3004,
3034, 3064, 3094, 3124, 3154, 3184, 3214, 3244, 3274, 3304, 3334,
3364, 3394, 3424, 3454, 3484, 3514, 3544, 3574, 3604, 3634, 3664,
3694, 3724, 3754, 3784, 3814, 3844, 3874, 3904, 3934, 3964, 3994,
4024, 4054, 4084, 4114, 4144, 4174, 4204, 4234, 4264, 4294, 4324,
4354, 4384, 4414, 4444, 4474, 4504, 4534, 4564), n = c(1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), logq = c(-6.90775527898214,
-6.90775527898214, -6.21460809842219, -6.21460809842219, -5.29831736654804,
-5.29831736654804, -5.11599580975408, -4.96184512992682, -4.50986000618377,
-4.13516655674236, -3.91202300542815, -3.64965874096066, -3.41124771751566,
-3.35240721749272, -3.19418321227783, -3.10109278921182, -2.99573227355399,
-2.95651156040071, -2.91877123241786, -2.84731226843572, -2.74887219562247,
-2.71810053695571, -2.64507540194082, -2.59026716544583, -2.52572864430826,
-2.44184716032755, -2.33304430047875, -2.1982250776698, -2.0874737133771,
-2.02495335639577, -1.94491064872223, -1.87080267656851, -1.82015894374975,
-1.73727128394399, -1.67131331615219, -1.58474529984373, -1.53247687129797,
-1.49165487677772, -1.46533756846034, -1.42295834549148, -1.37832619147071,
-1.3130438993803, -1.24827306322252, -1.20064501423326, -1.13943428318836,
-1.06421086195078, -1.02722229258144, -0.959720289801491, -0.901402119380404,
-0.84629836005412, -0.789658080940789, -0.736054681571222, -0.707246104939447,
-0.65200523722877, -0.614336000135655, -0.570929547835696, -0.532730459154041,
-0.500875292912823, -0.454130280089445, -0.424647927524938, -0.390084006069862,
-0.365283318475333, -0.339677367570161, -0.316081546973479, -0.295714244149045,
-0.266573109241546, -0.244622582991334, -0.216913001563574, -0.204567165741274,
-0.193584749072665, -0.180323554131282, -0.16369609267079, -0.151986356997882,
-0.143870370419702, -0.126697653045958, -0.118783535989967, -0.105360515657826,
-0.0921152889078056, -0.083381608939051, -0.0779615414697118,
-0.0682788407532944, -0.0608121393967575, -0.0576291128366364,
-0.0523464803722092, -0.0470916075338506, -0.0418642040986989,
-0.0377018671840115, -0.0356271776431512, -0.0304592074847086,
-0.026343975339602, -0.0232686269393543, -0.0222456089473197,
-0.0202027073175195, -0.019182819416774, -0.0161293819298836,
-0.0140989243795016, -0.0130852395486555, -0.0120725812342692,
-0.0110609473594249, -0.0110609473594249, -0.0100503358535015,
-0.0100503358535015, -0.0100503358535015, -0.0100503358535015,
-0.00904074465214907, -0.00803217169726427, -0.00803217169726427,
-0.00803217169726427, -0.00803217169726427, -0.00702461493696447,
-0.00501254182354429, -0.00400802139753882, -0.00300450902029872,
-0.00200200267067308, -0.00200200267067308, -0.00200200267067308,
-0.00200200267067308, -0.00200200267067308, -0.00200200267067308,
-0.00200200267067308, -0.00200200267067308, -0.00200200267067308,
-0.00200200267067308, -0.00200200267067308, -0.00200200267067308,
-0.00200200267067308, -0.00200200267067308, -0.00200200267067308,
-0.00200200267067308, -0.00100050033358353, -0.00100050033358353,
-0.00100050033358353, -0.00100050033358353, -0.00100050033358353,
-0.00100050033358353, 0, -6.90775527898214, -6.21460809842219,
-5.80914299031403, -5.80914299031403, -5.29831736654804, -4.96184512992682,
-4.50986000618377, -4.13516655674236, -3.81671282562382, -3.64965874096066,
-3.57555076880693, -3.47376807449699, -3.32423634052603, -3.14655516328857,
-3.12356564506388, -2.97592964625781, -2.88240358824699, -2.83021783507642,
-2.74887219562247, -2.63108915996608, -2.57702193869581, -2.48891467118554,
-2.48891467118554, -2.43041846450393, -2.3434070875143, -2.31263542884755,
-2.2730262907525, -2.1982250776698, -2.13707065451647, -2.07944154167984,
-2.04794287462046, -1.97328134585145, -1.93102153656156, -1.91054300521802,
-1.8578992717326, -1.79576749062559, -1.74296930505862, -1.69281952137315,
-1.65548185093551, -1.62455155024415, -1.6094379124341, -1.57503648571677,
-1.55589714550607, -1.51868354916564, -1.4961092271271, -1.43548460531066,
-1.38629436111989, -1.32802545299591, -1.29828348379718, -1.2413285908697,
-1.20397280432594, -1.16155208844198, -1.11474167059799, -1.06711362160874,
-1.03845836584836, -0.999672340813206, -0.972861083362549, -0.949330585952355,
-0.918793862092274, -0.889162064485903, -0.858021823750179, -0.823255865906966,
-0.787457860031187, -0.75928698306449, -0.7339691750802, -0.699165252885508,
-0.687165108882398, -0.667479433811368, -0.653926467406664, -0.63487827243597,
-0.61803970807314, -0.594207232705042, -0.579818495252942, -0.563874844855806,
-0.543004522130226, -0.53102833108351, -0.517514611916787, -0.504181081047322,
-0.481266821524446, -0.468404907882039, -0.446287102628419, -0.424647927524938,
-0.397496938458987, -0.373966441048793, -0.349557476169868, -0.324346056823372,
-0.310609577095486, -0.29169009384932, -0.267879445155601, -0.247180129142451,
-0.229413164327805, -0.214431610712188, -0.20089294237939, -0.192371892647456,
-0.179126665897435, -0.170788320980282, -0.157824085193567, -0.147340587898709,
-0.136965855073157, -0.133531392624523, -0.124430078378177, -0.117658043468232,
-0.104250021373799, -0.0987159729391577, -0.089924707527987,
-0.083381608939051, -0.0747235461959364, -0.0661398025045449,
-0.0586889963486796, -0.048140375327935, -0.0429075010112765,
-0.0408219945202552, -0.0356271776431512, -0.0335567835288428,
-0.0325231917055601, -0.0304592074847086, -0.027371196796132,
-0.0253178079842899, -0.0212236364516267, -0.019182819416774,
-0.0181639706276712, -0.0161293819298836, -0.0161293819298836,
-0.0120725812342692, -0.0100503358535015, -0.0100503358535015,
-0.00803217169726427, -0.00803217169726427, -0.00803217169726427,
-0.00601807232556302, -0.00601807232556302, -0.00601807232556302,
-0.00501254182354429, -0.00400802139753882, -0.00200200267067308,
-0.00200200267067308, -0.00200200267067308, -0.00200200267067308,
-0.00200200267067308, -0.00200200267067308, -0.00200200267067308,
-0.00200200267067308, -0.00100050033358353, -0.00100050033358353,
-0.00100050033358353, -0.00100050033358353, -0.00100050033358353,
-0.00100050033358353, -0.00100050033358353, -0.00100050033358353,
-0.00100050033358353, -0.00100050033358353, 0), logt = c(3.04452243772342,
3.93182563272433, 4.39444915467244, 4.70953020131233, 4.94875989037817,
5.14166355650266, 5.30330490805908, 5.44241771052179, 5.56452040732269,
5.67332326717149, 5.77144112313002, 5.86078622346587, 5.9427993751267,
6.01859321449623, 6.08904487544685, 6.15485809401642, 6.21660610108486,
6.27476202124194, 6.3297209055227, 6.3818160174061, 6.43133108193348,
6.47850964220857, 6.52356230614951, 6.56667242980324, 6.60800062529609,
6.64768837356333, 6.68586094706836, 6.72262979485545, 6.75809450442773,
6.79234442747081, 6.82546003625531, 6.85751406254539, 6.88857245956536,
6.91869521902047, 6.94793706861497, 6.97634807044775, 7.00397413672268,
7.03085747611612, 7.05703698169789, 7.0825485693553, 7.1074254741107,
7.13169851046691, 7.15539630189673, 7.1785454837637, 7.20117088328168,
7.22329567956231, 7.24494154633701, 7.26612877955645, 7.2868764117507,
7.30720231476474, 7.32712329225929, 7.34665516317654, 7.36581283720947,
7.38461038317697, 7.40306109109009, 7.42117752859539, 7.43897159239586,
7.45645455517621, 7.47363710849621, 7.49052940206071, 7.50714107972761,
7.5234813125735, 7.53955882930103, 7.55538194424027, 7.57095858316901,
7.58629630715272, 7.60140233458373, 7.61628356158038, 7.63094658089046,
7.64539769942863, 7.65964295456468, 7.67368812926773, 7.68753876620163,
7.70120018085745, 7.71467747380093, 7.72797554210556, 7.74109909003537,
7.75405263903576, 7.76684053708551, 7.77946696745832, 7.79193595693806,
7.80425138352811, 7.8164169836918, 7.82843635915759, 7.84031298332016,
7.85205020726589, 7.86365126544865, 7.87511928104029, 7.88645727097769,
7.89766815072691, 7.90875473878325, 7.91971976092457, 7.93056585423396,
7.94129557090653, 7.95191138185419, 7.96241568012106, 7.9728107841214,
7.98309894071089, 7.99328232810159, 8.00336305862995, 8.01334318138667,
8.02322468471667, 8.03300949859667, 8.04269949689764, 8.05229649953865,
8.06180227453835, 8.07121853996986, 8.0805469658245, 8.08978917578932,
8.09894674894334, 8.10802122137675, 8.11701408773731, 8.12592680270789,
8.13476078241865, 8.14351740579748, 8.15219801586179, 8.16080392095467,
8.16933639592839, 8.17779668327778, 8.18618599422608, 8.19450550976564,
8.20275638165564, 8.21093973337902, 8.2190566610606, 8.22710823434815,
8.23509549725836, 8.24301946898925, 8.25088114470065, 8.25868149626424,
8.26642147298455, 8.27410200229233, 8.28172399041139, 8.28928832300032,
8.29679586577005, 8.30424746507847, 8.31164394850298, 1.38629436111989,
3.52636052461616, 4.15888308335967, 4.54329478227, 4.82028156560504,
5.03695260241363, 5.21493575760899, 5.36597601502185, 5.4971682252932,
5.61312810638807, 5.71702770140622, 5.8111409929767, 5.89715386763674,
5.97635090929793, 6.04973345523196, 6.11809719804135, 6.18208490671663,
6.24222326545517, 6.29894924685594, 6.35262939631957, 6.40357419793482,
6.45204895443723, 6.49828214947643, 6.5424719605068, 6.58479139238572,
6.62539236800796, 6.66440902035041, 6.70196036600254, 6.73815249459596,
6.77308037565554, 6.80682936039218, 6.83947643822884, 6.87109129461055,
6.90173720665657, 6.93147180559945, 6.96034772910131, 6.98841318199959,
7.01571242048723, 7.04228617193974, 7.06817200038804, 7.09340462586877,
7.11801620446533, 7.1420365747068, 7.16549347506085, 7.18841273649695,
7.21081845347222, 7.23273313617761, 7.25417784645652, 7.27517231945277,
7.29573507274928, 7.31588350450979, 7.3356339819272, 7.35500192110526,
7.37400185935016, 7.39264752072162, 7.41095187558364, 7.42892719480227,
7.44658509915773, 7.46393660446893, 7.48099216286952, 7.49776170062257,
7.51425465281641, 7.53047999524554, 7.54644627374602, 7.56216163122565,
7.57763383260273, 7.59287028784482, 7.60787807327851, 7.6226639513236,
7.63723438878947, 7.6515955738576, 7.6657534318617, 7.67971363996637,
7.69348164083518, 7.70706265537047, 7.72046169459972, 7.7336835707759,
7.74673290775362, 7.7596141506969, 7.77233157516961, 7.7848892956551,
7.79729127354747, 7.80954132465341, 7.82164312623998, 7.8336002236611,
7.84541603659248, 7.85709386490249, 7.86863689418417, 7.88004820097158,
7.89133075766189, 7.90248743716286, 7.91352101728389, 7.92443418488756,
7.93522953981691, 7.94590959861313, 7.95647679803678, 7.96693349840484,
7.97728198675515, 7.98752447984877, 7.9976631270201, 8.00770001288403,
8.01763715990848, 8.02747653086048, 8.03722003113301, 8.04686951095958,
8.05642676752298, 8.06589354696427, 8.07527154629746, 8.0845624152353,
8.09376775793108, 8.10288913464087, 8.11192806331074, 8.12088602109284,
8.12976444579417, 8.13856473726163, 8.14728825870662, 8.15593633797239,
8.16451026874704, 8.17301131172497, 8.18144069571937, 8.18979961872823,
8.19808924895612, 8.20631072579402, 8.21446516075919, 8.22255363839696,
8.23057721714645, 8.23853693017177, 8.24643378616036, 8.25426877009018,
8.26204284396694, 8.26975694753298, 8.277411998949, 8.28500889544988,
8.29254851397576, 8.30003171177957, 8.30745932701195, 8.31483217928456,
8.3221510702129, 8.32941678393932, 8.33663008763715, 8.34379173199684,
8.35090245169481, 8.35796296584568, 8.36497397843873, 8.3719361787591,
8.37885024179449, 8.38571682862785, 8.39253658681668, 8.39931015075952,
8.40603814205008, 8.41272116981953, 8.41935983106747, 8.42595471098197
)), row.names = c(NA, 289L), class = "data.frame")
There was a couple of issues with the the definition for the plot.
One since the group factor did not apply to all of the geom_line() or is redefined, it should be moved from the ggplot function to the specific geom_line() definition.
Also, since you were adding in a new data frame to the definition, then explicitly add the "data=" to function.
Also, based on your model: lm(surscript$logq~surscript$logt) the equation for "temp" was incorrect it should be: q_predicted =exp(-14.273)*(t^1.717). If you truly want y=b*a^t, then your models should be: lm(surscript$logq~surscript$t) and q_predicted = exp(intercept)*exp(slope)^t.
library(ggplot2)
expmodint<- exp(-14.273)
expmodgrowth<- (1.717)
#q=exp(-14.273)*(t^1.717)
temp<-expmodint*(surscript$t^expmodgrowth)
temp1<-data.frame(temp,surscript$t)
head(temp1) #notice the name change
ggplot(surscript, aes(x=t, y=q)) +
geom_line(aes(group=sim)) +
geom_line(data=temp1, aes(x=surscript.t, y=temp), color="blue")

dividing the values of data.table1 by data.table2 in R

I have created two fictional data.tables which summarise the cost and count of the items that contributed to the cost. I would like to calculate the average item cost = cost/count.
How can i divide the values of the two data.tables ?
combi_sum <- dcast(merge(mtcarsTOTAL[,.(cost, gear)], iris[, .N, .(carb, gear, gender, age)], by = "gear"),
gender + age ~ carb, value.var = "cost", fun.aggregate = sum, fill = 0)
structure(list(gender = c(1L, 1L, 1L, 1L, 1L, 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, 3L, 3L, 3L, 3L), age = c(1L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 11L, 12L, 13L, 14L), `1` = c(978, 978, 0, 0, 1074, 0, 0,
0, 2642, 2642, 0, 0, 0, 0, 3620, 0, 978, 2642, 0, 0, 978, 0,
0, 978, 2052, 0, 0, 0, 0, 1074, 978, 0, 0, 0, 3620, 0), `2` = c(0,
0, 0, 978, 0, 0, 0, 2052, 0, 2642, 978, 0, 0, 0, 0, 0, 0, 0,
1074, 2642, 0, 0, 0, 0, 0, 0, 0, 1074, 0, 978, 0, 2642, 0, 0,
978, 2642), `3` = c(0, 0, 0, 0, 0, 978, 2642, 0, 2642, 0, 0,
2642, 978, 0, 978, 2642, 0, 0, 0, 0, 1074, 3620, 2642, 0, 0,
0, 978, 0, 2642, 0, 0, 2642, 0, 2642, 0, 0), `4` = c(0, 0, 1074,
0, 0, 0, 978, 0, 0, 0, 1074, 1074, 0, 2052, 0, 0, 0, 0, 0, 1074,
0, 2642, 1074, 978, 978, 2642, 0, 0, 2642, 0, 2052, 0, 2642,
1074, 0, 0)), row.names = c(NA, -36L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x7fb24d802ee0>, sorted = c("gender",
"age"))
combi_length <- dcast(merge(mtcarsTOTAL[,.(cost, gear)], iris[, .N, .(carb, gear, gender, age)], by = "gear"),
gender + age ~ carb, value.var = "cost", fun.aggregate = length, fill = 0)
structure(list(gender = c(1L, 1L, 1L, 1L, 1L, 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, 3L, 3L, 3L, 3L), age = c(1L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 11L, 12L, 13L, 14L), `1` = c(1L, 1L, 0L, 0L, 1L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 2L, 0L), `2` = c(0L,
0L, 0L, 1L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
0L, 1L, 1L), `3` = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L,
0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 0L,
1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L), `4` = c(0L, 0L, 1L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 2L, 0L, 1L, 1L, 0L,
0L)), row.names = c(NA, -36L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7fb24d802ee0>, sorted = c("gender",
"age"))
Here is another option:
combi_sum[combi_length, as.character(1L:4L) := {
m <- unlist(mget(paste0("x.", 1L:4L))) / unlist(mget(paste0("i.", 1L:4L)))
as.data.table(matrix(replace(m, is.nan(m), 0), nrow=.N))
}]
data:
library(data.table)
combi_sum <-
structure(list(gender = c(1L, 1L, 1L, 1L, 1L, 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, 3L, 3L, 3L, 3L), age = c(1L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 11L, 12L, 13L, 14L), `1` = c(978, 978, 0, 0, 1074, 0, 0,
0, 2642, 2642, 0, 0, 0, 0, 3620, 0, 978, 2642, 0, 0, 978, 0,
0, 978, 2052, 0, 0, 0, 0, 1074, 978, 0, 0, 0, 3620, 0), `2` = c(0,
0, 0, 978, 0, 0, 0, 2052, 0, 2642, 978, 0, 0, 0, 0, 0, 0, 0,
1074, 2642, 0, 0, 0, 0, 0, 0, 0, 1074, 0, 978, 0, 2642, 0, 0,
978, 2642), `3` = c(0, 0, 0, 0, 0, 978, 2642, 0, 2642, 0, 0,
2642, 978, 0, 978, 2642, 0, 0, 0, 0, 1074, 3620, 2642, 0, 0,
0, 978, 0, 2642, 0, 0, 2642, 0, 2642, 0, 0), `4` = c(0, 0, 1074,
0, 0, 0, 978, 0, 0, 0, 1074, 1074, 0, 2052, 0, 0, 0, 0, 0, 1074,
0, 2642, 1074, 978, 978, 2642, 0, 0, 2642, 0, 2052, 0, 2642,
1074, 0, 0)), row.names = c(NA, -36L), class = c("data.table",
"data.frame"))
setDT(combi_sum, key=c("gender", "age"))
combi_length <- structure(list(gender = c(1L, 1L, 1L, 1L, 1L, 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, 3L, 3L, 3L, 3L), age = c(1L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 11L, 12L, 13L, 14L), `1` = c(1L, 1L, 0L, 0L, 1L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 2L, 0L), `2` = c(0L,
0L, 0L, 1L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
0L, 1L, 1L), `3` = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L,
0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 0L,
1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L), `4` = c(0L, 0L, 1L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 2L, 0L, 1L, 1L, 0L,
0L)), row.names = c(NA, -36L), class = c("data.table", "data.frame"
))
setDT(combi_length, key=c("gender", "age"))
Maybe I am misunderstanding, but to divide values of one data.frame by another in R - you literally just divide them. I've created an example dataset to show you:
t1 <- data.frame(A1=c(10,2,4,1,4), B1=c(5,1,8,9,4), C1=c(12,10,10,5,1))
t2 <- data.frame(A2=c(8,2,5,10,1), B2=c(5,6,8,9,1), C2=c(6,5,15,10,12))
To divide t2/t1, you just do that:
t2/t1
Giving you:
A2 B2 C2
1 0.80 1.00 0.5
2 1.00 6.00 0.5
3 1.25 1.00 1.5
4 10.00 1.00 2.0
5 0.25 0.25 12.0
Basically, this can be understood as t1[x,y]/t2[x,y], giving you the answer dataset as t3[x,y]. Make sense? Is that what you were asking?

Is it possible to extend the intervals of the x-axis in R?

I have two plots: a barplot, and a ggplot(geom_jitter bubbleplot). Ultimately, I am using a photo editing app to line up these two plots. As you can see, the intervals in the bottom of these two plots do not match up, which is my problem here. I would like to make it so I can just change the bottom x-axis of both plots to 400 (lowest common interval to cover x-axis of both plots). I do not want to change the data values, just the axis values.
Barplot Code
GYPCdomain <- read.csv(file.choose(), header=TRUE)
GYPCbarplot <- barplot(as.matrix(GYPCdomain), horiz=TRUE, xlab = "Length (Protein Domains Shown)",
col=c("azure", "plum1", "skyblue"),
legend = c("Cytoplasmic", "Helical Membrane", "Extracellular"))
sample data:
structure(list(GYPC = c(0L, 0L, 171L, 0L, 72L, 0L, 141L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L)), class = "data.frame", row.names = c(NA, -42L))
Bubbleplot Code
library(ggplot2)
library(scales)
data(GYPC, package="ggplot2")
GYPC <- read.csv(file.choose(), header = TRUE)
GYPCggplot <- ggplot(GYPC, aes(Position, log10(Frequency)))+
geom_jitter(aes(col=Geographical.Location, size =(p.value)))+
labs(subtitle="Frequency of Various Polymorphisms", title="GYPC Gene") +
labs(color = "Geographical Location") +
labs(size = "p-value") + labs(x = "Position of Polymorphism on GYPC Gene") +
scale_size_continuous(range=c(1,4.5), trans = "reverse") +
guides(size = guide_legend(reverse = TRUE))
sample data:
structure(list(Variant = structure(c(4L, 4L, 4L, 4L, 4L, 8L,
8L, 8L, 8L, 8L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 12L,
12L, 12L, 12L, 12L, 14L, 14L, 14L, 14L, 14L, 2L, 2L, 2L, 2L,
2L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L, 15L, 15L), .Label = c("rs111631066",
"rs114199197", "rs115178969", "rs115201071", "rs139780142", "rs139816143",
"rs143080607", "rs143216051", "rs199797395", "rs531807314", "rs545780841",
"rs551011574", "rs560942282", "rs567759380", "rs571586275"), class = "factor"),
Position = c(213L, 213L, 213L, 213L, 213L, 60L, 60L, 60L,
60L, 60L, 249L, 249L, 249L, 249L, 249L, 183L, 183L, 183L,
183L, 183L, 282L, 282L, 282L, 282L, 282L, 294L, 294L, 294L,
294L, 294L, 150L, 150L, 150L, 150L, 150L, 135L, 135L, 135L,
135L, 135L, 258L, 258L, 258L, 258L, 258L, 255L, 255L, 255L,
255L, 255L, 138L, 138L, 138L, 138L, 138L, 159L, 159L, 159L,
159L, 159L, 141L, 141L, 141L, 141L, 141L, 198L, 198L, 198L,
198L, 198L, 258L, 258L, 258L, 258L, 258L), Geographical.Location = 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, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("AFR",
"AMR", "EAS", "EUR", "SAS"), class = "factor"), Frequency = c(0.023,
0.001, 0, 0, 0, 0.017, 0.001, 0, 0, 0, 0.012, 0, 0, 0, 0,
0.002, 0.003, 0.002, 0.023, 0.016, 0.001, 0, 0, 0, 0, 0,
0, 0, 0, 0.004, 0, 0, 0, 0.001, 0, 0, 0, 0, 0, 0.001, 0,
0, 0.001, 0, 0, 0.001, 0, 0, 0, 0, 0, 0.001, 0, 0, 0, 0,
0, 0, 0, 0.002, 0, 0, 0.001, 0, 0, 0, 0, 0, 0, 0.001, 0,
0, 0.001, 0, 0), pre.p.value = c(6.32e-17, 0.113, 0.00126,
0.00126, 0.00211, 2.51e-12, 0.356, 0.00806, 0.00809, 0.0139,
4.86e-10, 0.15, 0.0542, 0.0542, 0.0537, 0.000376, 0.0778,
0.0068, 7.4e-06, 0.0109, 0.264, 1, 1, 1, 1, 0.579, 1, 0.589,
0.59, 0.00144, 1, 1, 1, 0.201, 1, 1, 1, 1, 1, 0.195, 1, 1,
0.201, 1, 1, 1, 1, 0.201, 1, 1, 1, 0.139, 1, 1, 1, 1, 1,
1, 1, 0.0381, 1, 1, 0.201, 1, 1, 1, 1, 1, 1, 0.195, 1, 1,
0.201, 1, 1), p.value = c(0, 0.75, 0.5, 0.5, 0.5, 0, 0.75,
0.5, 0.5, 0.75, 0, 0.75, 0.75, 0.75, 0.75, 0.5, 0.75, 0.5,
0.25, 0.75, 0.75, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 1, 1, 1, 0.75,
1, 1, 1, 1, 1, 0.75, 1, 1, 0.75, 1, 1, 1, 1, 0.75, 1, 1,
1, 0.75, 1, 1, 1, 1, 1, 1, 1, 0.75, 1, 1, 0.75, 1, 1, 1,
1, 1, 1, 0.75, 1, 1, 0.75, 1, 1), log.p.value. = c(-16.19928292,
-0.947, -2.899629455, -2.899629455, -2.675717545, -11.60032628,
-0.449, -2.093664958, -2.092051478, -1.8569852, -9.313363731,
-0.824, -1.266000713, -1.266000713, -1.270025714, -3.424812155,
-1.11, -2.167491087, -5.13076828, -1.962573502, -0.5783960731,
0, 0, 0, 0, -0.2373214363, 0, -0.2298847052, -0.2291479884,
-2.841637508, 0, 0, 0, -0.6968039426, 0, 0, 0, 0, 0, -0.7099653886,
0, 0, -0.6968039426, 0, 0, 0, 0, -0.6968039426, 0, 0, 0,
-0.857, 0, 0, 0, 0, 0, 0, 0, -1.419075024, 0, 0, -0.6968039426,
0, 0, 0, 0, 0, 0, -0.7099653886, 0, 0, -0.6968039426, 0,
0), X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), range = structure(c(2L, 6L, 5L, 4L, 3L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
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("", "0 < p-value < 1E-9",
"1E-2 < p-value < 1", "1E-4 < p-value < 1E-2", "1E-6 < p-value < 1E-4",
"1E-9 < p-value < 1E-6"), class = "factor")), class = "data.frame", row.names = c(NA,
-75L))
I took the liberty to produce your barplot also with ggplot, because than we can use the awesome features of the cowplot package, which was made for things like these. Setting axis limits can be done with ylim() or xlim() but because of different width of the legends, we need the cowplot package to truly align the plots (or the legends would need to go below the plots)
#recreating the barplot
library(dplyr) #needed for data wrangling
GYPCbarplot_ggplot=GYPCdomain %>%
filter(GYPC>0) %>%
mutate(domain=factor(c("Cytoplasmic", "Helical Membrane", "Extracellular"),
levels=c("Cytoplasmic", "Helical Membrane", "Extracellular"),
ordered = T)) %>%
ggplot(aes(x=1,y=GYPC,fill=domain))+
geom_col(position="stack")+
scale_fill_manual(values=c("Cytoplasmic"="azure", "Helical Membrane"="plum1", "Extracellular"="skyblue"))+
coord_flip()+
xlab("GYPC")+
ylab( "Length (Protein Domains Shown)")+
ylim(0,400)+ #creates the limit
theme(panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
#the bubbleplot
GYPC_bubbleplot <- ggplot(GYPC_data, aes(Position, log10(Frequency)))+
geom_jitter(aes(col=Geographical.Location, size =(p.value)))+
labs(subtitle="Frequency of Various Polymorphisms", title="GYPC Gene") +
labs(color = "Geographical Location") +
labs(size = "p-value") + labs(x = "Position of Polymorphism on GYPC Gene") +
scale_size_continuous(range=c(1,4.5), trans = "reverse") +
guides(size = guide_legend(reverse = TRUE))+
xlim(0,400) #added this limit
library(cowplot) #used to arrange the two plots
plot_grid(GYPCbarplot_ggplot,GYPC_bubbleplot,
ncol = 1, #both plots in one column (below each other)
align = "v", #align both bottom axes
rel_heights = c(1,1.5) #make bottom plot a bit higher
)
et voila:
If I understand correctly, the OP is asking to synchronise the x-axes in order to show the protein domains a certain position on the GYPC gene belongs to.
If my assumption is correct then there is an alternative approach which fills the background of the bubble plot according to the protein domains:
library(dplyr)
domain_name <- c("Cytoplasmic", "Helical Membrane", "Extracellular")
domain_fill <- c("azure", "plum1", "skyblue")
names(domain_fill) <- domain_name
GPYCdomain_2 <- GYPCdomain %>%
filter(GYPC > 0) %>%
mutate(domain_name = forcats::fct_inorder(rev(domain_name)),
end_pos = cumsum(GYPC),
start_pos = lag(end_pos, default = 0L))
library(ggplot2)
ggplot(GYPC, aes(Position, log10(Frequency))) +
geom_rect(aes(xmin = start_pos, xmax = end_pos, ymin = -Inf, ymax = Inf, fill = domain_name),
data = GPYCdomain_2, inherit.aes = FALSE, alpha = 0.6) +
scale_fill_manual(values = domain_fill) +
geom_jitter(aes(color = Geographical.Location, size = (p.value))) +
labs(subtitle = "Frequency of Various Polymorphisms", title = "GYPC Gene") +
labs(color = "Geographical Location") +
labs(size = "p-value") +
labs(x = "Position of Polymorphism on GYPC Gene") +
labs(fill = "Protein Domain") +
scale_size_continuous(range = c(1, 4.5), trans = "reverse") +
guides(size = guide_legend(reverse = TRUE))

Matching colums and rows in a special condition

output1 <- output1 <- structure(list(row = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 214L, 214L,214L), col = c(17L, 17L, 17L, 17L, 17L, 17L, 16L, 110L, 111L,111L), cell = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 27244L, 27245L, 27245L), xcoord = c(783750L, 783750L, 783750L, 783750L, 783750L, 783750L,783725L, 786075L, 786100L, 786100L), ycoord = c(187050L, 187050L,187050L, 187050L, 187050L, 187050L, 187025L, 181725L, 181725L,181725L), species = structure(c(1L, 1L, 1L, 8L, 9L, 11L, 1L,3L, 3L, 3L), .Label = c("abiealba", "alnuinca", "alnuviri", "betupend","betupube", "fagusilv", "larideci", "piceabie", "pinucemb", "pinusilv","popunigr", "poputrem", "salicapr", "sorbaucu"), class = "factor"),age = c(100L, 20L, 10L, 100L, 100L, 100L, 100L, 30L, 70L,30L), biomass = c(0.1015, 0.0152, 0.0127, 0.5391, 0.02, 0.1584,0.1019, 0.0114, 0.0115, 0.0114), stems = c(1L, 10L, 10L,20L, 5L, 3L, 4L, 15L, 2L, 10L), slowGrowth = c(0L, 0L, 0L,0L, 14L, 0L, 0L, 0L, 0L, 0L), DBH = c(17.9273, 8.831, 8.2681,34.9717, 9.7366, 18.9254, 17.9523, 6.6486, 6.6793, 6.6486), height = c(14.0924, 8.0258, 7.625, 23.4468, 8.0478, 13.6345,14.1081, 3.6519, 3.6552, 3.6519), availableLight = c(0.0934,0.0807, 0.071, 0.4742, 0.0887, 0.101, 0.0985, 0.958, 0.9952,0.9624), light_rf = c(0.2619, 0.2067, 0.1708, 0.6971, 0.063,0.1049, 0.2896, 0.9768, 0.9972, 0.9793), LeafArea = c(5.4506,5.4506, 5.4506, 5.4506, 5.4506, 5.4506, 5.2884, 0.2307, 0.1732,0.1732), nitorgen_rf = c(0, 0, 0, 0, 0.1328, 0, 0, 0, 0,0), droughtIndex = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), moisture_rf = c(1,1, 1, 1, 1, 1, 1, 1, 1, 1), degreeDay_rf = c(0.258, 0.258,0.258, 0.4726, 0.5144, 0.237, 0.258, 0.1125, 0.1125, 0.1125), foliageWght = c(0.0093, 0.0031, 0.0028, 0.0265, 0.0036,0.0023, 0.0094, 5e-04, 5e-04, 5e-04), twigWght = c(0.0537,0.0115, 0.0096, 0.0513, 0.0149, 0.0847, 0.0538, 0.0109, 0.011,0.0109), boleWght = c(0.0384, 6e-04, 3e-04, 0.4613, 0.0015,0.0713, 0.0387, 0, 0, 0), deadFoliage = c(0.405, 0.405, 0.405,0.405, 0.405, 0.405, 0.3664, 0.0627, 0.0534, 0.0534), deadTwig = c(0.9887,0.9887, 0.9887, 0.9887, 0.9887, 0.9887, 0.9537, 0.7391, 0.8132,0.8132), deadbole = c(2.3166, 2.3166, 2.3166, 2.3166, 2.3166,2.3166, 2.3947, 0, 0, 0)), .Names = c("row", "col", "cell","xcoord", "ycoord", "species", "age", "biomass", "stems", "slowGrowth","DBH", "height", "availableLight", "light_rf", "LeafArea", "nitorgen_rf","droughtIndex", "moisture_rf", "degreeDay_rf", "foliageWght","twigWght", "boleWght", "deadFoliage", "deadTwig", "deadbole"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 131023L, 131024L,131025L), class = "data.frame")
and
Details <- structure(list(fireID = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1052L,1052L, 1052L), decade = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 100L, 100L,100L), cell = c(14150L, 14321L, 14320L, 14489L, 14323L, 13977L,14492L, 14461L, 14122L, 14123L), row = c(128L, 129L, 129L, 130L,129L, 127L, 130L, 130L, 128L, 128L), column = c(137L, 137L, 136L,135L, 139L, 136L, 138L, 107L, 109L, 110L), biomass = c(0.724241,0.652821, 0.776811, 0.860563, 0.649643, 0.751143, 0.760428, 20.5968,33.6653, 15.1725)), .Names = c("fireID", "decade", "cell", "row","column", "biomass"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,12896L, 12897L, 12898L), class = "data.frame")
I want to match these two dataset by rows and cols. Actually, I did it with
aa <- merge.data.frame(Details, output1, by=c("cell","row"))
but the problem is I have many rows in output1 which has same coordinates. However I only want to get one coordinates for each row in my details output.
Any suggestions?
Thanks in advance.
If I understand the question correctly you need something like this:
aa <- aa[!duplicated(aa[c("row", "cell")]), ]
I am removing not unique combinations of row and cell because I would imagine that cell plays a role in your analysis since you use it in the merge. Otherwise:
aa <- aa[!duplicated(aa["row"]), ]

Resources