使用dplyr将列包括在数据框的第一位置

时间:2018-09-17 14:54:31

标签: r dplyr

我想将此Date列添加到数据框的第一位置:

我的专栏:

structure(c(16437, 16440, 16441, 16442, 16443, 16444, 16447, 
16448, 16449, 16450, 16451, 16454, 16455, 16456, 16457, 16458, 
16461, 16462, 16463, 16464, 16465, 16468, 16469, 16470, 16471, 
16472, 16475, 16476, 16477, 16478, 16479, 16484, 16485, 16486, 
16489, 16490, 16491, 16492, 16493, 16496, 16497, 16498, 16499, 
16500, 16503, 16504, 16505, 16506, 16507, 16510, 16511, 16512, 
16513, 16514, 16517, 16518, 16519, 16520, 16521, 16524, 16525, 
16526, 16527, 16531, 16532, 16533, 16534, 16535, 16538, 16539, 
16540, 16541, 16542, 16545, 16547, 16548, 16549, 16552, 16553, 
16554, 16555, 16559, 16560, 16561, 16562, 16563, 16566, 16567, 
16568, 16569, 16570, 16573, 16574, 16575, 16576, 16577, 16580, 
16581, 16582, 16583, 16584, 16587, 16588, 16589, 16591, 16594, 
16595, 16596, 16597, 16598, 16601, 16602, 16603, 16604, 16605, 
16608, 16609, 16610, 16611, 16612, 16615, 16616, 16617, 16618, 
16619, 16622, 16623, 16624, 16626, 16629, 16630, 16631, 16632, 
16633, 16636, 16637, 16638, 16639, 16640, 16643, 16644, 16645, 
16646, 16647, 16650, 16651, 16652, 16653, 16654, 16657, 16658, 
16659, 16660, 16661, 16664, 16665, 16666, 16667, 16668, 16671, 
16672, 16673, 16674, 16675, 16678, 16679, 16680, 16681, 16682, 
16686, 16687, 16688, 16689, 16692, 16693, 16694, 16695, 16696, 
16699, 16700, 16701, 16702, 16703, 16706, 16707, 16708, 16709, 
16710, 16713, 16714, 16715, 16716, 16717, 16721, 16722, 16723, 
16724, 16727, 16728, 16729, 16730, 16731, 16734, 16735, 16736, 
16737, 16738, 16742, 16743, 16744, 16745, 16748, 16749, 16750, 
16751, 16752, 16755, 16756, 16757, 16758, 16762, 16763, 16764, 
16765, 16766, 16769, 16770, 16771, 16772, 16773, 16776, 16777, 
16778, 16779, 16780, 16783, 16784, 16785, 16786, 16787, 16790, 
16791, 16792, 16797, 16798, 16799), class = "Date")

在数据框的第一个位置,我想使用dplyr在上面添加此列。我怎样才能做到这一点?有帮助吗?

这是我的数据框:

structure(list(X1 = c(11.3705103971604, 11.4361888772212, 11.3814847407673, 
11.5781572141443, 11.6650578877301, 11.4879322097833, 11.6200420427224, 
11.5903615117163, 11.6353980629399, 11.6100907116964, 11.4995114041115, 
11.6041635984648, 11.2632713603058, 11.1935706189825, 11.2491613185505, 
10.9509166019474, 11.059855755038, 11.0500265998461, 11.1613270602247, 
11.1878247601249, 11.3072178249885, 11.4800459859323, 11.481166655375, 
11.589453795433, 11.7301992076358, 11.959377208751, 12.0862999884254, 
12.0593358248124, 12.0570662521239, 11.8220000717191, 11.9692526167084, 
12.1200956540721, 12.1992004836068, 12.2953006030963, 12.128952571293, 
12.0192621774975, 12.1251045480836, 12.0796181134971, 11.7145120625843, 
11.9720220880464, 12.0274693779273, 12.1333961960742, 12.2505641248377, 
12.2495809491581, 12.5210981477534, 12.4117590145453, 12.4715940362019, 
12.689426181096, 12.710779926552, 12.7288035444279, 12.426123038212, 
12.3113983599355, 12.6811265371994, 12.5921703301545, 12.5234937782327, 
12.5039801760039, 12.547109102407, 12.6069368797858, 12.6575542098387, 
12.6161784520003, 12.520968121015, 12.3396781554382, 12.3599544550583, 
12.3409644645097, 12.350967777872, 12.1565773782866, 12.1802145928127, 
12.0629033652874, 12.1702606934192, 12.0332406636718, 12.0571079618491, 
12.1775259903687, 12.0924863192867, 12.0415365243111, 11.9828925176098, 
11.957289744376, 11.996578295047, 12.0684970531889, 12.1236685177622, 
12.1687109291109, 12.2380167587276, 12.3212369503793, 12.3959292115518, 
12.4765267894404, 12.2991302146491, 12.1100900185669, 12.2329462626582, 
12.245728859356, 12.2236869613787, 12.0496507473918, 11.9538344130964, 
12.0518118441532, 12.0388356197727, 11.95940039871, 11.8781434912756, 
11.6863310222235, 11.8330891280463, 11.9408823195422, 11.9939546065049, 
11.8093424044721, 11.8161988047125, 11.8425621852579, 11.8836928598404, 
11.9376231261628, 12.051635164315, 12.0067033930621, 11.9985234466249, 
12.1604752828667, 12.1158903898603, 12.0222498688407, 12.1457549918187, 
12.1197129087358, 12.0301837780239, 11.8404069031842, 11.8759914068277, 
11.8672841670315, 11.9470184983668, 11.9191605855402, 12.0790366785404, 
11.8783939697791, 12.0065345422085, 12.0237953581659, 12.0515852113422, 
11.9837731412004, 12.0904372476941, 12.2640054914237, 12.232165419304, 
12.3340209053759, 12.3628555899805, 12.3458381166001, 12.370391124419, 
12.284072426251, 12.3643829797571, 12.4731083575205, 12.4492502474543, 
12.178147682165, 12.3628868785554, 13.006681342065, 12.9138193629157, 
12.8072681667656, 12.8111916088397, 12.9938459349279, 13.0564578290981, 
13.0972221853556, 13.2518006131121, 13.2499049500157, 13.3669572583489, 
13.5818075702527, 13.6078112117406, 13.5372344728282, 13.6061775142769, 
13.4238513988049, 13.4322045442571, 13.5203244820638, 13.6806083054447, 
13.6978205689576, 13.8280098045951, 14.0391579601948, 14.1140682237891, 
14.1621687322518, 14.1221378817907, 13.9987123612292, 14.0148583851797, 
13.86877687275, 14.2634494722593, 14.4568682313658, 14.4429216916436, 
14.5099266860977, 14.8689717013479, 14.6877276965417, 14.5995643039012, 
14.8703789253299, 14.8692552510994, 14.8610844772401, 14.7479047245292, 
14.7768068880437, 14.9981106646785, 15.5614828364565, 15.4206284933885, 
15.7466735262007, 16.1607024675111, 15.5556259720179, 15.2569290278737, 
15.8227771253025, 15.6122544495591, 15.0431158432978, 15.0588972575848, 
14.7161148089248, 14.8329994505802, 14.8502222622877, 15.0831271722391, 
14.966193537687, 15.0831824732443, 15.3153152731595, 15.4928411485645, 
15.3850579804894, 15.4300386719528, 15.4524156422696, 15.45643234762, 
15.4030377141522, 15.5457154763358, 15.6664853571003, 15.6425929205804, 
15.4782405452126, 15.6024659177892, 15.6822485300339, 15.6368861456299, 
15.4336821690196, 15.2219652132957, 15.1934223994422, 15.3286917708909, 
15.3884502101958, 15.317148003051, 15.1326509110122, 15.0416673425379, 
15.0641080258579, 15.0331238411805, 15.0802297149824, 14.9900564032526, 
14.8262869444627, 14.864073314085, 14.810308045629, 15.0896177416021, 
15.0127102213002, 15.2487108709434, 15.2039367896072, 15.0829780767552, 
15.0193059314004, 15.0114376745095, 14.9018879151908, 15.0652848761905, 
14.9902358836699, 14.8817549416668, 14.8662625878514, 15.1084168950614, 
15.1011543074551, 15.1287660759837, 15.3116624769127, 15.1825949713451, 
15.478362732115, 15.6253305532077, 15.714627859823, 15.7145064858483, 
15.8767071671909, 15.8956642233257, 15.9862765528313), X2 = c(0.815985668583493, 
0.785901410160401, 0.883379439873731, 0.657393335260043, 0.551458670295994, 
0.797072901406409, 0.67471192056567, 0.725700146174797, 0.688141220613497, 
0.747334902457814, 0.860441089474532, 0.767301697836965, 1.16418940649154, 
1.2588049791726, 1.24231266579949, 1.56259358867134, 1.49882028870574, 
1.50094166106973, 1.37312644118948, 1.32855722603718, 1.22607669248767, 
1.06752243985254, 1.07859112450807, 0.988627652844425, 0.830897123409637, 
0.635557574070514, 0.455247192822684, 0.51966532179437, 0.608256100701481, 
0.913458989717903, 0.804365734451807, 0.601239945507128, 0.53505066803847, 
0.446971947637067, 0.641251354105971, 0.751009508162651, 0.667691594874206, 
0.737462506323697, 1.14985656420904, 0.928343921449438, 0.8858244507572, 
0.843021909459105, 0.747691564120232, 0.787880501146684, 0.642357777103769, 
0.768280611215045, 0.633815907877018, 0.453817506236916, 0.521446363065575, 
0.491111258258193, 0.842054670939854, 0.958669795902621, 0.539313994944375, 
0.591403125029089, 0.670331621708554, 0.666119653387939, 0.633420321332412, 
0.533269634917958, 0.484278900802123, 0.552312757406433, 0.651503658742762, 
0.784986300998917, 0.775385778140381, 0.806739097297595, 0.787619024841757, 
0.986290365334712, 0.934824334399632, 1.11672251320378, 1.00955056407273, 
1.22820200802087, 1.24787480682248, 1.1455647779077, 1.22847548573392, 
1.32942631880349, 1.44842014140406, 1.45434465089429, 1.43630719879515, 
1.39878945042714, 1.35511537080129, 1.36578451550709, 1.37798393655158, 
1.27110678925821, 1.20676571639615, 1.17220865352661, 1.40235750614943, 
1.65573400783318, 1.52915536895589, 1.5343772995027, 1.61256709526695, 
1.80477473508842, 1.94525137227224, 1.84731003508014, 1.89570584093209, 
1.99313980912788, 2.09838709913722, 2.32684288968914, 2.14780119265735, 
2.04494932980298, 2.00507498913038, 2.2594856896536, 2.30026848469754, 
2.31376987801377, 2.27985976961306, 2.23020757512109, 2.21856726609985, 
2.29563385520148, 2.34548257187368, 2.13262449147243, 2.2208321840111, 
2.35627766874734, 2.22078469615859, 2.23787016391177, 2.31787218155194, 
2.50843042659996, 2.4410566154186, 2.51812301987563, 2.41826757407123, 
2.51787413998356, 2.34952180685947, 2.61124200745753, 2.48723892712708, 
2.4845848556105, 2.44200162321643, 2.50280322974257, 2.40639878151601, 
2.27767679755103, 2.28769293708878, 2.12186454461728, 2.09972590257706, 
2.13100244818685, 2.12538471063302, 2.21532301990606, 2.11220877207614, 
1.99704622170493, 2.02702272124596, 2.33120843866794, 2.11265344415438, 
1.45533945262172, 1.78191367551797, 1.89245537772145, 1.88769351245611, 
1.71232862205787, 1.66235621986885, 1.66522223670439, 1.49115615210066, 
1.46073167323715, 1.28213876458404, 0.996122815028314, 0.985352246306041, 
1.01533581806752, 0.903736746616103, 1.10234663860326, 1.09658286842916, 
1.01382701327287, 0.816234302303281, 0.806800099861399, 0.649263135184672, 
0.461905036139408, 0.349169129162623, 0.248764061766764, 0.258646377961623, 
0.448644423025011, 0.410315625694414, 0.55354337045068, 0.155096962833478, 
-0.0864943451146184, -0.142318062260402, -0.238448895672311, 
-0.45589236560161, -0.287737402571902, -0.199380476802629, -0.409175714822407, 
-0.449704035603166, -0.504956959839686, -0.400378959181691, -0.443402678902397, 
-0.7107183573566, -1.20559113785042, -1.12445010183705, -1.48099115921181, 
-1.76984570914109, -1.23019671187626, -0.819077904818272, -1.30876087039307, 
-1.07296854408777, -0.544378111885714, -0.466980690445425, -0.159682574513794, 
-0.31754696219327, -0.461049965504113, -0.708519591336348, -0.689037282439877, 
-0.877912157657774, -1.06994763616467, -1.2874867264237, -1.24361129733061, 
-1.27238468348837, -1.33377980844151, -1.3566843228894, -1.24810275875976, 
-1.54272062266413, -1.63676829115661, -1.6093880001269, -1.40297356925055, 
-1.44641654248365, -1.6077361218866, -1.47646567959196, -1.23640359743279, 
-0.950682646435549, -0.955646897211958, -1.12773061966578, -1.16506873199113, 
-1.06759414291993, -0.884227392389801, -0.727240120087522, -0.786850429461588, 
-0.761805527377928, -0.816587263448955, -0.751634252437863, -0.607031341203412, 
-0.635505247821672, -0.543931565208979, -0.873970009893844, -0.787401671012984, 
-1.08832092409133, -1.01370514791325, -0.824973287398553, -0.711597621824593, 
-0.699301090710554, -0.574722469301763, -0.759937117387893, -0.674199898140996, 
-0.564580590144181, -0.555050147618914, -0.731044705391207, -0.772452427487537, 
-0.822179262510098, -1.03564286939129, -0.959669009617761, -1.30699259017452, 
-1.40442378868212, -1.52795375804993, -1.49216070853065, -1.63306509676206, 
-1.61123854411925, -1.65757288942859), X3 = c(3.61812048719847, 
3.21321611089539, 2.67984625287038, 2.40105084281996, 2.25489917251872, 
2.01033429785739, 2.1406774180125, 1.76433853912847, 1.59268111349162, 
1.3274642472015, 1.18925330129184, 1.40813368995165, 1.47400037895125, 
1.45143280880454, 1.51375853844365, 1.8227801842581, 1.53298208008111, 
1.54879967303179, 1.47595324505692, 1.4814548406281, 1.64766015676885, 
1.90262562552942, 1.4828826259998, 1.53705396000587, 1.49311980789129, 
1.17872006398186, 1.58277821163934, 1.91873511515087, 2.67493985419419, 
2.40080185998742, 2.01978480294705, 2.10504807034548, 2.10335487765146, 
2.2350739971641, 2.14749116675842, 1.78010891243133, 1.6010948003925, 
1.4956978862496, 1.34864604052943, 1.21261981118024, 1.08936240152483, 
1.25180419103097, 1.52947740024316, 2.2236182368064, 2.89786371851648, 
2.4361694638638, 2.49906245951698, 2.5690837794283, 2.44003362906361, 
2.4402672475223, 2.08579835156497, 1.72814708958874, 2.13389146268547, 
1.95979624768661, 1.7118493005833, 1.75699659513076, 1.51271139059297, 
1.77559719163497, 1.89168344637764, 1.70875889626376, 1.51404083947842, 
1.37068233100011, 1.3226355407526, 1.1802032755549, 1.02273506856884, 
0.838334752292243, 1.23631994788298, 1.01232614813328, 1.01415092025376, 
0.687791493399658, 0.4765836155068, 0.518694924317016, 1.20204520329455, 
1.59408506425551, 1.48794584451402, 1.66083333577911, 1.80752625269501, 
1.0637935949244, 1.04224177449975, 0.594347384374145, 1.1205941847678, 
1.30478039541776, 1.09893016866259, 0.916608523239554, 1.40612343996539, 
1.09253573163983, 1.09353530046924, 1.09450431113696, 0.850915590311249, 
0.797772196299923, 0.571119160025547, 0.756206963566258, 0.664875632356571, 
0.309790294490824, 0.39412822103421, 0.321032612885346, 0.251274265704359, 
0.0310048248233358, 0.150011763125327, 0.0557957293108197, 0.0766972404107198, 
0.215745477640819, 0.444575310561606, 0.547564932103394, 0.391486716129929, 
0.0681459463644024, -0.0184535817470765, 0.575448087545443, 0.942657966653605, 
0.81339119650238, 1.23410342973546, 1.05948542436112, 1.08500072189548, 
1.44687376298398, 1.63546219877473, 1.04226937831662, 0.714991930432878, 
0.839701794757963, 1.04249351087901, 1.06530168784771, 0.803948993750917, 
0.495769202659063, 0.714272509336469, 0.210769435993214, -0.269097084326521, 
-0.580474707257837, -0.548713922956807, -0.694743000683138, -1.04761790447818, 
-1.0514764224088, -1.23866128729851, -1.10149297666572, -1.06078702041169, 
-1.49473012856572, -1.84522154917183, -1.66590257018975, -1.77221241306122, 
-1.45586690255273, -1.21432622509763, -1.01932761903749, -0.844873554055134, 
-1.37126606257236, -2.50545285838942, -2.97549504612003, -2.56960715299618, 
-2.41818338885556, -1.82322452727245, -0.302138937038413, 0.079771956601265, 
-0.335293350565757, -0.793985955658979, -1.15501138412697, -0.978247100803156, 
-1.25391489103657, -1.2305419068899, -1.40016781531862, -1.63109506063133, 
-2.33863607246429, -2.04815064424975, -1.00706529603741, -0.718896074983893, 
-1.22806527292674, -1.47388153236977, -1.01683988408215, -0.576887848264937, 
0.0997206923601024, 1.68061851751311, 1.19589323743297, 1.42567208517679, 
1.11954022869363, 1.24958300937863, 2.03555868470092, 1.83915094947541, 
1.24948761619389, 1.6639008523373, 1.76262685263685, 1.98732248049318, 
2.40268542333395, 3.30390656314326, 3.64450242687766, 5.15197804783305, 
3.59520251656408, 3.35123895882765, 4.40789859420344, 3.69742983076253, 
3.44763587179928, 3.62901045316789, 2.79702419488618, 2.65412336976005, 
2.46593509824459, 3.00896196591254, 2.73970118371967, 4.02182775458112, 
4.38588764000312, 3.76436370427298, 3.18597323986389, 3.22137039152941, 
3.46917065082801, 3.24059777758853, 3.57197787769625, 3.27986611696537, 
3.39879788899194, 3.30885708289768, 2.94722512916036, 3.06169806574464, 
3.19697334435449, 3.25528194663563, 2.95333467817125, 2.7856172702605, 
3.09974236067634, 2.94760225373672, 3.10804225115366, 3.3262121372854, 
3.42532725671773, 3.44196314886506, 3.67744825478467, 3.61248459732837, 
3.51949068898003, 3.50532494219991, 2.97589613964816, 2.53766401322434, 
2.8659330352424, 3.12171151443948, 3.96027083989438, 4.51508843598857, 
4.85580067313517, 4.53494890060225, 4.10887726416968, 4.43333649499594, 
4.58098904301694, 4.44746062585927, 4.62480412143571, 4.5468258863183, 
5.57841085681441, 5.16061576372042, 5.65985257340728, 5.56880924585225, 
5.81942302339051, 5.52052906840443, 5.43721169933826, 5.9518432349241, 
5.66855679542385, 5.2329982064256, 4.76942707498383, 4.7989238005975, 
4.72604921707455)), row.names = c(NA, 246L), class = "data.frame")

我一直在尝试使用mutate函数,但是它不起作用。当我刚开始使用dplyr软件包时,我一直在尝试使用此特定的软件包进行编码。

有帮助吗?

2 个答案:

答案 0 :(得分:4)

bind_cols来自dplyr软件包:

library(dplyr)
data.frame(date)  %>% bind_cols(data.frame(df))

给你...

      date       X1          X2          X3
1   2015-01-02 11.37051  0.81598567  3.61812049
2   2015-01-05 11.43619  0.78590141  3.21321611
3   2015-01-06 11.38148  0.88337944  2.67984625
4   2015-01-07 11.57816  0.65739334  2.40105084

答案 1 :(得分:4)

我们可以使用add_column中的tibble根据列名创建列

library(tidyverse)
add_column(df, dates, .before = 'X1')

或基于索引

add_column(df, dates, .after = 0) %>%
    head
#     dates       X1        X2       X3
#1 2015-01-02 11.37051 0.8159857 3.618120
#2 2015-01-05 11.43619 0.7859014 3.213216
#3 2015-01-06 11.38148 0.8833794 2.679846
#4 2015-01-07 11.57816 0.6573933 2.401051
#5 2015-01-08 11.66506 0.5514587 2.254899
#6 2015-01-09 11.48793 0.7970729 2.010334