Divide multiple columns of one data frame by row names value of another dataframe R

筅森魡賤 提交于 2019-12-13 01:53:49

问题


I have a data.frame gtable that has 61 columns. A total of 55 of the 61 columns [7:61] are sample names that match the row.name samples in my data.frame stats. There is another row.name mapped.reads.num in stats that has corresponding values for the samples.

I need divide columns [7:61] in my gtable data.frame by the values of the row.name mapped.reads.num and output a new data.frame with same columns as in gtable and the new values.

The code I am trying is:

> test <- data.frame(gtable[7:61] / stats["mapped.reads.num",])

Error in Ops.data.frame(gtable[7:61], stats["mapped.reads.num", ]) : 
/ only defined for equally-sized data frames

So what I want is a new data.frame with all columns [1:61] that are in gtable but with the updated values that are a result of the division above.

Here is some of my data.

gtable:

> dput(head(gtable))

structure(list(GeneID = c("ENSG00000000003", "ENSG00000000005", 
"ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938"
), Chr = c("chrX", "chrX", "chr20", "chr1", "chr1", "chr1"), 
GeneName = c("TSPAN6", "TNMD", "DPM1", "SCYL3", "C1orf112", 
"FGR"), Start = c(99883667L, 99839799L, 49551404L, 169818772L, 
169631245L, 27938575L), Stop = c(99894988L, 99854882L, 49575092L, 
169863408L, 169823221L, 27961788L), CodingLength = c(2968L, 
1610L, 1207L, 6876L, 6354L, 3474L), s_bb00082 = c("1135", 
"5", "831", "893", "450", "70"), s_bb00136 = c("706", "2", 
"545", "788", "540", "278"), s_bb02092 = c("916", "4", "880", 
"1048", "594", "96"), s_bb02130 = c("330", "7", "379", "350", 
"292", "113"), s_bb02269 = c("850", "2", "480", "467", "381", 
"159"), s_bb03176 = c("484", "6", "698", "625", "395", "232"
), s_bb03188 = c("760", "2", "651", "919", "692", "233"), 
s_bb03242 = c("607", "8", "760", "839", "397", "81"), s_bb04018 = c("441", 
"10", "576", "788", "640", "263"), s_bb05005 = c("1421", 
"3", "702", "914", "596", "97"), s_bb05212 = c("930", "6", 
"815", "831", "581", "427"), s_bb05315 = c("793", "8", "495", 
"619", "521", "307"), s_bb05343 = c("822", "14", "990", "924", 
"563", "451"), s_bb06173 = c("478", "10", "619", "685", "500", 
"110"), s_bb07006 = c("669", "6", "988", "860", "379", "267"
), s_bb07055 = c("503", "2", "571", "482", "389", "244"), 
s_bb07213 = c("662", "4", "632", "793", "507", "98"), s_bb07215 = c("719", 
"3", "610", "762", "387", "152"), s_bb07221 = c("506", "3", 
"615", "525", "373", "214"), s_bb07253 = c("2088", "8", "570", 
"849", "361", "103"), s_bb07278 = c("1042", "6", "489", "962", 
"726", "266"), s_bb08015 = c("461", "5", "509", "673", "461", 
"288"), s_bb08101 = c("510", "2", "639", "651", "396", "220"
), s_bb08227 = c("482", "0", "522", "608", "513", "147"), 
s_bb08337 = c("784", "2", "589", "822", "401", "186"), s_bb09050 = c("601", 
"10", "668", "701", "363", "269"), s_bb09069 = c("468", "0", 
"588", "643", "334", "285"), s_bb09115 = c("812", "0", "368", 
"470", "327", "195"), s_bb09309 = c("595", "2", "554", "963", 
"595", "104"), s_bb10014 = c("444", "11", "600", "651", "369", 
"229"), s_bb10044 = c("683", "6", "520", "808", "425", "279"
), s_bb10247 = c("816", "4", "718", "716", "445", "118"), 
s_bb10420 = c("265", "5", "475", "530", "490", "238"), s_bb11076 = c("831", 
"0", "625", "466", "405", "146"), s_bb11109 = c("1167", "4", 
"535", "771", "626", "660"), s_bb11119 = c("769", "4", "440", 
"704", "486", "200"), s_bb11384 = c("912", "2", "427", "549", 
"457", "170"), s_bb12052 = c("486", "5", "457", "444", "332", 
"151"), s_bb12090 = c("645", "2", "408", "398", "286", "137"
), s_bb12101 = c("354", "0", "531", "553", "421", "101"), 
s_bb12111 = c("721", "0", "405", "625", "372", "299"), s_bb12142 = c("3928", 
"8", "403", "1035", "325", "96"), s_bb12165 = c("1736", "6", 
"523", "765", "381", "83"), s_bb12260 = c("794", "4", "614", 
"539", "353", "174"), s_bb13044 = c("865", "1", "374", "455", 
"287", "143"), s_bb13315 = c("277", "8", "501", "552", "260", 
"47"), s_bb98005 = c("565", "16", "644", "668", "371", "137"
), s_bb98262 = c("345", "4", "546", "806", "424", "91"), 
s_bb10008 = c("1591", "10", "1711", "1646", "1057", "490"
), s_bb10236 = c("514", "5", "767", "744", "523", "304"), 
s_bb98320 = c("560", "10", "1053", "1077", "593", "115"), 
s_bb98324 = c("462", "7", "794", "1010", "538", "255"), s_bb99097 = c("1038", 
"10", "1456", "1734", "1298", "612"), s_bb99151 = c("2596", 
"4", "1741", "1795", "1032", "771"), s_bb99166 = c("1193", 
"13", "1449", "1425", "891", "423")), .Names = c("GeneID", 
"Chr", "GeneName", "Start", "Stop", "CodingLength", "s_bb00082", 
"s_bb00136", "s_bb02092", "s_bb02130", "s_bb02269", "s_bb03176", 
"s_bb03188", "s_bb03242", "s_bb04018", "s_bb05005", "s_bb05212", 
"s_bb05315", "s_bb05343", "s_bb06173", "s_bb07006", "s_bb07055", 
"s_bb07213", "s_bb07215", "s_bb07221", "s_bb07253", "s_bb07278", 
"s_bb08015", "s_bb08101", "s_bb08227", "s_bb08337", "s_bb09050", 
"s_bb09069", "s_bb09115", "s_bb09309", "s_bb10014", "s_bb10044", 
"s_bb10247", "s_bb10420", "s_bb11076", "s_bb11109", "s_bb11119", 
"s_bb11384", "s_bb12052", "s_bb12090", "s_bb12101", "s_bb12111", 
"s_bb12142", "s_bb12165", "s_bb12260", "s_bb13044", "s_bb13315", 
"s_bb98005", "s_bb98262", "s_bb10008", "s_bb10236", "s_bb98320", 
"s_bb98324", "s_bb99097", "s_bb99151", "s_bb99166"), row.names = c("1", 
"2", "3", "4", "5", "6"), class = "data.frame")

stats:

> dput(stats)

structure(list(V2 = c("s_bb00082", "118,919,334", "118,857,614", 
"103,343,443 (86.9)", "89,839,574 (75.5)", "13,503,869 (11.4)", 
"43,966,262 (37.0)", "240,414,294 (202.2)", "103343443"), V3 = c("s_bb00136", 
"119,138,942", "119,073,285", "101,498,158 (85.2)", "90,320,377 (75.8)", 
"11,177,781 (9.4)", "40,819,281 (34.3)", "222,431,407 (186.7)", 
"101498158"), V4 = c("s_bb02092", "154,244,518", "154,165,681", 
"130,499,592 (84.6)", "114,971,350 (74.5)", "15,528,242 (10.1)", 
"55,799,738 (36.2)", "308,004,109 (199.7)", "130499592"), V5 = c("s_bb02130", 
"125,276,906", "125,170,419", "106,938,562 (85.4)", "98,670,718 (78.8)", 
"8,267,844 (6.6)", "31,258,536 (25.0)", "162,297,018 (129.6)", 
"106938562"), V6 = c("s_bb02269", "137,500,040", "137,380,927", 
"116,574,780 (84.8)", "107,220,033 (78.0)", "9,354,747 (6.8)", 
"36,927,991 (26.9)", "193,093,063 (140.4)", "116574780"), V7 = c("s_bb03176", 
"146,395,500", "146,270,388", "124,619,457 (85.1)", "113,148,447 (77.3)", 
"11,471,010 (7.8)", "45,972,822 (31.4)", "241,880,059 (165.2)", 
"124619457"), V8 = c("s_bb03188", "128,055,138", "127,935,605", 
"109,770,323 (85.7)", "98,593,301 (77.0)", "11,177,022 (8.7)", 
"41,791,357 (32.6)", "227,169,478 (177.4)", "109770323"), V9 = c("s_bb03242", 
"148,154,652", "148,017,561", "124,021,870 (83.7)", "112,545,054 (76.0)", 
"11,476,816 (7.7)", "42,183,333 (28.5)", "220,340,383 (148.7)", 
"124021870"), V10 = c("s_bb04018", "131,223,754", "131,097,454", 
"110,770,095 (84.4)", "98,100,780 (74.8)", "12,669,315 (9.7)", 
"40,918,851 (31.2)", "228,508,418 (174.1)", "110770095"), V11 = c("s_bb05005", 
"127,584,888", "127,458,173", "108,436,878 (85.0)", "94,494,689 (74.1)", 
"13,942,189 (10.9)", "45,757,687 (35.9)", "252,590,533 (198.0)", 
"108436878"), V12 = c("s_bb05212", "140,652,042", "140,510,428", 
"118,797,157 (84.5)", "103,874,665 (73.9)", "14,922,492 (10.6)", 
"51,881,008 (36.9)", "280,995,043 (199.8)", "118797157"), V13 = c("s_bb05315", 
"138,409,450", "138,268,143", "114,903,154 (83.0)", "104,115,662 (75.2)", 
"10,787,492 (7.8)", "42,052,850 (30.4)", "224,181,633 (162.0)", 
"114903154"), V14 = c("s_bb05343", "125,903,118", "125,780,042", 
"107,610,184 (85.5)", "90,092,659 (71.6)", "17,517,525 (13.9)", 
"54,993,809 (43.7)", "311,179,897 (247.2)", "107610184"), V15 = c("s_bb06173", 
"135,137,536", "135,003,369", "115,152,555 (85.2)", "101,068,299 (74.8)", 
"14,084,256 (10.4)", "45,468,489 (33.6)", "239,973,913 (177.6)", 
"115152555"), V16 = c("s_bb07006", "134,746,118", "134,617,790", 
"116,033,251 (86.1)", "98,736,415 (73.3)", "17,296,836 (12.8)", 
"53,015,332 (39.3)", "293,307,193 (217.7)", "116033251"), V17 = c("s_bb07055", 
"118,105,530", "117,990,305", "101,555,777 (86.0)", "91,640,778 (77.6)", 
"9,914,999 (8.4)", "39,511,999 (33.5)", "205,378,512 (173.9)", 
"101555777"), V18 = c("s_bb07213", "153,409,250", "153,254,804", 
"128,699,480 (83.9)", "114,521,058 (74.7)", "14,178,422 (9.2)", 
"48,480,606 (31.6)", "264,765,534 (172.6)", "128699480"), V19 = c("s_bb07215", 
"143,957,388", "143,812,328", "123,167,238 (85.6)", "111,860,651 (77.7)", 
"11,306,587 (7.9)", "43,690,722 (30.3)", "236,111,216 (164.0)", 
"123167238"), V20 = c("s_bb07221", "148,506,328", "148,345,297", 
"125,386,916 (84.4)", "111,145,772 (74.8)", "14,241,144 (9.6)", 
"47,458,664 (32.0)", "250,901,773 (169.0)", "125386916"), V21 = c("s_bb07253", 
"118,329,800", "118,199,155", "98,321,008 (83.1)", "86,910,578 (73.4)", 
"11,410,430 (9.6)", "38,537,231 (32.6)", "209,250,869 (176.8)", 
"98321008"), V22 = c("s_bb07278", "143,963,148", "143,805,475", 
"121,620,275 (84.5)", "110,174,778 (76.5)", "11,445,497 (8.0)", 
"44,440,332 (30.9)", "242,905,288 (168.7)", "121620275"), V23 = c("s_bb08015", 
"127,957,452", "127,826,202", "107,764,096 (84.2)", "95,164,934 (74.4)", 
"12,599,162 (9.8)", "40,650,935 (31.8)", "219,897,728 (171.9)", 
"107764096"), V24 = c("s_bb08101", "137,351,268", "137,208,841", 
"117,197,326 (85.3)", "105,333,157 (76.7)", "11,864,169 (8.6)", 
"42,096,302 (30.6)", "230,046,259 (167.5)", "117197326"), V25 = c("s_bb08227", 
"137,338,192", "137,195,789", "115,045,891 (83.8)", "103,861,577 (75.6)", 
"11,184,314 (8.1)", "38,278,525 (27.9)", "202,203,461 (147.2)", 
"115045891"), V2 = c("s_bb08337", "111,566,254", "111,459,962", 
"97,426,140 (87.3)", "85,344,209 (76.5)", "12,081,931 (10.8)", 
"41,897,472 (37.6)", "229,765,923 (205.9)", "97426140"), V3 = c("s_bb09050", 
"121,590,604", "121,472,061", "106,849,088 (87.9)", "93,720,771 (77.1)", 
"13,128,317 (10.8)", "44,069,682 (36.2)", "236,142,675 (194.2)", 
"106849088"), V4 = c("s_bb09069", "112,087,790", "111,979,772", 
"96,506,791 (86.1)", "85,997,307 (76.7)", "10,509,484 (9.4)", 
"37,243,917 (33.2)", "203,086,222 (181.2)", "96506791"), V5 = c("s_bb09115", 
"84,807,392", "84,735,639", "72,524,774 (85.5)", "64,581,000 (76.2)", 
"7,943,774 (9.4)", "30,375,552 (35.8)", "165,606,914 (195.3)", 
"72524774"), V6 = c("s_bb09309", "130,626,166", "130,517,303", 
"110,029,814 (84.2)", "98,256,799 (75.2)", "11,773,015 (9.0)", 
"44,423,365 (34.0)", "252,761,773 (193.5)", "110029814"), V7 = c("s_bb10014", 
"124,563,828", "124,462,427", "106,689,763 (85.7)", "94,358,412 (75.8)", 
"12,331,351 (9.9)", "44,222,969 (35.5)", "237,704,396 (190.8)", 
"106689763"), V8 = c("s_bb10044", "112,510,692", "112,408,836", 
"93,514,202 (83.1)", "83,352,847 (74.1)", "10,161,355 (9.0)", 
"37,432,246 (33.3)", "209,566,548 (186.3)", "93514202"), V9 = c("s_bb10247", 
"111,241,066", "111,139,740", "96,584,111 (86.8)", "84,083,649 (75.6)", 
"12,500,462 (11.2)", "44,159,779 (39.7)", "241,863,635 (217.4)", 
"96584111"), V10 = c("s_bb10420", "113,330,370", "113,226,143", 
"96,216,328 (84.9)", "86,551,935 (76.4)", "9,664,393 (8.5)", 
"36,998,748 (32.6)", "198,689,386 (175.3)", "96216328"), V11 = c("s_bb11076", 
"97,194,370", "97,119,394", "83,681,667 (86.1)", "73,699,151 (75.8)", 
"9,982,516 (10.3)", "36,072,349 (37.1)", "193,412,105 (199.0)", 
"83681667"), V12 = c("s_bb11109", "122,503,752", "122,408,327", 
"105,169,514 (85.9)", "93,676,424 (76.5)", "11,493,090 (9.4)", 
"41,497,611 (33.9)", "224,512,856 (183.3)", "105169514"), V13 = c("s_bb11119", 
"103,100,544", "103,021,768", "88,905,382 (86.2)", "78,562,694 (76.2)", 
"10,342,688 (10.0)", "36,865,089 (35.8)", "200,719,088 (194.7)", 
"88905382"), V14 = c("s_bb11384", "104,536,288", "104,450,587", 
"89,427,140 (85.5)", "80,053,842 (76.6)", "9,373,298 (9.0)", 
"32,423,808 (31.0)", "173,988,951 (166.4)", "89427140"), V15 = c("s_bb12052", 
"112,391,398", "112,298,101", "96,120,757 (85.5)", "87,315,274 (77.7)", 
"8,805,483 (7.8)", "34,170,760 (30.4)", "182,357,957 (162.3)", 
"96120757"), V16 = c("s_bb12090", "113,086,410", "112,994,111", 
"93,418,875 (82.6)", "83,383,790 (73.7)", "10,035,085 (8.9)", 
"34,804,902 (30.8)", "181,597,976 (160.6)", "93418875"), V17 = c("s_bb12101", 
"119,760,200", "119,661,599", "100,836,962 (84.2)", "90,348,949 (75.4)", 
"10,488,013 (8.8)", "36,106,863 (30.1)", "190,751,545 (159.3)", 
"100836962"), V18 = c("s_bb12111", "103,100,460", "103,016,548", 
"90,481,939 (87.8)", "81,212,317 (78.8)", "9,269,622 (9.0)", 
"33,389,408 (32.4)", "177,644,705 (172.3)", "90481939"), V19 = c("s_bb12142", 
"108,879,028", "108,788,404", "92,934,112 (85.4)", "79,679,966 (73.2)", 
"13,254,146 (12.2)", "39,141,681 (35.9)", "214,916,975 (197.4)", 
"92934112"), V20 = c("s_bb12165", "102,883,862", "102,801,693", 
"86,186,616 (83.8)", "76,974,145 (74.8)", "9,212,471 (9.0)", 
"34,656,660 (33.7)", "188,725,481 (183.4)", "86186616"), V21 = c("s_bb12260", 
"134,250,278", "134,146,164", "113,882,842 (84.8)", "101,889,020 (75.9)", 
"11,993,822 (8.9)", "41,785,216 (31.1)", "222,678,517 (165.9)", 
"113882842"), V22 = c("s_bb13044", "102,379,238", "102,298,930", 
"89,748,321 (87.7)", "80,618,042 (78.7)", "9,130,279 (8.9)", 
"35,746,929 (34.9)", "191,239,060 (186.8)", "89748321"), V23 = c("s_bb13315", 
"107,651,934", "107,568,111", "93,194,746 (86.6)", "83,514,090 (77.6)", 
"9,680,656 (9.0)", "33,353,018 (31.0)", "173,910,437 (161.5)", 
"93194746"), V24 = c("s_bb98005", "112,105,860", "112,016,935", 
"94,369,236 (84.2)", "84,816,813 (75.7)", "9,552,423 (8.5)", 
"36,967,227 (33.0)", "198,202,069 (176.8)", "94369236"), V25 = c("s_bb98262", 
"102,808,398", "102,728,580", "88,127,185 (85.7)", "76,617,237 (74.5)", 
"11,509,948 (11.2)", "37,153,641 (36.1)", "209,289,438 (203.6)", 
"88127185"), V2 = c("s_bb10008", "340,499,070", "340,422,280", 
"284,816,399 (83.6)", "248,762,075 (73.1)", "36,054,324 (10.6)", 
"119,359,928 (35.1)", "633,471,756 (186.0)", "284816399"), V3 = c("s_bb10236", 
"169,673,950", "169,651,345", "135,795,507 (80.0)", "121,626,439 (71.7)", 
"14,169,068 (8.4)", "53,375,589 (31.5)", "285,082,193 (168.0)", 
"135795507"), V4 = c("s_bb98320", "179,380,730", "179,357,440", 
"147,567,698 (82.3)", "129,896,840 (72.4)", "17,670,858 (9.9)", 
"61,174,456 (34.1)", "330,677,695 (184.3)", "147567698"), V5 = c("s_bb98324", 
"165,767,984", "165,747,538", "140,771,037 (84.9)", "123,432,263 (74.5)", 
"17,338,774 (10.5)", "57,999,533 (35.0)", "319,470,580 (192.7)", 
"140771037"), V6 = c("s_bb99097", "257,886,536", "257,843,755", 
"223,556,514 (86.7)", "197,410,263 (76.5)", "26,146,251 (10.1)", 
"97,530,807 (37.8)", "548,977,311 (212.9)", "223556514"), V7 = c("s_bb99151", 
"261,480,608", "261,434,148", "226,628,485 (86.7)", "199,107,889 (76.1)", 
"27,520,596 (10.5)", "99,517,980 (38.1)", "553,777,854 (211.8)", 
"226628485"), V8 = c("s_bb99166", "240,891,618", "240,837,051", 
"204,623,466 (84.9)", "180,740,549 (75.0)", "23,882,917 (9.9)", 
"88,254,170 (36.6)", "479,412,236 (199.0)", "204623466")), .Names = c("V2", 
"V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", 
"V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21", 
"V22", "V23", "V24", "V25", "V2", "V3", "V4", "V5", "V6", "V7", 
"V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16", 
"V17", "V18", "V19", "V20", "V21", "V22", "V23", "V24", "V25", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8"), row.names = c("samples", 
"Total reads", "Used reads", "Mapped reads", "Mapped reads (Genome)", 
"Mapped reads (Junction)", "Gene count", "Exon count", "mapped.reads.num"
), class = "data.frame")

回答1:


Based on the dput ouput the gtable[7:61] are 'character' class. We convert those to 'numeric', unlist the subset of 'stats', replicate it to make the length the same as the 'gtable' subset by using the convenient function col, and divide.

gtable[7:61] <- lapply(gtable[7:61], as.numeric)
gtable[7:61]/as.numeric(unlist(stats["mapped.reads.num",]))[col(gtable[7:61])]


来源:https://stackoverflow.com/questions/33150534/divide-multiple-columns-of-one-data-frame-by-row-names-value-of-another-datafram

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!