我对R(以及本网站)很新,我遇到了一个简单的问题,但我还没有找到解决方案。 我研究的数据集包含在几个日期水柱内CTD测量的潜在密度值。我想在数据框中按日期对这些度量进行分组。
我在开始时拥有的内容:
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
初始“ctd”数据框的示例(在添加如上所示的julian天之后):
id station date ... pot_density julian
B12171_01.cnv B+ 19/06/2012 ... 28.73863 32790
现在我想获取“pot_density”值(每个日期有几个,即数据框中的“日期”和“julian”相同)并将它们存储在新的数据框中,因此julian天成为行和列由当天/行的每一个“pot_density”测量值组成。
dens<-data.frame()
dens<-aggregate(pot_density ~ julian, data=ctd, FUN=paste)
现在我已进入“窝点”数据框:
julian pot_density
27548 c("28.4698015312665", "28.4704581365093", "28.4706453687...)
... ...
我现在想要将每个行的“pot_density”值取消为n列,这就是我被困住的地方。我需要能够在每个采样日期应用检测pycnocline的函数,并绘制密度线的年际变化...
我尝试了几个功能:
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)
请注意事先装入所需的包装;我在论坛上搜索了一个类似的问题,但是找不到任何相关的主题(或者无法理解它与我自己的问题类似,再次我是R的新手并且在编码方面一般都不是很熟练)。
我是否遗漏了任何可以完全按照我要做的功能?我没有正确输入上述函数的参数吗?
> 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")
无法执行dput(dens[1:100,])
,因为不幸的是控制台不会包含它。
提前多多感谢!
答案 0 :(得分:3)
使用dplyr
可以通过每个日期/朱利安的第一次分组来执行一列到多列分割, 用逗号分割,取消列出并转换为data.frame row
DF=data.frame(julian=27548,pot_density=paste(c(28.4698015312665,28.4704581365093,28.4706453687),collapse=","),stringsAsFactors=FALSE)
DF
# julian pot_density
#1 27548 28.4698015312665,28.4704581365093,28.4706453687
newDF=DF %>%
dplyr::group_by(julian) %>%
dplyr::do(.,data.frame(matrix(unlist(strsplit(.$pot_density,",")),nrow=1)) ) %>%
as.data.frame()
newDF
# julian X1 X2 X3
#1 27548 28.4698015312665 28.4704581365093 28.4706453687
更新了数据集:
对于更新的数据集,您可以在dplyr::do
函数中使用unlist来实现所需的矩形data.frame结构
请注意,由于10个向量的长度不同,因此在某些列中会有NA值作为结果和数量 列最多为10个向量的长度,在这种情况下为85
sapply(dens$pot_density,length)
# 001 002 003 004 005 006 007 008 009 010
# 82 83 83 84 84 84 83 84 81 85
newDens=dens %>%
dplyr::group_by(julian) %>%
dplyr::do(.,data.frame(matrix(unlist(.$pot_density),nrow=1)) ) %>%
as.data.frame()
head(newDens)
# julian X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14
# 1 27548 28.46980 28.47046 28.47065 28.47137 28.47160 28.47132 28.47194 28.47164 28.47194 28.47244 28.47294 28.47309 28.47288 28.47310
# 2 27555 28.38543 28.38806 28.39109 28.40103 28.41289 28.42243 28.43977 28.44522 28.44582 28.44576 28.44620 28.44655 28.44736 28.44999
# 3 27562 28.42227 28.42382 28.42443 28.42479 28.42547 28.42512 28.42507 28.42421 28.42399 28.42443 28.42443 28.42527 28.42468 28.42435
# 4 27569 28.36748 28.36746 28.36878 28.37012 28.37175 28.37264 28.37423 28.37468 28.37515 28.37497 28.37476 28.37457 28.37728 28.37809
# 5 27576 28.45268 28.45358 28.45255 28.45186 28.45280 28.45318 28.45289 28.45554 28.45886 28.46282 28.47461 28.47840 28.48306 28.48473
# 6 27583 28.38106 28.38130 28.38171 28.38312 28.38544 28.38715 28.38908 28.39021 28.39180 28.39539 28.39802 28.40041 28.40451 28.40721
# X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29
# 1 28.47376 28.47400 28.47423 28.47386 28.47432 28.47427 28.47501 28.47566 28.47508 28.47558 28.47528 28.47529 28.47534 28.47544 28.47639
# 2 28.45058 28.45241 28.45280 28.45279 28.45242 28.45243 28.45257 28.45329 28.45320 28.45371 28.45462 28.45493 28.45548 28.45603 28.45587
# 3 28.42487 28.42552 28.42493 28.42470 28.42469 28.42460 28.42477 28.42491 28.42480 28.42508 28.42372 28.42351 28.42387 28.42422 28.42406
# 4 28.37909 28.38681 28.39588 28.39948 28.39925 28.40067 28.40377 28.40711 28.40835 28.40862 28.40971 28.40957 28.40982 28.41094 28.41169
# 5 28.48765 28.48769 28.48837 28.48860 28.48843 28.48838 28.48861 28.48836 28.48877 28.48880 28.48866 28.48823 28.48845 28.48855 28.48994
# 6 28.40871 28.41264 28.41362 28.41674 28.42094 28.42703 28.43125 28.43744 28.44327 28.44861 28.45126 28.45242 28.45129 28.45123 28.45237
# X30 X31 X32 X33 X34 X35 X36 X37 X38 X39 X40 X41 X42 X43 X44
# 1 28.47697 28.47747 28.47791 28.47790 28.47803 28.47818 28.47809 28.47788 28.47792 28.47816 28.47811 28.47780 28.47793 28.47787 28.47827
# 2 28.45652 28.45807 28.45933 28.45977 28.46019 28.46051 28.46100 28.46136 28.46127 28.46158 28.46191 28.46233 28.46503 28.46818 28.46907
# 3 28.42378 28.42353 28.42367 28.42346 28.42334 28.42329 28.42322 28.42341 28.42340 28.42404 28.42429 28.42447 28.42436 28.42467 28.42444
# 4 28.41317 28.41636 28.42153 28.42233 28.42240 28.42396 28.42525 28.42537 28.42547 28.42604 28.42650 28.42772 28.42909 28.43003 28.43136
# 5 28.48980 28.49084 28.49092 28.49133 28.49152 28.49214 28.49238 28.49239 28.49209 28.49234 28.49233 28.49241 28.49260 28.49329 28.49381
# 6 28.45339 28.45486 28.45551 28.45618 28.45713 28.45885 28.46112 28.46298 28.46378 28.46390 28.46541 28.46610 28.46873 28.47033 28.47103
# X45 X46 X47 X48 X49 X50 X51 X52 X53 X54 X55 X56 X57 X58 X59
# 1 28.47823 28.47792 28.47776 28.47791 28.47761 28.47762 28.47767 28.47726 28.47711 28.47687 28.47735 28.47686 28.47717 28.47754 28.47722
# 2 28.46970 28.46996 28.47060 28.47118 28.47135 28.47296 28.47468 28.47556 28.47722 28.47915 28.47984 28.48097 28.48108 28.48111 28.48129
# 3 28.42407 28.42406 28.42438 28.42469 28.42493 28.42558 28.42621 28.42715 28.42722 28.42752 28.42765 28.42871 28.43017 28.43179 28.43443
# 4 28.43191 28.43311 28.43480 28.43596 28.43673 28.43921 28.44073 28.44223 28.44265 28.44352 28.44448 28.44501 28.44557 28.44542 28.44716
# 5 28.49392 28.49363 28.49379 28.49324 28.49351 28.49468 28.49558 28.49573 28.49635 28.49718 28.49854 28.49863 28.49868 28.50034 28.50022
# 6 28.47487 28.47718 28.48008 28.48167 28.48309 28.48441 28.48660 28.48846 28.49091 28.49313 28.49472 28.49529 28.49647 28.49742 28.49801
# X60 X61 X62 X63 X64 X65 X66 X67 X68 X69 X70 X71 X72 X73 X74
# 1 28.47667 28.47667 28.47652 28.47673 28.47643 28.47634 28.47668 28.47667 28.47647 28.47646 28.47658 28.47668 28.47670 28.47662 28.47669
# 2 28.48107 28.48153 28.48151 28.48126 28.48123 28.48145 28.48140 28.48129 28.48126 28.48143 28.48146 28.48141 28.48154 28.48136 28.48149
# 3 28.43473 28.43517 28.43597 28.43618 28.43668 28.43699 28.43688 28.43797 28.43824 28.43870 28.43863 28.43913 28.44231 28.44498 28.44527
# 4 28.44915 28.45137 28.45238 28.45239 28.45248 28.45288 28.45327 28.45397 28.45462 28.45597 28.45791 28.45879 28.46073 28.46372 28.46587
# 5 28.50005 28.49965 28.50040 28.50208 28.50291 28.50319 28.50352 28.50502 28.50531 28.50528 28.50728 28.50742 28.51176 28.51329 28.51406
# 6 28.49889 28.49929 28.50054 28.50253 28.50502 28.50801 28.51164 28.51209 28.51328 28.51400 28.51412 28.51475 28.51701 28.51829 28.51830
# X75 X76 X77 X78 X79 X80 X81 X82 X83 X84 X85
# 1 28.47676 28.47684 28.47698 28.47675 28.47646 28.47656 28.47680 28.47696 NA NA NA
# 2 28.48138 28.48152 28.48168 28.48117 28.48099 28.48080 28.48079 28.48080 28.48076 NA NA
# 3 28.44555 28.44556 28.44747 28.44876 28.44821 28.44815 28.45076 28.45319 28.45599 NA NA
# 4 28.46681 28.46803 28.46890 28.46963 28.47142 28.47305 28.47342 28.47462 28.47682 28.47908 NA
# 5 28.51426 28.51492 28.51440 28.51432 28.51416 28.51403 28.51449 28.51467 28.51461 28.51462 NA
# 6 28.51946 28.52087 28.52085 28.52243 28.52380 28.52534 28.52637 28.52713 28.52725 28.52821 NA
答案 1 :(得分:1)
尝试通过对代码进行最少的更改来解决您的问题:
ctd <- data.frame(pot_density = c(28.7, 27, 26, 25, 24), julian = c(rep(32790, 2), rep(27548, 3)))
dens <- aggregate(pot_density ~ julian, data = ctd, FUN = paste, collapse = "/")
tidyr::separate(dens, pot_density, sep = "/", into = paste0("pot_density.", 1:3))
虽然您可能不应该首先使用paste
(数字到字符转换可能不是您想要的)。