我正在尝试将数据行插入一列并添加缺少的行名称。
下面使用dput()
包含数据。有一列包含行号,另一列包含概率。 head()
和tail()
位于下方。我正在尝试cbind
此输出到另一个包含所有行(1:1859)的data.table(未包含)并且想要插入缺少的数据而不是重新排序行名称。
虽然merge()
也可能合适,但我认为添加缺失数据(如下所述)是最佳选择。
data <- structure(c(0.447578502202603, 0.0377643922223044, 0.293829065219696,
0.247731422173557, 0.258454182579447, 0.370728239680138, 0.428982119725404,
0.970982798568656, 0.476951492304074, 0.0615086640134777, 0.0392505025014708,
0.546527152105146, 0.0736894630543969, 0.0377643922223044, 0.0638671437811352,
0.734381095218283, 0.0377643922223044, 0.0338509156119364, 0.231359441488042,
0.0338509156119364, 0.04619620544431, 0.999572420447029, 0.0673439981500492,
0.08618274906934, 0.999644103069121, 0.131956884728731, 0.999983792297403,
0.660168012045625, 0.390705302475554, 0.0377643922223044, 0.0552759476600101,
0.0414465870459318, 0.201360905713179, 0.839342408810146, 0.0615086640134777,
0.931569705880893, 0.4180243177983, 0.738789959119669, 0.835133795402349,
0.043759947041066, 0.454885741938136, 0.04619620544431, 0.251883463316181,
0.811927454514953, 0.058575107958477, 0.997171202614914, 0.244653304575698,
0.188230945092145, 0.935828195624839, 0.285569077721315, 0.04619620544431,
0.0766469090759971, 0.994245893309182, 0.447027271738386, 0.3225305943877,
0.972375288448514, 0.891106943907199, 0.575627360054002, 0.04619620544431,
0.4317288379505, 0.0414465870459318, 0.992759922251054, 0.0338509156119364,
0.0615086640134777, 0.718299686119277, 0.872478318264161, 0.804458697603663,
0.940024143413868, 0.0751585335541173, 0.04619620544431, 0.926209597279401,
0.04619620544431, 0.0818186063327247, 0.377287322854212, 0.0907566629876851,
0.996113282860611, 0.125593391286759, 0.92373131289081, 0.941035250522889,
0.090325139971336, 0.943334232863117, 0.109671739827895, 0.998542976682527,
0.109671739827895, 0.0392505025014708, 0.889203370047101, 0.994268372555809,
0.999343060502805, 0.0907566629876851, 0.0748296820784503, 0.892525104393848,
0.999778068669299, 0.0414465870459318, 0.999984581697105, 0.0564002780597096,
0.4180243177983, 0.0615086640134777, 0.0564002780597096, 0.975214436029541,
0.0651541701402252, 0.999986112592448, 0.0685414676275887, 0.998977350763494,
0.043759947041066, 0.980634462234512, 0.0615086640134777, 0.0748296820784503,
0.04619620544431, 0.0976031677612942, 0.919233160408482, 0.837212708038958,
0.0709957739192715, 0.0392505025014708, 0.996022681479465, 0.0673439981500492,
0.999955192558713, 0.864341255293794, 0.734971575445267, 0.0638671437811352,
0.0709957739192715, 0.04619620544431, 0.0907566629876851, 0.999998458689101,
0.314322859396997, 0.0392505025014708, 0.999801201200068, 0.0454708459833313,
0.991789969855556, 0.752688915960774, 0.99769572871491, 0.99382173976708,
0.201360905713179, 0.999513222571325, 0.0748296820784503, 0.0615086640134777,
0.0377643922223044, 0.468735871033994, 0.398450495507929, 0.746838749209026,
0.754076861680862, 0.993134922361687, 0.404290664862705, 0.859673130206918,
0.109671739827895, 0.649996563375757, 0.258954275898764, 0.891633421104863,
0.984588170265556, 0.580217119257266, 0.0603333623372893, 0.44696610247175,
0.737685847082119, 0.825902270166115, 0.28219718214757, 0.726311595263121,
0.119494527095048, 0.404290664862705, 0.08618274906934, 0.505299803596011,
0.980931153130498, 0.755763506431425, 0.0748296820784503, 0.113653331527467,
0.0392505025014708, 0.0736894630543969, 0.665432767712236, 0.0907566629876851,
0.694742633658554, 0.0555240242100787, 0.908398411052764, 0.981500034696054,
0.485232687368431, 0.0615086640134777, 0.984844760053708, 0.0638671437811352,
0.988456538219304, 0.999817421739174, 0.968665938432762, 0.0748296820784503,
0.994049039099016, 0.0377643922223044, 0.753136156263104, 0.123420382453217,
0.492815772761299, 0.0552759476600101, 0.04619620544431, 0.993558959352535,
0.958692909665931, 0.796855287902484, 0.785348058724267, 0.0746634183614283,
0.0377643922223044, 0.780704435206794, 0.0907566629876851, 0.406564500335203,
0.0552759476600101, 0.0748296820784503, 0.970156690398876, 0.0660942198437862,
0.873996817804486, 0.375360542396413, 0.0748296820784503, 0.965123786868184,
0.854643072955185, 0.943460469480539, 0.946225993521448, 0.201360905713179,
0.0454708459833313, 0.48996783434662, 0.05244950717931, 0.0338509156119364,
0.0638671437811352, 0.0603333623372893, 0.970486206624244, 0.0629566341884062,
0.0377643922223044, 0.0907566629876851, 0.779347354285913, 0.0976031677612942,
0.205175835577883, 0.0941241472498526, 0.0615086640134777, 0.886524283077346,
0.08618274906934, 0.402954528643347, 0.0414465870459318, 0.324532816807941,
0.0673439981500492, 0.332945789420635, 0.0859766122732282, 0.631281978569196,
0.0828903902268199, 0.281237919273814, 0.0526229830201917, 0.102715061383218,
0.424333471946505, 0.109671739827895, 0.334510671397625, 0.205175835577883,
0.159566259573363, 0.0377643922223044, 0.0673439981500492, 0.0552759476600101,
0.445839037463781, 0.10425385465164, 0.0748296820784503, 0.33704115305663,
0.446163485277425, 0.327458212154153, 0.335397230073375, 0.0907566629876851,
0.279182787435862, 0.0377643922223044, 0.627397908880837, 0.0615086640134777,
0.199646115900485, 0.0377643922223044, 0.119494527095048, 0.0990738346450488,
0.0651541701402252, 0.210363278368539, 0.835632648112557, 0.106253455281839,
0.672696490538852, 0.0791065698133259, 0.0377643922223044, 0.0579525711534136,
0.168644694798739, 0.0818186063327247, 0.488666093794954, 0.104471026168687,
0.411123824699131, 0.043759947041066, 0.832302321076392, 0.08618274906934,
0.850445549496536, 0.0818186063327247, 0.0972966397634192, 0.742789223995539,
0.0972966397634192, 0.0615086640134777, 0.350147967076315, 0.0834455585328606,
0.200218965701701, 0.168645314126027, 0.264834336288992, 0.55183655930448,
0.998360258355913, 0.747849926324466, 0.822078275048955, 0.200218965701701,
0.174707839309301, 0.752710934656085, 0.156119240284303, 0.999997937032243,
0.0978349800253458, 0.130366347717656, 0.936549304101673, 0.295797942069145,
0.572760013365421, 0.108316297792363, 0.130366347717656, 0.75739263755114,
0.9509658243644, 0.175032116991885, 0.200218965701701, 0.910206430852628,
0.157048689970524, 0.84708487363099, 0.994577688845304, 0.997334831946742,
0.731923661895837, 0.664962538422614, 0.964120396944314, 0.629922816044294,
0.634363916847897, 0.969653673564233, 0.168645314126027, 0.200218965701701,
0.584286635005199, 0.809356934865227, 0.168645314126027, 0.875688841240383,
0.996773953650802, 0.999394580778824, 0.153330048922211, 0.108316297792363,
0.714615974581087, 0.236025596147989, 0.763135390061522, 0.262082283352036,
0.785945199310967, 0.197576150353845, 0.130366347717656, 0.262082283352036,
0.185872085793341, 0.786714647400174, 0.250808000191318, 0.493642763685253,
0.276025749421507, 0.995707589992483, 0.723972570676443, 0.185872085793341,
0.993575053140907, 0.636494879586229, 0.108316297792363, 0.51959645967254,
0.968463022816875, 0.130366347717656, 0.168645314126027, 0.999999602413311,
0.999848027073353, 0.998701533631995, 0.645730934751545, 0.168645314126027,
0.108316297792363, 0.999615214558767, 0.225949824127342, 0.921457054113434,
0.626047910850807, 0.319965727014557, 0.826915145901163, 0.124068593426873,
0.848810790860168, 0.999968351142109, 0.0377643922223044, 0.0907566629876851,
0.222255143040685, 0.686163330183421, 0.0615086640134777, 0.11082712320078,
0.998780200453573, 0.0990738346450488, 0.994340014830895, 0.0907566629876851,
0.403298870654814, 0.778521668722922, 0.57616281020405, 0.119494527095048,
0.460774595279868, 0.677212956298485, 0.365153052456205, 0.320482835690074,
0.800285167816025, 0.303551214055041, 0.297985558181075, 0.546380002925809,
0.0709957739192715, 0.994563843708545, 0.825941535676903, 0.131956884728731,
0.0884778789514815, 0.999556616678201, 0.444558396246521, 0.642499464304237,
0.434854789018697, 0.0907566629876851, 0.0638671437811352, 0.669045125239348,
0.0848854096888075, 0.0673439981500492, 0.0638671437811352, 0.610028366489965,
0.55760277113571, 0.0941241472498526, 0.559467842208759, 0.481647439553369,
0.74474475824611, 0.995837860976588, 0.0651541701402252, 0.990339791555457,
0.74195494832408, 0.826620120748051, 0.966211415481097, 0.0594262684938816,
0.542802653776859, 0.359955774042464, 0.0685414676275887, 0.0696832889395705,
0.0615086640134777, 0.0615086640134777, 0.0615086640134777, 0.204608070642916,
0.755095270418887, 0.0748296820784503, 0.846297179600085, 0.0789374775562426,
0.0552759476600101, 0.343312043382595, 0.999312893094822, 0.949898124393167,
0.99771682132211, 0.999999975045591, 0.36952591599141, 0.109671739827895,
0.109671739827895, 0.302226900436509, 0.0818186063327247, 0.578940170110261,
0.280112560120911, 0.354812583361725, 0.125593391286759, 0.979668461464564,
0.570205685940354, 0.997331720059511, 0.999976386735486, 0.999998780444334,
0.481647439553369, 0.998664271658994, 0.360223460393037, 0.0748296820784503,
0.0748296820784503, 0.0748296820784503, 0.999999988913185, 0.0673439981500492,
0.119494527095048, 0.610756343952574, 0.558853759443228, 0.999820628386899,
0.408500564715029, 0.10425385465164, 0.719058910018478, 0.716133907391236,
0.736523706770552, 0.999998306360338, 0.864325682062083, 0.906816655648972,
0.10425385465164, 0.279678755209289, 0.0972966397634192, 0.587214486975681,
0.687662372854392, 0.0972966397634192, 0.999673568188575, 0.972232324617564,
0.73955827239797, 0.109671739827895, 0.495731299058, 0.691643479226959,
0.505951643690991, 0.594536350465403, 0.0685414676275887, 0.0748296820784503,
0.934774363293305, 0.833637951576354, 0.312948818204438, 0.159566259573363,
0.999961012396035, 0.367903495817727, 0.520973034217547, 0.0660942198437862,
0.0638671437811352, 0.0392505025014708, 0.0392505025014708, 0.0615086640134777,
0.0594262684938816, 0.447865753947512, 0.911271265074087, 0.3204972515358,
0.4628182151749, 0.0748296820784503, 0.08618274906934, 0.0638671437811352,
0.0972966397634192, 0.123405647405202, 0.106253455281839, 0.387590964006095,
0.391557595472122, 0.0615086640134777, 0.109671739827895, 0.351378505737096,
0.0748296820784503, 0.0552759476600101, 0.429078727020032, 0.322872157269432,
0.400034823953817, 0.0965706726779576, 0.116239419899748, 0.109671739827895,
0.775197200601877, 0.0377643922223044, 0.0907566629876851, 0.0615086640134777,
0.891212981560056, 0.737020114099671, 0.354812583361725, 0.0972966397634192,
0.306255657561044, 0.573732063798352, 0.0615086640134777, 0.0615086640134777,
0.10425385465164, 0.677429963276143, 0.505690444049929, 0.0638671437811352,
0.794202665990141, 0.0615086640134777, 0.0615086640134777, 0.0907566629876851,
0.134273004564522, 0.39598904795251, 0.320482835690074, 0.0846150773633906,
0.5652984000057, 0.131956884728731, 0.0615086640134777, 0.413749799864906,
0.197985138905177, 0.074823794121398, 0.787342519959583, 0.546853023386094,
0.123420382453217, 0.0615086640134777, 0.0615086640134777, 0.0429276994305486,
0.0736894630543969, 0.0615086640134777, 0.629873023652277, 0.999901323519759,
0.0377643922223044, 0.10425385465164, 0.0615086640134777, 0.997409410220319,
0.997250944891722, 0.359900707035101, 0.0594262684938816, 0.0748296820784503,
0.488057069325502, 0.0377643922223044, 0.0406564654194419, 0.418008793465899,
0.999999567938908, 0.256815940505751, 0.320468119900392, 0.997298940922502,
0.0673439981500492, 0.631238015585201, 0.999913728560637, 0.995827744758923,
0.512094861901834, 0.660057003931633, 0.405970489390143, 0.530070012404131,
0.913171532463041, 0.805590604539588, 0.912014367187904, 0.436121088179947,
0.998089531782825, 0.532943879571607, 0.974620473464361, 0.499524389055702,
0.0907566629876851, 0.0907566629876851, 0.0748296820784503, 0.0748296820784503,
0.109671739827895, 0.0846150773633906, 0.0905584750799325, 0.0972966397634192,
0.0686156722735077, 0.0748296820784503, 0.0709957739192715, 0.0833391845206762,
0.0615086640134777, 0.0709957739192715, 0.0615086640134777, 0.0976031677612942,
0.0990738346450488, 0.0338509156119364, 0.328752977917769, 0.0500544149408275,
0.539813007232865, 0.276025749421507, 0.441872144478899, 0.999757886707462,
0.907964788293882, 0.999994380435774, 0.998911259869357, 0.999973669501945,
0.987069369818857, 0.999024882562814, 0.986900066502302, 0.995584458177745,
0.191289023108439, 0.999902507747188, 0.551001002608127, 0.197576150353845,
0.168645314126027, 0.108316297792363, 0.168645314126027, 0.188203218691261,
0.998997233176256, 0.168645314126027, 0.713967217146069, 0.222458690265271,
0.200218965701701, 0.276025749421507, 0.989326278356066, 0.168645314126027,
0.839088345086636, 0.976964838174943, 0.319965727014557, 0.108316297792363,
0.895985099196173, 0.197576150353845, 0.909919072597867, 0.236025596147989,
0.618116563617652, 0.667180769072741, 0.914672801654092, 0.533539814247767,
0.153330048922211, 0.915817361674353, 0.200218965701701, 0.200218965701701,
0.774948988466037, 0.250808000191318, 0.816264428519818, 0.811015978211602,
0.200218965701701, 0.200218965701701, 0.774948988466037, 0.749284366489231,
0.479963638235032, 0.191289023108439, 0.168645314126027, 0.528402401264738,
0.754216344557484, 0.168645314126027, 0.766523690849346, 0.174348655194393,
0.168645314126027, 0.168645314126027, 0.834476188409728, 0.174348655194393,
0.168645314126027, 0.667052326472271, 0.174348655194393, 0.153330048922211,
0.558824128213264, 0.999105432221686, 0.693661035349947, 0.179688784137895,
0.200218965701701, 0.168645314126027, 0.229479575976715, 0.174348655194393,
0.904732762609591, 0.179688784137895, 0.118033778911209, 0.747868771342673,
0.200218965701701, 0.168645314126027, 0.168645314126027, 0.970747258668972,
0.857748516375815, 0.206724569188298, 0.108316297792363, 0.168645314126027,
0.966754082880875, 0.980599299013409, 0.163567941589258, 0.83446560515758,
0.209650239232065, 0.153330048922211, 0.926920329088, 0.0978349800253458,
0.182666397714213, 0.153330048922211, 0.239664014724832, 0.222458690265271,
0.174348655194393, 0.289318271113194, 0.999781677878208, 0.457311145881054,
0.185682184351642, 0.99828397689456, 0.996478706002414, 0.168645314126027,
0.171478181484122, 0.108316297792363, 0.787261632099557, 0.987216363123405,
0.168645314126027, 0.153330048922211, 0.222659696409762, 0.21280075792145,
0.900939326812682, 0.998531241515998, 0.770949155212397, 0.577410833720496,
0.99981890398624, 0.250153703366728, 0.838686212775639, 0.201417022909777
), .Dim = c(735L, 1L), .Dimnames = list(c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70",
"71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81",
"82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92",
"93", "94", "95", "96", "97", "98", "99", "100", "101", "102",
"103", "104", "105", "106", "107", "108", "109", "110", "111",
"112", "113", "114", "115", "116", "117", "118", "119", "120",
"121", "122", "123", "124", "125", "126", "127", "128", "129",
"130", "131", "132", "133", "134", "135", "136", "137", "138",
"139", "140", "141", "142", "143", "144", "145", "146", "147",
"148", "149", "150", "151", "152", "153", "154", "155", "156",
"157", "158", "159", "160", "161", "162", "163", "164", "165",
"166", "167", "168", "169", "170", "171", "172", "173", "174",
"175", "176", "177", "178", "179", "180", "181", "182", "183",
"184", "185", "186", "187", "188", "189", "190", "191", "192",
"193", "194", "195", "196", "197", "198", "199", "200", "201",
"202", "203", "204", "205", "206", "207", "208", "209", "210",
"211", "212", "213", "214", "215", "216", "217", "218", "219",
"220", "221", "222", "223", "224", "225", "226", "227", "228",
"229", "230", "231", "232", "233", "234", "235", "236", "237",
"238", "239", "240", "241", "242", "243", "244", "245", "246",
"247", "248", "249", "250", "251", "252", "253", "254", "255",
"256", "257", "258", "259", "260", "261", "262", "263", "264",
"265", "266", "267", "268", "269", "270", "271", "272", "273",
"274", "275", "276", "277", "278", "279", "280", "281", "282",
"283", "284", "285", "286", "287", "288", "289", "290", "291",
"292", "293", "294", "295", "296", "297", "298", "299", "300",
"301", "302", "303", "304", "305", "306", "307", "308", "309",
"310", "311", "312", "313", "314", "315", "316", "317", "318",
"319", "320", "321", "322", "323", "324", "325", "326", "327",
"328", "329", "330", "331", "332", "333", "334", "335", "336",
"337", "338", "339", "340", "341", "342", "343", "344", "345",
"346", "347", "348", "349", "350", "351", "352", "353", "354",
"355", "356", "357", "358", "359", "360", "361", "362", "363",
"364", "365", "1490", "1491", "1492", "1493", "1494", "1495",
"1496", "1497", "1498", "1499", "1500", "1501", "1502", "1503",
"1504", "1505", "1506", "1507", "1508", "1509", "1510", "1511",
"1512", "1513", "1514", "1515", "1516", "1517", "1518", "1519",
"1520", "1521", "1522", "1523", "1524", "1525", "1526", "1527",
"1528", "1529", "1530", "1531", "1532", "1533", "1534", "1535",
"1536", "1537", "1538", "1539", "1540", "1541", "1542", "1543",
"1544", "1545", "1546", "1547", "1548", "1549", "1550", "1551",
"1552", "1553", "1554", "1555", "1556", "1557", "1558", "1559",
"1560", "1561", "1562", "1563", "1564", "1565", "1566", "1567",
"1568", "1569", "1570", "1571", "1572", "1573", "1574", "1575",
"1576", "1577", "1578", "1579", "1580", "1581", "1582", "1583",
"1584", "1585", "1586", "1587", "1588", "1589", "1590", "1591",
"1592", "1593", "1594", "1595", "1596", "1597", "1598", "1599",
"1600", "1601", "1602", "1603", "1604", "1605", "1606", "1607",
"1608", "1609", "1610", "1611", "1612", "1613", "1614", "1615",
"1616", "1617", "1618", "1619", "1620", "1621", "1622", "1623",
"1624", "1625", "1626", "1627", "1628", "1629", "1630", "1631",
"1632", "1633", "1634", "1635", "1636", "1637", "1638", "1639",
"1640", "1641", "1642", "1643", "1644", "1645", "1646", "1647",
"1648", "1649", "1650", "1651", "1652", "1653", "1654", "1655",
"1656", "1657", "1658", "1659", "1660", "1661", "1662", "1663",
"1664", "1665", "1666", "1667", "1668", "1669", "1670", "1671",
"1672", "1673", "1674", "1675", "1676", "1677", "1678", "1679",
"1680", "1681", "1682", "1683", "1684", "1685", "1686", "1687",
"1688", "1689", "1690", "1691", "1692", "1693", "1694", "1695",
"1696", "1697", "1698", "1699", "1700", "1701", "1702", "1703",
"1704", "1705", "1706", "1707", "1708", "1709", "1710", "1711",
"1712", "1713", "1714", "1715", "1716", "1717", "1718", "1719",
"1720", "1721", "1722", "1723", "1724", "1725", "1726", "1727",
"1728", "1729", "1730", "1731", "1732", "1733", "1734", "1735",
"1736", "1737", "1738", "1739", "1740", "1741", "1742", "1743",
"1744", "1745", "1746", "1747", "1748", "1749", "1750", "1751",
"1752", "1753", "1754", "1755", "1756", "1757", "1758", "1759",
"1760", "1761", "1762", "1763", "1764", "1765", "1766", "1767",
"1768", "1769", "1770", "1771", "1772", "1773", "1774", "1775",
"1776", "1777", "1778", "1779", "1780", "1781", "1782", "1783",
"1784", "1785", "1786", "1787", "1788", "1789", "1790", "1791",
"1792", "1793", "1794", "1795", "1796", "1797", "1798", "1799",
"1800", "1801", "1802", "1803", "1804", "1805", "1806", "1807",
"1808", "1809", "1810", "1811", "1812", "1813", "1814", "1815",
"1816", "1817", "1818", "1819", "1820", "1821", "1822", "1823",
"1824", "1825", "1826", "1827", "1828", "1829", "1830", "1831",
"1832", "1833", "1834", "1835", "1836", "1837", "1838", "1839",
"1840", "1841", "1842", "1843", "1844", "1845", "1846", "1847",
"1848", "1849", "1850", "1851", "1852", "1853", "1854", "1855",
"1856", "1857", "1858", "1859"), NULL))
head
就在这里
head(data)
[,1]
1 0.44757850
2 0.03776439
3 0.29382907
4 0.24773142
5 0.25845418
6 0.37072824
而tail
就在这里
tail(data)
[,1]
1854 0.7709492
1855 0.5774108
1856 0.9998189
1857 0.2501537
1858 0.8386862
1859 0.2014170
如下所示,行名称从365跳到1490。
data[363:368,]
> data[363:368,]
363 364 365 1490 1491 1492
0.12406859 0.84881079 0.99996835 0.03776439 0.09075666 0.2222551
我想添加一个行名称向量,用于填充366:1489中缺少的数据,并使每个值为零。下面我创建了填充行ID以及0的矢量。
将这些“插入”到data
表中的最佳方法是什么,以便行名称是连续的?
RowID <- 366:1489
Num <- rep(0, length(RowID))
提前致谢
答案 0 :(得分:2)
你可以用
来做newdata<- rbind(data[1:365,],something_new,data[366:nrow(data),]
通过定义newdata
的rownames或预定义要“插入”的something_new
矩阵的行名称来跟进。
答案 1 :(得分:2)
这有点笼统(你的方法适用于那个特定的差距):
df <- data.frame(id=as.integer(rownames(data)),value=data)
all.rows <- data.frame(id=min(df$id):max(df$id),value=0)
new.df[which(all.rows$id %in% df$id),]$value <- df$value
new.df
是一个包含所有可能行的模板,并初始化为0.然后根据df
中的内容设置相应的行。
如果data
非常大,data.tables可能会更快:
library(data.table)
dt <- data.table(id=as.integer(rownames(data)), value=data, key="id")
all.rows <- data.table(id=min(dt$id):max(dt$id), key="id")
new.dt <- dt[all.rows]
new.dt[is.na(value.V1),] <- 0L
答案 2 :(得分:1)
有点迟到的答案,但这里是使用dplyr
的单线程,通过为观察编号添加一列来实现此目的。
library(dplyr)
left_join(data.frame(obs = 1:1859), data.frame(data, obs=as.numeric(rownames(data))))
当然,为了概括这一点,您可以将1:1859
替换为nrow(mydf)
或nrow(mymatrix)
。
答案 3 :(得分:0)
感谢@Carl Witthoft让我朝着正确的方向前进。下面的代码就是我需要的。而不是“插入”数据。我通过rbind()
添加了缺失的数据,然后使用了order().
#Make data.frame and change colname()
data <- as.data.frame(data)
head(data)
colnames(data) <- "new"
#Make new data to insert
RowID <- 366:1489
Num <- rep(0, length(RowID))
names(Num) <- RowID
Num <- as.data.frame(Num)
colnames(Num) <- "new"
head(Num)
#Order data by the created RowID column
newdata <- rbind(data, Num)
newdata$RowID <- as.numeric(rownames(newdata))
newdata <- newdata[order(newdata$RowID),]
head(newdata)
tail(newdata)