I'm quite new to R (and to this website) and I'm stuck with a simple problem, yet I have not found a solution for it. I work on a dataset containing potential density values measured by a CTD within a water column at several dates. I want to group those measures by date within a data frame.
What I have at the beginning :
ctd<-read.csv("data/ptb_ctd.csv", sep=";", stringsAsFactors = FALSE)
julian <- as.numeric(as.Date(ctd$date, format= "%d/%m/%Y")) + 17280
ctd$julian <- julian
Example from the initial "ctd" data frame (after adding julian days as shown above) :
id station date ... pot_density julian
B12171_01.cnv B+ 19/06/2012 ... 28.73863 32790
Now I want to take "pot_density" values (several for each date, ie same "date" & "julian" in the data frame) and store them in a new data frame, so julian days become rows, and columns are formed by every single "pot_density" measurement for that day/row.
dens<-data.frame()
dens<-aggregate(pot_density ~ julian, data=ctd, FUN=paste)
Now I have in the "dens" data frame:
julian pot_density
27548 c("28.4698015312665", "28.4704581365093", "28.4706453687...)
... ...
I want now to unlist the "pot_density" values into n columns for each rows, and that's where I'm stuck. I need that to be able to apply a function detecting the pycnocline for every sampling date, and to plot the interannual variation of the pycnocline...
I tried several function :
dens <- str_split_fixed(dens, pattern=",", n=Inf)
dens<-split(dens$pot_density, julian)
dens<-spread(dens, key="julian", value="pot_density", convert=FALSE, fill=NA)
dens<-lapply(dens$pot_dens, FUN=spread)
dens<-reshape(dens,varying=NULL, v.names = pot_density,timevar=julian)
Mind that needed packages were loaded beforehand; I searched for a similar problem on the forum but could not find any related topics (or could not understand it was similar to my own problem, again I'm new to R and not very skillful in general when it comes to coding).
Did I miss any function that would do exactly what I want to do here ? Do I not correctly enter arguments for the above functions ?
> dput(dens[1:10,])
structure(list(julian = c(27548, 27555, 27562, 27569, 27576,
27583, 27588, 27590, 27597, 27604), pot_density = structure(list(
`001` = c(28.4698015312665, 28.4704581365093, 28.4706453687199,
28.4713658034907, 28.4715960505218, 28.4713220614417, 28.4719434816675,
28.4716438928615, 28.4719381177117, 28.4724430855808, 28.4729393244543,
28.4730915014395, 28.472882160301, 28.4730989562061, 28.4737587846116,
28.4739971132756, 28.4742267211307, 28.4738625366081, 28.4743244355707,
28.4742699732992, 28.4750118357917, 28.4756588360735, 28.4750839098374,
28.475580174204, 28.4752806092672, 28.4752907658838, 28.4753439905428,
28.4754444132004, 28.4763878641565, 28.4769744045504, 28.4774661037934,
28.4779106145504, 28.4778992589288, 28.4780299398942, 28.4781821563902,
28.4780933780096, 28.4778841043003, 28.4779158290446, 28.4781583123097,
28.478112611112, 28.4778043852182, 28.4779263792905, 28.4778719824292,
28.4782739109617, 28.4782282194151, 28.4779241414187, 28.4777620899599,
28.4779143365161, 28.4776102589242, 28.4776204821671, 28.4776737743953,
28.4772624867899, 28.477105022565, 28.476865564174, 28.4773490796508,
28.4768645036095, 28.4771716304488, 28.4775433527325, 28.4772223424882,
28.476673185145, 28.4766705975139, 28.4765172931616, 28.4767341623638,
28.4764260035167, 28.4763373003382, 28.4766788132085, 28.476667545619,
28.4764670489733, 28.4764644743016, 28.4765778578981, 28.4766784022163,
28.4767015185159, 28.4766173680835, 28.4766918586333, 28.4767621912918,
28.4768370707511, 28.4769806916393, 28.4767458510698, 28.4764551085279,
28.4765602010568, 28.476802425097, 28.4769630495639), `002` = c(28.3854267585043,
28.3880639603979, 28.3910917267553, 28.4010348607969, 28.412893371411,
28.4224347755983, 28.4397727476692, 28.4452234057089, 28.4458186136571,
28.4457641137874, 28.446195710763, 28.4465498862407, 28.447359840826,
28.4499888764699, 28.4505841110556, 28.4524108843229, 28.4528041503486,
28.4527927667575, 28.452415805632, 28.4524259732418, 28.4525741718935,
28.4532899260366, 28.4532011281387, 28.4537061521382, 28.4546198491653,
28.4549269344061, 28.4554837839473, 28.4560318858134, 28.4558656729801,
28.4565168108963, 28.4580714631404, 28.459325139906, 28.4597742710259,
28.4601931366756, 28.4605089496824, 28.4610011510342, 28.461360027075,
28.4612712582959, 28.4615783463796, 28.4619069702394, 28.4623304367783,
28.4650345230459, 28.4681773148704, 28.4690697062383, 28.4696951373126,
28.4699591415863, 28.4705974387778, 28.4711798263061, 28.4713535340786,
28.4729642397172, 28.4746824791714, 28.4755574246585, 28.4772239205411,
28.4791527947114, 28.479842680591, 28.4809713436873, 28.4810804730612,
28.4811121691725, 28.4812901464768, 28.4810680352355, 28.4815299275549,
28.4815057026794, 28.4812620912953, 28.4812292832326, 28.4814502899062,
28.4814045681574, 28.4812943327379, 28.481261535575, 28.4814266226144,
28.4814583458203, 28.4814126352126, 28.4815433020397, 28.4813556469537,
28.4814863176814, 28.4813761037037, 28.4815197036496, 28.4816848098903,
28.48116592119, 28.4809912072346, 28.4808035711785, 28.4807923130716,
28.4808025610578, 28.4807568786814), `003` = c(28.4222716517768,
28.4238227923045, 28.4244286059802, 28.4247857381142, 28.4254711015647,
28.4251247518509, 28.4250664765352, 28.4242110005252, 28.4239874893299,
28.4244331766597, 28.4244307460999, 28.4252724126586, 28.4246797174392,
28.4243535117068, 28.4248676773248, 28.4255202537067, 28.4249313231394,
28.4246952098579, 28.4246927964123, 28.4246040446426, 28.4247728637667,
28.4249126280299, 28.424798557425, 28.4250804805711, 28.4237196924828,
28.4235140937569, 28.423868268211, 28.4242187238988, 28.4240563062563,
28.4237822331315, 28.4235297494561, 28.4236732796012, 28.4234639766482,
28.4233410268371, 28.4232865598096, 28.4232194426204, 28.4234061603524,
28.4234038076627, 28.4240423645779, 28.4242938447276, 28.4244679089948,
28.4243576326924, 28.4246738680081, 28.4244430125298, 28.4240699899697,
28.4240587249963, 28.4243787102132, 28.4246860351459, 28.4249337795727,
28.4255775029735, 28.4262123010587, 28.4271491888576, 28.4272242462023,
28.4275188694648, 28.4276497675173, 28.4287147586972, 28.430171883095,
28.4317926616795, 28.4344297892021, 28.4347331829235, 28.435169843448,
28.4359720371187, 28.4361803258259, 28.436676736914, 28.4369879156638,
28.4368776977024, 28.4379719221961, 28.4382360824147, 28.4386982121987,
28.4386311295355, 28.4391275598127, 28.4423121792161, 28.4449764223293,
28.4452748965623, 28.4455518106192, 28.4455621665654, 28.4474708628147,
28.4487640574255, 28.4482148281136, 28.4481477640663, 28.4507580309098,
28.453191788152, 28.4559910165078), `004` = c(28.3674842654145,
28.3674585519213, 28.3687819915356, 28.3701195579054, 28.3717521524566,
28.372642125416, 28.3742275133602, 28.3746753772148, 28.3751518674221,
28.3749668049827, 28.3747626363604, 28.3745749409343, 28.3772798275775,
28.3780912856612, 28.3790912835113, 28.3868064589531, 28.3958804355645,
28.3994818365295, 28.3992476445208, 28.400672676044, 28.4037720361812,
28.40710651225, 28.4083455760644, 28.4086190175331, 28.4097124086866,
28.4095712793646, 28.4098230665193, 28.4109380474015, 28.411694221968,
28.4131744935837, 28.4163624784683, 28.4215274536921, 28.4223266670829,
28.422401914773, 28.4239591967716, 28.4252535051301, 28.4253685001913,
28.4254653481037, 28.4260447806714, 28.4265035561596, 28.427720349943,
28.4290884003017, 28.4300280158559, 28.4313562610103, 28.4319120864368,
28.4331142634078, 28.4348025261395, 28.4359579129168, 28.4367262283533,
28.4392120327445, 28.4407292465714, 28.4422338288396, 28.4426492362709,
28.4435165077577, 28.4444791255601, 28.4450061474402, 28.4455673940549,
28.4454229351384, 28.4471559829733, 28.449146520896, 28.4513692873495,
28.4523786554164, 28.4523890271721, 28.4524768192166, 28.452883216132,
28.4532680268433, 28.4539714377001, 28.4546152681519, 28.4559736714407,
28.4579082276664, 28.4587880246834, 28.4607314323666, 28.4637244206224,
28.4658657310663, 28.4668140374613, 28.4680276744066, 28.4688960579203,
28.4696286168744, 28.4714159066223, 28.4730508468001, 28.4734165292716,
28.4746173674025, 28.4768223015874, 28.4790806324759), `005` = c(28.4526758311781,
28.4535846776619, 28.4525481478231, 28.451856584908, 28.452796815023,
28.4531774243305, 28.4528906364108, 28.4555429468128, 28.4588625838871,
28.4628239080419, 28.4746066981118, 28.4783993954704, 28.4830649177188,
28.4847313847029, 28.4876454410833, 28.4876856311901, 28.4883710903493,
28.4886005631158, 28.48842567521, 28.4883841535125, 28.4886050424675,
28.4883613194199, 28.4887671844829, 28.4887987867212, 28.4886582671045,
28.4882251409733, 28.4884546185867, 28.4885505620628, 28.489940591599,
28.4898002226066, 28.4908374297358, 28.4909204841301, 28.4913307399261,
28.4915207647966, 28.4921408576779, 28.4923781710111, 28.4923882445491,
28.4920930862268, 28.4923439633267, 28.4923280301164, 28.4924110117045,
28.4925974550899, 28.4932947417217, 28.4938071280703, 28.4939161205064,
28.4936256066853, 28.4937859910672, 28.4932416796044, 28.4935055716303,
28.4946804064894, 28.4955798990654, 28.495731830197, 28.496348426394,
28.497184308583, 28.4985446097212, 28.4986321483468, 28.4986767625505,
28.5003393077807, 28.5002159856526, 28.5000497376759, 28.4996549175607,
28.5004011987899, 28.5020784731132, 28.5029113589508, 28.5031890632051,
28.503518813731, 28.5050218566516, 28.5053072866085, 28.5052829446752,
28.5072791558746, 28.507418126105, 28.5117619091457, 28.513289288532,
28.5140645484551, 28.5142641072432, 28.5149236725456, 28.5144047544632,
28.5143243805205, 28.5141581735936, 28.5140311818116, 28.5144929463786,
28.5146663817948, 28.5146121632208, 28.5146223221971), `006` = c(28.3810645677661,
28.3813004672918, 28.3817133134175, 28.3831197047741, 28.3854421338472,
28.3871465607181, 28.3890759077963, 28.3902102320865, 28.3918042896855,
28.3953858494501, 28.3980196932803, 28.4004112692514, 28.4045071681478,
28.4072099242962, 28.4087066328063, 28.4126367239094, 28.4136233956574,
28.4167355043942, 28.4209358369089, 28.4270260115261, 28.4312535365273,
28.4374438272673, 28.4432689680516, 28.4486149408372, 28.4512622328464,
28.4524165801954, 28.451291051108, 28.4512323293889, 28.4523651405086,
28.453386129449, 28.4548632140204, 28.4555100338371, 28.4561826678098,
28.4571305840868, 28.4588485442057, 28.461116982636, 28.4629807022809,
28.4637778898482, 28.4638998839528, 28.4654069253183, 28.4661010178108,
28.4687266064548, 28.4703284448451, 28.4710266704567, 28.4748731605503,
28.4771755897834, 28.4800799759928, 28.481668318886, 28.4830932651969,
28.4844062566217, 28.4865964387086, 28.4884598794474, 28.4909083129323,
28.4931281742258, 28.4947161865994, 28.4952936224602, 28.4964728837094,
28.4974244422872, 28.4980148947077, 28.498888977389, 28.4992899860013,
28.5005381834585, 28.5025299171707, 28.5050208121045, 28.5080144643618,
28.5116394505137, 28.5120879762683, 28.5132847433542, 28.5140001857922,
28.5141222899542, 28.5147471523046, 28.5170136918687, 28.5182878702478,
28.5182979535264, 28.5194552147107, 28.5208744963843, 28.5208499857886,
28.5224323655125, 28.5237971288702, 28.5253429098379, 28.5263695153631,
28.5271258562564, 28.527248006475, 28.5282139926612), `007` = c(28.4428150935287,
28.4424508630218, 28.4417443567577, 28.4416646398661, 28.4417397473699,
28.4421390371037, 28.442120592066, 28.4423469529172, 28.4423356122218,
28.4426196264003, 28.4425488912568, 28.4422963064997, 28.4423101669536,
28.4423672400721, 28.4427664113, 28.4430845199499, 28.4432100343813,
28.4432239239436, 28.4435240263028, 28.4436080984706, 28.4432655222554,
28.44328479265, 28.4432176650369, 28.4433611850002, 28.4431608315413,
28.4429046633504, 28.4433578509172, 28.4433339312575, 28.4433658299195,
28.443586780443, 28.4441894718964, 28.4444519073077, 28.4446476391897,
28.4449171469023, 28.4452982751327, 28.4450025792219, 28.4452145723092,
28.4449115896032, 28.4450209283627, 28.4446694275982, 28.4443395262938,
28.4441176151897, 28.4439731216071, 28.44385022707, 28.4441792008458,
28.445123837753, 28.4453880241501, 28.4453299167678, 28.4458353149539,
28.4462543386658, 28.4457480468022, 28.4461112536262, 28.4463196297161,
28.4467044330388, 28.44669320099, 28.4467593880092, 28.4467913463193,
28.4468017142769, 28.4468247244756, 28.4475586978776, 28.4465573988107,
28.4470196343157, 28.4467977636148, 28.4467865543127, 28.4463450818671,
28.4463807551729, 28.4466755253629, 28.44699558456, 28.4476558489935,
28.447545647844, 28.4483749134677, 28.4487128468895, 28.4494698189587,
28.4525367472493, 28.4523834039983, 28.4533988713831, 28.4597114389207,
28.4601480695703, 28.4631900743702, 28.4706825902074, 28.4713645255192,
28.4722141080554, 28.4730032365892), `008` = c(28.4440779782437,
28.4505956863115, 28.4535932840915, 28.4535879092648, 28.4565387084024,
28.4572727573952, 28.4564823312937, 28.4569833241453, 28.4575960612924,
28.4575846529954, 28.4580553202582, 28.4579361864828, 28.4577914802344,
28.4580601711389, 28.4585436274683, 28.4587173788659, 28.4592142669674,
28.4593105825231, 28.4589760656734, 28.4588959957052, 28.459366652193,
28.459450169993, 28.4600372442301, 28.4605037909707, 28.4604667797696,
28.4599431386898, 28.4599317672, 28.4601142320655, 28.4596511161578,
28.459828961528, 28.4601144934852, 28.4604646354726, 28.4602984226369,
28.4602957824936, 28.4594927222365, 28.4592531932092, 28.4590869921369,
28.458955149021, 28.4590771281833, 28.4596811096851, 28.4597128453913,
28.4596671549211, 28.4597030042121, 28.4598593541125, 28.4600203119703,
28.4597982398213, 28.4594254267156, 28.4595474309644, 28.4595489395492,
28.4598950062382, 28.4603010783983, 28.4602554090402, 28.4602871699419,
28.4604178938905, 28.4604409569542, 28.460580385231, 28.4606121531285,
28.4605449626663, 28.4605423677549, 28.460311628625, 28.4601454853491,
28.4596008936146, 28.4602305747069, 28.459888050339, 28.4598424058183,
28.4604033554904, 28.4601941673923, 28.459808594271, 28.4600167754613,
28.4600270416968, 28.4602136976296, 28.4602326529227, 28.460780767236,
28.4610879112809, 28.4614596359738, 28.4615258161975, 28.4614371393666,
28.461701236826, 28.4619653365135, 28.4617992370959, 28.4616331402453,
28.461600380565, 28.4611933269593, 28.4606314160947), `009` = c(28.3884938819469,
28.3991291494431, 28.4065985628763, 28.4104173489313, 28.4146068255518,
28.4256803266826, 28.4238708919297, 28.4314322857144, 28.4393698362899,
28.4401635654092, 28.4416195512874, 28.443697308483, 28.4486024306673,
28.4548524832992, 28.4556046206078, 28.457456083197, 28.4585727177855,
28.4596101571606, 28.4596940627478, 28.4599201283979, 28.4637709580809,
28.4658190981975, 28.4675964656121, 28.4693941394046, 28.4731174250685,
28.4756174378251, 28.4784603727885, 28.4829153881772, 28.4844775260772,
28.4909182152346, 28.4949120278993, 28.4981211213728, 28.5005162490627,
28.5017114868563, 28.5020595549861, 28.5024981747026, 28.5026878645526,
28.5030027481228, 28.5029747624519, 28.502981364463, 28.5030736863932,
28.5034776833868, 28.5034959329191, 28.5038668436455, 28.5038850907069,
28.5041010936429, 28.5042610671501, 28.5042958012423, 28.5049188190567,
28.5051347905828, 28.5048234600677, 28.5045928631841, 28.5046457076724,
28.5048138909565, 28.5065226478816, 28.508447119696, 28.5119695430694,
28.5128591013956, 28.5140054138731, 28.5160870633279, 28.5179441352946,
28.5190661799043, 28.519936664811, 28.5207215309144, 28.5212739244757,
28.5216444991972, 28.5214006753231, 28.521643006578, 28.5231836365758,
28.5250152565693, 28.5254632426238, 28.526485444833, 28.5432251656557,
28.542529924166, 28.5335121158562, 28.5423678332577, 28.5559546601205,
28.5573166653937, 28.5581807264982, 28.5596006871624, 28.5596279220476
), `010` = c(28.474471643365, 28.4755344437003, 28.4755354352828,
28.4772207235164, 28.4837031924224, 28.4991628815303, 28.5054555296438,
28.5073623509313, 28.5086213293914, 28.5095143536491, 28.5104368771422,
28.5110116477836, 28.5117248857941, 28.511700110621, 28.5117792574717,
28.5120296704322, 28.5122403743187, 28.5125335930436, 28.5132334836023,
28.5137783964317, 28.5142081161052, 28.5143065792181, 28.5146861086325,
28.5151614146978, 28.5151926630474, 28.5150498659623, 28.5150995818892,
28.5147915053608, 28.5150713653184, 28.5156558678698, 28.5167056466776,
28.5162987439244, 28.5163513853399, 28.5188299654365, 28.5204640732977,
28.5212779965398, 28.5226422100855, 28.5229272240745, 28.5235995521255,
28.5250065215569, 28.5298969287621, 28.5330588589363, 28.5347301613804,
28.5363025743411, 28.5403108834867, 28.5448076615469, 28.5465426591561,
28.5533640823294, 28.5582304504621, 28.5648169441483, 28.5671395744164,
28.571040867585, 28.5720307990048, 28.5730846721842, 28.5731506190491,
28.5733928400798, 28.5735014186948, 28.5738559767499, 28.5739219342504,
28.5741854787987, 28.5745478175427, 28.5744588241025, 28.5750962296399,
28.5750072409194, 28.5748969428948, 28.5750190860188, 28.5749301053486,
28.5746357757441, 28.5746455924802, 28.574740658428, 28.5748841268116,
28.5752678021668, 28.5764069409072, 28.5772283918006, 28.57884014518,
28.5785536068131, 28.5781256673738, 28.5789044970138, 28.5789356375162,
28.5783178803397, 28.5787015620915, 28.5810338413594, 28.582194225733,
28.5817101405053, 28.582157724943)), .Names = c("001", "002",
"003", "004", "005", "006", "007", "008", "009", "010"))), .Names = c("julian",
"pot_density"), row.names = c(NA, 10L), class = "data.frame")
Can't do dput(dens[1:100,])
as the console won't contain it all unfortunately.
Thanks a lot in advance !