基于值在三个子帧中划分数据帧

时间:2015-09-09 14:25:37

标签: r

我有一个如下数据框。我想把它分成平均row.values的三个子矩阵。基本上我需要先计算平均row.value,然后根据ID和平均row.value划分数据框。有没有办法直接在一个命令中执行此操作?

dput(头(matrix_cpm_spike_norm_mature,6))

structure(c(3.60338983255681, 2.60614455986074, 7.38026015357423, 
0.485930325361538, 10.0728954273074, 11.2022336797709, 2.3081852592818, 
1.92925594571705, 4.42105830784866, 1.97655358070322, 11.4414272084792, 
10.5245987429194, 3.79928941804137, 1.43988964742944, 2.43182718729225, 
1.85054706660746, 10.6157635199152, 9.93246596282924, 1.76499400158053, 
0.867000595584972, 4.17937486393757, 1.62313934970233, 10.9760540633484, 
11.4384402909535, 3.12565733850373, 1.02306009758869, 6.15822532096255, 
1.45585403170949, 11.1021517437179, 10.9107307780349, 1.84965418527454, 
1.57953239648466, 3.92960542264649, 0.968010692772174, 11.0236055961936, 
10.5913003928287, 4.16812971357324, 2.58284099400479, 5.77874217548982, 
2.71612416959378, 11.6945755132658, 12.2125038706251, 2.28044423317132, 
2.28044423317132, 4.20181017967427, 1.93489752637374, 12.0201280590592, 
11.4757936894966, 3.29573463740003, 3.1840621322311, 5.6701373082146, 
1.96630490538038, 10.7374381660113, 13.2995125381539, 2.91175170429137, 
2.1569190504634, 5.20328658455966, 2.1569190504634, 10.2217040585341, 
9.83559070490264, 3.93309437957696, 2.51818736936141, 7.40221631369804, 
0.335828139281744, 10.56613413102, 10.6619866025163, 2.27796568023663, 
1.66056419670379, 5.29496497910422, 0.699488415484219, 10.310084339381, 
9.75424847574386, 3.64268206121968, 2.37908914926602, 8.24035280411754, 
1.86474527822428, 8.96352397286089, 10.2227828989983, 2.33835251965787, 
2.06871044842623, 4.5954355631791, 1.01279126170234, 12.3601973455172, 
11.4656151558222, 2.44441563566059, 2.40421119777587, 6.02973594460351, 
1.52508878726709, 10.4309455175175, 9.04837239279591, 2.19268157249804, 
1.24045869709133, 3.09706859211401, 1.13115919913948, 10.7933496311904, 
9.9695518430138, 2.92171136565383, 0.408245399247249, 2.04506253637125, 
1.21781706285458, 11.799265629373, 10.0589147547578, 2.01873582708745, 
1.46069392909814, 3.53893363843314, 1.196243674246, 11.337876323565, 
11.2124904638672, 2.52480547770426, 1.46864391283124, 4.93028527141748, 
0.732496016009805, 10.1639221283662, 9.76884595465318, 1.44174154410475, 
1.64024858387856, 5.63721432238654, 1.72991175679785, 11.1211504054224, 
10.6024617121637, 3.22964454373919, 1.02206068958127, 6.97852650980799, 
1.30645573502801, 12.8991256257565, 13.0000814142899, 2.826661275181, 
1.2548377150201, 4.36474368941959, 2.12408033247534, 12.4705231651432, 
11.9618071621679, 2.69847153335308, 2.27631143253466, 4.20951126623905, 
-0.0664422382974684, 8.98849795367716, 9.41468863016541, 2.43225940352207, 
2.09595624532069, 4.63715429950104, 1.25491429244567, 10.9872971587847, 
10.4925408800726, 3.45374293794281, 3.13429367766325, 7.39158804595222, 
1.54024232057877, 10.8542522087055, 10.6155963202093, 2.69223465587794, 
0.809912072570472, 4.64506635251942, 1.6137442296552, 11.418571423468, 
12.2726701702859, 2.77267844825925, 1.78512859921307, 9.14121538410264, 
-4.43688074939158, 9.73718972990295, 10.2957742347039, 1.49118640848005, 
1.5813150312641, 4.0667005226673, 0.59204571815906, 10.0375004920899, 
9.54533950930523, 2.16345862609174, 3.05436004610877, 8.07790690254631, 
1.79368307261622, 12.3560914214054, 13.0051227140041, 2.58124788178095, 
1.88617681546547, 2.73360657353678, 1.77592657048602, 10.9528780864007, 
10.4348473225329, 3.00497812997592, 1.7631510316129, 5.90700548290542, 
0.877605937609275, 9.87041660889873, 9.7614993662457, 2.05885671172016, 
1.76410360006183, 6.3570619407495, 1.76410360006183, 11.685705297838, 
11.3602560106241, 2.82800833596723, 2.03247959283134, 6.95870308054991, 
1.27136597104121, 10.8372565515699, 9.81173432822107, 2.98238612294936, 
1.83072752194121, 5.68689117080157, 1.16721273421709, 12.1340028664354, 
11.3680995516162, 3.39653560487816, 2.69687166650826, 6.48412064518055, 
1.29454179328719, 10.0028066556529, 10.512440462176, 1.4596941835747, 
0.727974609981551, 3.88239656448561, 0.800353161112983, 10.6363990565411, 
10.1243371999547, 3.3438733844774, 0.697114163823588, 8.47892011973568, 
0.697114163823588, 10.6855866660417, 10.6118837404499, 2.13674190430653, 
1.1527414454512, 4.18464270261989, 1.27853746250223, 10.7320904309318, 
9.89189546554829, 3.823237482187, 0.874031743370891, 1.95508453030069, 
1.87846554046761, 9.88713195004868, 9.86183499157279, 2.53517059781643, 
0.728446508864785, 3.50839578521561, 1.9965292874828, 10.9487017925789, 
10.9382935538901, 2.62649833974985, 1.92098108152647, 3.36108007152541, 
0.488100205530239, 10.4894419819966, 10.2226664777547, 1.33967056255352, 
0.0917001628908314, 3.81741552255672, 2.03856822618478, 12.2392733147047, 
11.4938478602584, 3.25046552747648, 1.48275456354609, 4.81128734606174, 
1.96154924623549, 12.1358994156854, 11.4729501435715, 2.22982247734829, 
1.68173260626794, 5.2904025048379, 1.68173260626794, 11.5137150099059, 
11.010850144881, 3.91979174751635, 2.17257846100131, 3.53963311590271, 
1.6107186758943, 11.4951784786694, 10.1260992493434, 2.35254220581856, 
1.26444532526964, 4.6646553023283, 1.17529994013973, 10.9857870198993, 
10.5814974795421, 3.75054671795939, 3.03029499770489, 9.00153489042535, 
3.27015511904915, 11.5616951026666, 10.6545660172833, 2.18325656314713, 
1.50344897782345, 3.97329240591904, 1.50344897782345, 11.3467527232018, 
10.707768513033, 2.7932432629389, 1.70473241476131, 5.05340701550577, 
0.547092257741687, 11.7624630621943, 9.91053139355945, 2.80574376605516, 
1.63874222935751, 3.92143952780207, 1.834040048536, 11.4458009229089, 
10.9155922874574, 2.81245930319929, 1.47154823046698, 4.17913709197299, 
0.836392585913434, 12.5796307717185, 11.6787505410903, 1.7257545501894, 
0.949499417203444, 5.34316953950321, 1.63528985628147, 12.2668256759477, 
11.696888238442, 2.43827755616949, 1.81088892324984, 5.13790770293989, 
1.62918292819459, 10.2635519811317, 10.3028832599596, 2.07789375455372, 
1.08266801489579, 3.36924723755809, 0.361290751749976, 9.978530962904, 
9.62993954501451, 3.46479867592192, 2.12156177109216, 8.78601850725548, 
1.28447344131806, 11.5362902737332, 11.569001443705, 1.88432186226604, 
0.738852102905189, 3.93512805821853, 2.28505136817535, 12.2366325701317, 
11.5419247012961, 2.72781971767877, 0.981047395388437, 2.42660975369917, 
1.39151946304238, 9.47444971798845, 10.1389766220257, 1.9620421090479, 
1.31100308652463, 3.9625997590393, 1.31100308652463, 11.0538785110822, 
10.5756552873608, 2.24522309004463, 1.98668802513104, 4.91591774317019, 
1.41382278476808, 10.9307830177181, 9.75061291019652, 2.89312477978526, 
1.41138577836645, 5.3114727278592, 1.18511963455519, 12.8070122897496, 
11.9900344090709, 3.27111851368705, 0.108008163568448, 3.27111851368705, 
1.04663775582004, 10.5015959502624, 10.9439135408891, 1.96611160593682, 
1.75218878170152, 1.96611160593682, -0.675585494894112, 11.1733364353258, 
10.5537870233171, 2.76365773789404, 2.24622484994411, 4.82500775257594, 
0.859189165848259, 11.3729184708685, 10.4792280890542, 2.2746810191026, 
1.26862765095299, 3.1182053649793, 1.35619562745757, 11.541422194603, 
11.0876007441211, 2.93955281780494, 2.41792430951958, 7.43488775671718, 
1.59455665002896, 10.8522501620988, 10.7066940648954, 2.56175708272954, 
1.63789352792862, 5.62616733093608, 1.77321879142546, 11.5341769241796, 
11.3614738869516, 3.52512139810446, 2.04455677275519, 9.07892025450834, 
1.80422229584867, 12.2658423589039, 12.7955854152128, 2.41662572851679, 
0.739356302189606, 6.58079778253096, 2.2056509250739, 11.4453206203817, 
11.2577755286648, 4.43488006430324, 2.47577649074622, 7.97026648668841, 
2.47577649074622, 10.5347452823169, 10.9336199647057, 0.253851439446271, 
1.33349021364545, 1.90668843852765, 1.38668924767966, 11.529988569981, 
9.68152140791945, 2.69321031115829, 1.69780604669796, 9.9826122018356, 
-0.0693106968443571, 11.2474341893491, 10.2586104182553, 2.30251678501683, 
1.67135129150304, 5.1970813744178, 1.88064524158918, 11.4072019068968, 
10.9762764532365, 2.73053313404777, 2.92486117981077, 9.751901699517, 
2.31573050368619, 11.0745724079847, 11.5397308498818, 1.85114298699175, 
1.74446232598571, 4.67405729933025, 1.36771765795199, 11.1449736215866, 
10.6467965079355, 3.12408501589049, 2.00482310148083, 4.90897925267742, 
0.753058050023766, 10.3589506698824, 10.3513173985724, 2.72147165969401, 
1.46186080167698, 3.9354821726169, 1.23699756995357, 11.1881701541595, 
10.931341142772, 2.99260441125988, 2.23826963603629, 3.87458928218141, 
0.65507014009726, 9.74342426391136, 10.8918928191143, 2.78046014914365, 
0.505490227190171, 4.43327034269568, 1.9329054288394, 11.6078716913683, 
10.9011106771378, 2.92822672503857, 1.76286882344236, 6.82012516033837, 
1.49833750295022, 9.57121023826284, 8.39324323219324, 2.81052780333747, 
1.40588646539131, 3.18875457138522, 1.18481091851206, 11.462231229116, 
10.5770583597432, 3.30911666206261, 1.33469191841737, 4.96029091396055, 
2.17413398672633, 10.5583178628059, 10.0329059364534, 1.88186145438922, 
0.989773711428552, 5.0108103947152, 0.769365496730378, 11.7867744116038, 
11.2871021676365, 2.88127273360004, 1.87947399714945, 2.88127273360004, 
1.92593791384916, 9.46125879965226, 9.29462968277333, 2.36391085778722, 
1.74079268896779, 3.93604270788606, 1.34641095936402, 10.2068762533305, 
10.0118036485061, 2.57361661875718, 2.03528106162882, 4.33420919282757, 
1.1624557079541, 11.0754175076428, 11.0622083554009, 1.93155319127651, 
1.52032894976282, 4.46324520367219, 1.52032894976282, 11.3476141375944, 
10.4138620742203, 3.85081675794147, 2.74371623100224, 1.11770920823725, 
1.98482146279348, 12.566434472332, 11.9424300470872, 2.53925962537711, 
1.48382075377967, 4.87249055114745, 1.07284442662947, 11.7111768375923, 
11.5795125663534, 2.15535566170556, 1.6234973786264, 7.72868868952004, 
0.562556076894618, 9.85480133708155, 9.92934562704339, 2.54728578873058, 
1.8265104903494, 3.76801234500258, 1.2821466213423, 11.2568527058396, 
10.8343412926481, 3.05211979795212, 0.152264838074353, 2.54493306112919, 
1.77792835724608, 11.5338955198532, 9.97573017264385, 2.42059823105591, 
0.60936602148068, 3.12812928480832, 1.14355342605761, 11.5524961118039, 
10.5285363661307), .Dim = c(6L, 92L), .Dimnames = list(c("hsa-miR-671-5p", 
"hsa-miR-3909", "hsa-miR-1247-5p", "hsa-miR-628-3p", "hsa-miR-23b-3p", 
"hsa-miR-127-3p"), c("100G", "100R", "106G", "106R", "122G", 
"122R", "124G", "124R", "126G", "126R", "134G", "134R", "141G", 
"141R", "167G", "167R", "185G", "185R", "192G", "192R", "235G", 
"235R", "239G", "239R", "243G", "243R", "246G", "246R", "261G", 
"261R", "267G", "267R", "26G", "26R", "270G", "270R", "279G", 
"279R", "299G", "299R", "301G", "301R", "305G", "305R", "342G", 
"342R", "350G", "350R", "356G", "356R", "35G", "35R", "361G", 
"361R", "366G", "366R", "367G", "367R", "377G", "377R", "379G", 
"379R", "388G", "388R", "400G", "400R", "402G", "402R", "46G", 
"46R", "48G", "48R", "55G", "55R", "57G", "57R", "60G", "60R", 
"68G", "68R", "70G", "70R", "73G", "73R", "77G", "77R", "82G", 
"82R", "93G", "93R", "94G", "94R")))

1 个答案:

答案 0 :(得分:4)

我想在获得cut

之后我们需要split matrix rowMeans v1 <- rowMeans(m1) v2 <- cut(v1, breaks=3) v2 #[1] (5.52,8.48] (11.4,14.4] (5.52,8.48] (11.4,14.4] (5.52,8.48] (5.52,8.48] #Levels: (5.52,8.48] (8.48,11.4] (11.4,14.4]
split

我们list矩阵的行序列(&#39; m1&#39;)by&#39; v2&#39;,将&#39; m1&#39;的行子集。基于行索引创建3个矩阵的 lst <- lapply(split(1:nrow(m1), v2), function(i) m1[i,])

console.log = function(str){

  return str.split('').reverse().join('');

}