Search code examples
rtranspose

How do I switch my column and rows while keeping observations assigned to their respective values?


I am struggling with tidying up the following dataset:

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   19506 obs. of  172 variables:
 $ 1  : chr  "Type" "400451" "9899" "348093" ...
 $ 2  : chr  "M" "1.2575" "-0.1454" "2.1116" ...
 $ 3  : chr  "M" "1.3275" "-0.0884" "1.9335" ...
 $ 4  : chr  "M" "0.928" "-0.5821" "1.9764" ...
 $ 5  : chr  "T" "0.7097" "-0.2078" "1.6443" ...
 $ 6  : chr  "M" "1.0658" "1.7897" "-0.3944" ...
 $ 7  : chr  "M" "0.1221" "-1.3247" "-1.0067" ...
 $ 8  : chr  "M" "0.4503" "1.1782" "0.4839" ...
 $ 9  : chr  "M" "0.94" "-0.6168" "0.0222" ...
 $ 10 : chr  "T" "0.4121" "0.0528" "-0.1973" ...
 $ 11 : chr  "M" "1.3295" "0.0976" "1.1886" ...
 $ 12 : chr  "M" "0.83" "-1.0897" "0.7189" ...
 $ 13 : chr  "M" "0.6119" "-1.2696" "-0.3148" ...
 $ 14 : chr  "M" "0.58" "-1.2093" "1.5396" ...
 $ 15 : chr  "M" "0.3742" "-0.4551" "0.2487" ...
 $ 16 : chr  "M" "0.0755" "-1.2964" "0.2243" ...
 $ 17 : chr  "T" "0.0846" "-0.7627" "0.9797" ...
 $ 18 : chr  "M" "0.1758" "-1.1776" "0.309" ...
 $ 19 : chr  "M" "-0.7555" "-1.1323" "1.07" ...
 $ 20 : chr  "M" "0.9497" "-0.1933" "-0.2824" ...

The first row is "Type" and is either "M" or "T". This is an observation and I would like to move it to be a row rather than a column. The issue is the first value of each row (column 1) designates the following values. Here is what it looks like, and what I would like it to look like:

What it is now

> dput(fhcrc_dput)
structure(list(`1` = c("Cancer Type", "400451", "9899", "348093", 
"57099", "57082", "64718", "5619", "9100"), `2` = c("M", "1.2575", 
"-0.1454", "2.1116", "0.579", "0.955", "0.2545", "-0.9899", "1.2995"
), `3` = c("M", "1.3275", "-0.0884", "1.9335", "0.0167", "1.0678", 
"-0.4117", "-0.1657", "0.2189"), `4` = c("M", "0.928", "-0.5821", 
"1.9764", "0.8708", "0.9526", "-1.7639", "0.979", "0.7118"), 
    `5` = c("T", "0.7097", "-0.2078", "1.6443", "0.2616", "0.7887", 
    "-0.0658", "0.6369", "0.6559"), `6` = c("M", "1.0658", "1.7897", 
    "-0.3944", "1.0882", "-2.1511", "-2.2967", "-0.7775", "-0.7927"
    ), `7` = c("M", "0.1221", "-1.3247", "-1.0067", "-0.2519", 
    "-1.7865", "1.6031", "-0.1751", "0.5607"), `8` = c("M", "0.4503", 
    "1.1782", "0.4839", "0.6171", "-1.1749", "-0.2469", "-0.3145", 
    "-0.3991"), `9` = c("M", "0.94", "-0.6168", "0.0222", "0.3043", 
    "-0.3958", "0.6127", "-1.1389", "0.372"), `10` = c("T", "0.4121", 
    "0.0528", "-0.1973", "-0.2621", "-0.36", "1.2994", "0.1884", 
    "-0.0119"), `11` = c("M", "1.3295", "0.0976", "1.1886", "0.8967", 
    "0.9029", "1.7968", "-0.0256", "-0.7882"), `12` = c("M", 
    "0.83", "-1.0897", "0.7189", "1.0855", "0.8218", "0.373", 
    "-0.2197", "-0.3914"), `13` = c("M", "0.6119", "-1.2696", 
    "-0.3148", "-0.4936", "-1.1412", "-0.3886", "0.4914", "-0.6463"
    ), `14` = c("M", "0.58", "-1.2093", "1.5396", "-0.6586", 
    "1.4827", "-0.2693", "0.707", "0.0025"), `15` = c("M", "0.3742", 
    "-0.4551", "0.2487", "-0.0584", "-0.4608", "0.024", "-0.8955", 
    "-0.2082"), `16` = c("M", "0.0755", "-1.2964", "0.2243", 
    "-0.2862", "-0.2829", "-0.3147", "-0.4", "0.0113"), `17` = c("T", 
    "0.0846", "-0.7627", "0.9797", "-2.937", "-0.0584", "1.0808", 
    "-0.0654", "-1.3395"), `18` = c("M", "0.1758", "-1.1776", 
    "0.309", "-0.5905", "0.8577", "0.7597", "0.267", "0.2166"
    ), `19` = c("M", "-0.7555", "-1.1323", "1.07", "-0.5069", 
    "0.3271", "-0.5159", "-0.5847", "1.3"), `20` = c("M", "0.9497", 
    "-0.1933", "-0.2824", "1.1376", "-1.2679", "-1.7647", "-0.9387", 
    "0.6249"), `21` = c("M", "0.9459", "0.3495", "-0.277", "1.4102", 
    "-1.222", "-1.2328", "-0.3862", "0.5943"), `22` = c("M", 
    "-0.65", "-1.0268", "-1.2453", "0.7603", "1.0068", "0.5931", 
    "0.6711", "-1.7322"), `23` = c("M", "-3.0292", "-0.5336", 
    "-0.7234", "0.8514", "0.7217", "0.7033", "-0.0705", "-0.6"
    ), `24` = c("M", "-0.9559", "-0.9856", "1.2736", "-0.5379", 
    "1.1214", "0.8179", "0.7962", "-1.282"), `25` = c("M", "-0.7204", 
    "0.6786", "1.3822", "0.1713", "0.9529", "-0.6079", "-1.2844", 
    "-1.9962"), `26` = c("M", "-1.339", "0.1339", "-1.0053", 
    "0.5974", "-1.6252", "-3.2053", "-0.2687", "-0.9268"), `27` = c("M", 
    "0.6068", "-0.8974", "-1.3648", "1.1643", "1.1041", "0.603", 
    "-0.5545", "-0.2975"), `28` = c("T", "0.1255", "0.3037", 
    "1.22", "1.3381", "1.1223", "0.4463", "0.6065", "-1.1376"
    ), `29` = c("M", "0.6895", "-0.2836", "1.1811", "0.5499", 
    "-0.0749", "-1.1233", "-0.8967", "-1.5915"), `30` = c("M", 
    "1.0583", "1.001", "-1.163", "0.7774", "0.9795", "-0.7429", 
    "-0.4362", "1.1313"), `31` = c("M", "0.873", "0.5164", "-1.3186", 
    "1.541", "0.6507", "0.1247", "-0.2046", "1.8289"), `32` = c("M", 
    "0.3912", "-1.1097", "-0.1967", "0.0913", "-0.7546", "-2.1291", 
    "-0.1827", "0.2748"), `33` = c("M", "0.7857", "0.1292", "0.047", 
    "-1.4175", "-1.8012", "1.048", "0.6682", "-0.6392"), `34` = c("M", 
    "0.9366", "-0.5076", "-0.9124", "-1.3523", "0.9254", "1.1485", 
    "0.4214", "-0.7012"), `35` = c("M", "1.4286", "0.2583", "0.9995", 
    "-1.6214", "0.5682", "1.6275", "1.8373", "-2.4776"), `36` = c("M", 
    "-0.2608", "-0.941", "1.0473", "0.7506", "1.4833", "-0.8638", 
    "1.7668", "0.4221"), `37` = c("M", "0.2476", "-0.3212", "1.3935", 
    "-0.4963", "1.6396", "-0.8564", "-1.51", "0.0176"), `38` = c("M", 
    "-0.9719", "-0.9647", "-0.6476", "0.4465", "-1.8286", "-0.652", 
    "0.4552", "0.1291"), `39` = c("M", "1.9382", "-1.1389", "1.1881", 
    "-1.0042", "-1.4342", "0.9411", "3.1536", "0.4231"), `40` = c("M", 
    "0.0465", "0.8693", "0.8739", "0.5776", "1.6157", "-2.8256", 
    "0.3568", "-0.2769"), `41` = c("M", "1.2434", "-1.3256", 
    "1.6446", "0.2237", "-1.497", "0.0776", "-1.1762", "0.3318"
    ), `42` = c("M", "0.5723", "-0.3898", "1.3301", "-1.1684", 
    "-2.2287", "-0.3115", "-1.2297", "2.4811"), `43` = c("M", 
    "-1.6681", "1.0222", "-1.2662", "-1.9253", "-0.1715", "-1.0619", 
    "-0.1994", "-0.9892"), `44` = c("M", "-0.7116", "0.3928", 
    "-0.0106", "-3.4471", "2.0327", "0.122", "1.8086", "0.0496"
    ), `45` = c("M", "-1.3924", "1.4804", "-0.6911", "-0.9529", 
    "1.3117", "0.7396", "0.6516", "-0.9249"), `46` = c("M", "-0.2372", 
    "1.0534", "-0.6773", "-1.4936", "1.3616", "0.0393", "2.098", 
    "0.2936"), `47` = c("M", "-0.6616", "-0.4986", "-0.3795", 
    "-0.0817", "0.8948", "1.5369", "3.9697", "-0.5429"), `48` = c("M", 
    "-0.8237", "1.663", "-0.0975", "-1.1997", "1.3639", "-0.4003", 
    "-1.2681", "1.7587"), `49` = c("T", "0.5597", "-0.784", "-1.484", 
    "1.064", "-0.6788", "0.8761", "1.2098", "-1.3219"), `50` = c("M", 
    "-0.0085", "-0.7057", "-0.1074", "0.7229", "-0.1715", "0.968", 
    "0.5943", "0.6485"), `51` = c("M", "-0.9234", "-1.3164", 
    "1.1298", "0.789", "0.1222", "0.4959", "0.2459", "0.6565"
    ), `52` = c("T", "0.4756", "-0.6995", "-0.3026", "1.4569", 
    "0.9451", "0.8624", "0.6333", "-0.8346"), `53` = c("T", "0.1992", 
    "0.1877", "0.1713", "1.3144", "-0.4802", "1.3244", "-0.6446", 
    "0.9392"), `54` = c("M", "-0.0437", "-1.1442", "0.9649", 
    "0.7897", "0.4474", "0.9554", "-1.6456", "0.5133"), `55` = c("M", 
    "-0.1093", "-0.6034", "0.9196", "-0.1674", "-0.557", "0.0604", 
    "-1.0156", "0.7881"), `56` = c("M", "0.1021", "0.207", "0.2467", 
    "0.772", "-1.2526", "-0.4443", "1.1132", "1.1145"), `57` = c("T", 
    "-2.9478", "-0.0841", "0.592", "1.0606", "0.9903", "-0.3696", 
    "-0.0152", "-0.1769"), `58` = c("M", "-1.9128", "0.2469", 
    "0.9841", "1.3688", "0.4445", "-0.7611", "-1.5429", "-0.9829"
    ), `59` = c("M", "-2.8137", "0.0626", "0.2128", "0.8947", 
    "1.6569", "-1.1533", "0.3456", "-0.016"), `60` = c("M", "-3.0029", 
    "-0.4661", "0.7943", "0.7514", "0.8933", "-0.9821", "-0.4578", 
    "-1.0991"), `61` = c("M", "-2.439", "0.1152", "0.9586", "1.1028", 
    "0.8177", "-0.7009", "-1.3575", "-0.9603"), `62` = c("M", 
    "-0.9417", "-1.3419", "-0.4844", "-0.5388", "0.8722", "1.4321", 
    "0.6553", "-0.7543"), `63` = c("M", "-0.6351", "-0.6564", 
    "-0.7578", "-1.1484", "-0.0958", "0.5959", "-0.2782", "0.148"
    ), `64` = c("M", "-0.7002", "-0.6526", "-0.6268", "-1.1608", 
    "0.5", "0.7462", "1.2573", "-0.0362"), `65` = c("T", "-0.9235", 
    "-0.3315", "-0.7322", "-1.1013", "-0.0401", "0.5743", "-0.6418", 
    "0.1016"), `66` = c("M", "-0.769", "0.1359", "-0.5188", "-1.3023", 
    "-0.5571", "-1.0192", "1.2968", "0.4597"), `67` = c("T", 
    "0.9085", "0.3999", "0.4177", "0.3866", "-2.0914", "1.3959", 
    "-0.2249", "0.2271"), `68` = c("M", "0.597", "0.4266", "0.5321", 
    "0.1193", "0.3066", "-2.0966", "-0.2996", "-0.336"), `69` = c("M", 
    "1.7257", "-0.5499", "0.8156", "0.1132", "-0.1197", "-0.6312", 
    "0.5543", "-0.5092"), `70` = c("T", "0.9762", "0.939", "1.0768", 
    "-0.0592", "0.8144", "0.9429", "2.7423", "2.0026"), `71` = c("M", 
    "0.5041", "-0.4614", "1.0334", "0.5181", "0.501", "-0.4688", 
    "-1.4262", "0.8578"), `72` = c("M", "0.3055", "0.7724", "0.9956", 
    "-0.4858", "0.3875", "-0.6435", "-0.418", "1.3757"), `73` = c("M", 
    "0.4817", "0.2392", "1.2747", "0.5939", "0.3255", "0.0592", 
    "0.1078", "1.1289"), `74` = c("M", "1.1491", "-0.5334", "1.3543", 
    "-0.153", "0.5809", "0.9287", "-0.8507", "1.3275"), `75` = c("M", 
    "1.4115", "2.218", "1.2103", "-0.8672", "-0.5119", "0.681", 
    "-0.9971", "0.1327"), `76` = c("M", "0.1631", "0.2862", "0.3319", 
    "0.3068", "-0.5513", "-0.9483", "-0.9035", "0.9987"), `77` = c("T", 
    "-0.0499", "-0.1059", "-0.3935", "-1.1677", "-0.0729", "1.4262", 
    "0.4552", "-0.5967"), `78` = c("M", "-1.0184", "-0.9104", 
    "-2.3532", "-0.5933", "-1.3503", "-1.3973", "2.353", "-0.6618"
    ), `79` = c("M", "0.8801", "1.9352", "-0.6269", "-1.1553", 
    "-0.9221", "0.6717", "-0.437", "-0.5179"), `80` = c("M", 
    "0.2397", "1.7347", "-0.4415", "-0.1544", "-0.3079", "1.2114", 
    "-0.5825", "-0.2152"), `81` = c("M", "0.8397", "2.9351", 
    "-0.9384", "-0.9485", "0.4736", "1.0199", "0.9185", "-0.975"
    ), `82` = c("M", "0.6171", "1.7608", "-1.1982", "0.1496", 
    "-1.1882", "1.1926", "1.6598", "-0.2911"), `83` = c("M", 
    "1.0558", "1.6149", "-1.4614", "-0.5264", "-0.2457", "1.4305", 
    "0.2716", "-0.2073"), `84` = c("M", "-0.2009", "1.8177", 
    "-0.6734", "-0.6491", "0.71", "1.2388", "-1.0071", "-0.9515"
    ), `85` = c("M", "0.8114", "1.8763", "0.0943", "0.7915", 
    "0.2579", "1.1019", "-0.6184", "-0.7433"), `86` = c("M", 
    "0.9221", "1.9438", "-0.5112", "-0.8264", "0.6547", "1.2048", 
    "-0.7178", "-0.4378"), `87` = c("T", "0.5109", "1.4981", 
    "-1.6571", "-1.3225", "1.0651", "0.8302", "0.6239", "-0.2862"
    ), `88` = c("M", "0.4301", "2.0377", "-0.7916", "-0.9694", 
    "0.7127", "1.3464", "-1.4886", "-1.2279"), `89` = c("M", 
    "1.1538", "1.9375", "-0.5245", "-0.578", "0.3223", "0.9641", 
    "-0.3459", "0.7562"), `90` = c("M", "1.1161", "2.0144", "-0.882", 
    "-0.6457", "0.4954", "0.9684", "-0.0984", "-1.2806"), `91` = c("M", 
    "0.5713", "1.9659", "-0.9762", "0.1543", "0.387", "1.073", 
    "0.1034", "-0.9336"), `92` = c("M", "0.622", "-0.2525", "0.3313", 
    "-0.3944", "0.2385", "0.5251", "-0.4036", "-0.2153"), `93` = c("M", 
    "1.0209", "-1.2081", "0.1944", "0.6992", "1.4776", "-1.22", 
    "-0.2436", "2.029"), `94` = c("M", "1.7941", "-1.2681", "0.5402", 
    "0.8792", "0.9088", "-0.0335", "0.5863", "0.9746"), `95` = c("M", 
    "-0.1809", "0.3666", "1.1836", "0.4725", "1.3514", "0.2656", 
    "-0.2934", "1.1738"), `96` = c("M", "-1.3082", "-0.1856", 
    "1.5561", "0.7852", "-1.2223", "-0.2447", "1.0086", "1.2872"
    ), `97` = c("T", "-0.2794", "1.7535", "1.2718", "-1.0445", 
    "-1.5595", "0.0158", "0.7153", "-0.1177"), `98` = c("M", 
    "-0.7495", "0.5431", "0.7736", "-0.2247", "0.3518", "-0.5478", 
    "0.3049", "-0.012"), `99` = c("M", "0.397", "-1.3494", "-0.7259", 
    "1.9184", "-1.4019", "0.3732", "0.1693", "-0.5715"), `100` = c("M", 
    "-0.1553", "-0.6815", "-1.5664", "0.8553", "0.425", "0.3736", 
    "0.2924", "-0.5639"), `101` = c("M", "-0.3267", "-0.4253", 
    "-0.4179", "0.4243", "-0.3665", "0.9297", "-1.9835", "0.0793"
    ), `102` = c("M", "-0.5623", "-1.1916", "0.3568", "0.1833", 
    "-1.2429", "0.6394", "-1.2554", "0.7428"), `103` = c("M", 
    "0.2869", "1.5131", "1.2351", "0.4146", "0.1532", "1.071", 
    "-0.9502", "-0.579"), `104` = c("M", "-0.1016", "-0.8778", 
    "-0.2123", "-0.0771", "-0.5467", "-0.3485", "-0.7596", "-0.5026"
    ), `105` = c("M", "-0.0515", "-1.115", "0.5066", "1.1975", 
    "-1.6136", "0.2882", "0.8069", "0.2485"), `106` = c("M", 
    "-0.4389", "0.2405", "-1.4605", "0.2357", "-1.2978", "-0.822", 
    "1.6014", "-0.3971"), `107` = c("M", "-0.7161", "-0.8751", 
    "-0.5101", "1.1332", "-0.0548", "-0.5167", "-0.5852", "-0.9426"
    ), `108` = c("M", "-0.148", "-1.1844", "-0.3226", "1.3795", 
    "-0.9411", "-1.3328", "-0.0893", "-1.6291"), `109` = c("T", 
    "0.1279", "-0.7482", "1.5869", "-1.2163", "0.9793", "-0.078", 
    "-1.1134", "2.5832"), `110` = c("M", "-0.3348", "-0.3684", 
    "-0.42", "-0.9305", "-0.4861", "-0.7106", "-1.7698", "0.5639"
    ), `111` = c("M", "-1.0393", "-1.4821", "-0.2824", "-0.4501", 
    "0.7964", "-0.498", "-0.9897", "2.8851"), `112` = c("M", 
    "-1.0647", "-0.7958", "0.1788", "0.6122", "1.0087", "-0.1729", 
    "-0.042", "1.5412"), `113` = c("M", "-0.7039", "-0.7314", 
    "-0.0455", "-0.3977", "0.9964", "-0.5168", "-0.6166", "1.6622"
    ), `114` = c("M", "0.1621", "-0.0616", "-0.0651", "-0.4971", 
    "0.994", "-1.3211", "-0.1984", "1.4424"), `115` = c("M", 
    "-0.2008", "-0.7285", "1.3667", "-0.9898", "1.1621", "-0.166", 
    "0.6095", "1.6038"), `116` = c("M", "-0.3433", "-1.3273", 
    "-0.6079", "-1.6232", "1.1541", "-0.6451", "1.6083", "0.9281"
    ), `117` = c("M", "-1.3378", "-1.232", "-0.4819", "0.2133", 
    "-0.4676", "-0.4283", "-0.2766", "0.3001"), `118` = c("M", 
    "-0.9899", "1.8628", "-0.8474", "0.3539", "1.3119", "-0.1519", 
    "-0.1965", "-1.9985"), `119` = c("M", "0.4772", "-0.4662", 
    "-1.4437", "1.6259", "-1.7169", "0.9521", "0.4426", "-0.1736"
    ), `120` = c("M", "-0.4164", "-0.0181", "-2.5094", "0.052", 
    "-0.1032", "0.1192", "-0.5941", "0.3789"), `121` = c("M", 
    "-1.0691", "-1.3426", "0.8019", "-0.0605", "-1.2527", "-0.513", 
    "-0.1962", "-0.7124"), `122` = c("M", "0.2506", "-0.4603", 
    "-0.4948", "0.951", "-1.2247", "0.1446", "-0.0483", "0.6212"
    ), `123` = c("M", "0.4638", "-0.6021", "-0.7046", "0.9174", 
    "-1.011", "-0.1069", "0.8625", "0.4636"), `124` = c("M", 
    "-0.9399", "-0.0474", "1.3111", "0.3876", "-0.7755", "0.0892", 
    "0.5118", "0.6739"), `125` = c("M", "0.1801", "-0.6352", 
    "-1.9097", "0.6343", "-1.7559", "0.6839", "0.9365", "-0.0456"
    ), `126` = c("M", "-0.0234", "-0.6471", "-1.1087", "1.6507", 
    "-1.3148", "0.0499", "-0.3075", "0.358"), `127` = c("T", 
    "-0.0178", "0.6062", "-1.7451", "-0.042", "-0.8715", "-0.0458", 
    "-0.5508", "-0.91"), `128` = c("M", "-0.475", "-0.686", "-1.7519", 
    "0.5034", "-1.0186", "-0.697", "0.4672", "-0.1863"), `129` = c("M", 
    "-0.6116", "1.4331", "-0.9939", "0.3622", "-0.6085", "-0.6596", 
    "0.0531", "-0.4025"), `130` = c("M", "-1.5522", "0.0197", 
    "-0.2849", "-0.0462", "-0.348", "-1.2656", "-0.6432", "-0.4144"
    ), `131` = c("M", "0.4793", "-0.5161", "-1.0353", "-0.4972", 
    "0.7406", "-1.4817", "-0.0202", "-0.9747"), `132` = c("M", 
    "0.6518", "-0.3765", "1.6492", "-0.9119", "1.629", "1.6337", 
    "0.8544", "0.9539"), `133` = c("M", "-3.5086", "-0.6348", 
    "-0.9323", "0.1988", "0.6334", "0.1559", "2.6354", "1.4016"
    ), `134` = c("M", "1.5594", "0.8408", "0.7516", "1.261", 
    "-0.7557", "1.3559", "0.7549", "-1.9106"), `135` = c("M", 
    "0.9966", "0.0128", "0.2559", "0.8849", "-1.1407", "1.3868", 
    "0.0994", "-0.5223"), `136` = c("M", "0.6223", "0.6908", 
    "0.4832", "1.3769", "0.0009", "1.0522", "0.0587", "-0.2298"
    ), `137` = c("M", "0.8884", "-0.383", "0.1536", "1.059", 
    "-1.8859", "-0.4138", "-0.2171", "2.3357"), `138` = c("M", 
    "-0.1823", "0.9891", "2.159", "0.4245", "0.95", "-0.8138", 
    "-1.0705", "0.4354"), `139` = c("M", "0.3295", "1.3506", 
    "1.5812", "-0.4761", "-0.0507", "-0.6293", "-0.8422", "-0.4496"
    ), `140` = c("M", "-0.4033", "-1.0864", "-0.8222", "-1.0034", 
    "-0.0538", "-0.2127", "-0.9512", "-1.0263"), `141` = c("M", 
    "0.2786", "-0.7095", "-0.6584", "-0.7525", "-0.3022", "-0.1588", 
    "0.6861", "-0.0092"), `142` = c("T", "-1.8777", "0.1257", 
    "-0.7251", "-0.9724", "0.3787", "-0.0521", "-0.7073", "0.4882"
    ), `143` = c("M", "-0.035", "-0.3962", "-0.5153", "0.05", 
    "-0.1692", "0.5034", "-1.1066", "0.6867"), `144` = c("T", 
    "0.061", "1.1142", "-0.2166", "-0.1613", "0.2642", "0.3254", 
    "-0.9376", "0.0367"), `145` = c("M", "-2.3095", "-0.2388", 
    "-1.0424", "0.7211", "1.7163", "-2.4657", "2.5072", "0.4637"
    ), `146` = c("M", "-0.3177", "-0.6006", "-1.0596", "0.759", 
    "0.773", "0.9819", "-0.628", "-0.3971"), `147` = c("M", "-0.0391", 
    "-0.0546", "-0.3335", "-0.8924", "-0.5203", "-1.3242", "0.0444", 
    "-0.1828"), `148` = c("T", "0.5056", "0.0312", "0.489", "-0.713", 
    "-0.4855", "-0.7392", "-0.3404", "-1.6624"), `149` = c("T", 
    "0.7703", "-0.2244", "-1.361", "-0.3847", "0.0356", "-2.976", 
    "-0.1839", "-3.1214"), `150` = c("M", "1.7033", "-0.6026", 
    "-1.659", "-0.4858", "-1.5274", "0.8073", "-0.0252", "-0.3191"
    ), `151` = c("M", "0.4129", "0.3076", "-0.7818", "-0.5489", 
    "-0.477", "0.7811", "-0.7317", "-0.3063"), `152` = c("M", 
    "0.6783", "-0.0628", "-0.0402", "-1.2769", "-1.1288", "-0.4143", 
    "1.7316", "-0.8931"), `153` = c("M", "1.0192", "-0.3054", 
    "-0.5921", "1.2258", "0.1344", "1.1929", "-1.1172", "0.8213"
    ), `154` = c("M", "0.1607", "-0.9311", "-0.0737", "1.4171", 
    "0.0726", "-0.3417", "-1.2246", "-0.3158"), `155` = c("M", 
    "1.6854", "-0.1367", "-0.8133", "1.3381", "0.205", "0.3212", 
    "-0.1022", "-0.8816"), `156` = c("M", "0.2821", "-0.5688", 
    "0.2083", "1.6888", "0.5855", "0.0818", "-0.5009", "1.4748"
    ), `157` = c("M", "1.1446", "1.8614", "0.298", "1.0187", 
    "-0.648", "-1.4591", "0.9102", "1.3019"), `158` = c("M", 
    "0.6928", "1.4823", "-0.5615", "1.597", "0.4342", "-0.2836", 
    "1.6071", "0.0874"), `159` = c("T", "-0.1953", "-0.9879", 
    "1.4138", "1.0527", "-1.4272", "1.5324", "-0.4214", "-0.9917"
    ), `160` = c("M", "0.3124", "-0.1237", "2.0012", "0.7713", 
    "-1.0965", "-0.3225", "-1.0051", "0.0255"), `161` = c("M", 
    "1.4984", "-0.4291", "-0.6132", "1.0182", "0.3818", "0.0381", 
    "0.4984", "-0.4618"), `162` = c("M", "-1.4916", "1.8228", 
    "-1.4599", "-0.0389", "-0.2048", "0.2717", "-0.7729", "-0.0581"
    ), `163` = c("M", "-0.3305", "0.7849", "-1.4079", "-3.6095", 
    "-2.0851", "1.0226", "1.2391", "-2.2932"), `164` = c("M", 
    "-0.7542", "2.0434", "-1.2585", "-1.3704", "-1.0982", "-0.6049", 
    "0.2776", "-0.3835"), `165` = c("T", "-1.5508", "1.5687", 
    "-0.7557", "-1.3412", "-1.4012", "-2.0327", "-0.7975", "-1.0566"
    ), `166` = c("M", "-1.0494", "-1.0584", "0.2337", "-0.4735", 
    "0.7736", "-0.4517", "1.384", "-0.9928"), `167` = c("M", 
    "-1.068", "-1.1204", "0.4303", "-0.7373", "-0.0487", "-1.7436", 
    "-0.6885", "0.7059"), `168` = c("M", "0.0398", "0.3073", 
    "0.8069", "-1.4621", "0.6946", "-0.3977", "-1.3762", "0.7399"
    ), `169` = c("M", "0.576", "-1.1043", "0.5374", "-1.8338", 
    "0.6311", "1.4223", "-1.0269", "-0.0234"), `170` = c("M", 
    "-0.1383", "0.419", "0.73", "-1.2268", "1.3396", "-0.1392", 
    "-0.6297", "0.4873"), `171` = c("M", "-0.2585", "-0.1363", 
    "0.469", "0.0006", "1.232", "0.1844", "-0.6289", "0.4616"
    ), `172` = c("M", "-0.6862", "1.2348", "0.2574", "-1.6054", 
    "1.0088", "-0.355", "0.5984", "1.548")), row.names = c(NA, 
9L), class = c("tbl_df", "tbl", "data.frame"))

How I want it

> dput(df)
structure(list(Type = c("M", "T", "M", "M", "T"), Gene_Name = c("101", 
"1231", "3342", "4342", "324"), Z_Score = c("1", "-.32", ".2", 
".53", "-1")), class = "data.frame", row.names = c(NA, -5L))

I suppose this would be considered "Tidying" the data? I am really lost.

Any and all help appreciated.


Solution

  • A proposition using the melt() function of the data.table package :

    df <- read.table(header = FALSE, text = "
    Gene_Name  M       M       M       T      M
    400451     1.2575  1.3275  0.928   0.7097 1.0658
      9899    -0.1454 -0.0884 -0.5821 -0.2078 1.7897
    ")
    
    names(df) <- df[1,]
    df <- df[-1,]
    df
    #>   Gene_Name       M       M       M       T      M
    #> 2    400451  1.2575  1.3275   0.928  0.7097 1.0658
    #> 3      9899 -0.1454 -0.0884 -0.5821 -0.2078 1.7897
    
    data.table::melt(data = data.table::setDT(df),
                     id.vars = "Gene_Name",
                     mesure.vars = names(df[,-1]),
                     variable.name = "Type",
                     value.name = "Z_Score")
    #>     Gene_Name Type Z_Score
    #>  1:    400451    M  1.2575
    #>  2:      9899    M -0.1454
    #>  3:    400451    M  1.3275
    #>  4:      9899    M -0.0884
    #>  5:    400451    M   0.928
    #>  6:      9899    M -0.5821
    #>  7:    400451    T  0.7097
    #>  8:      9899    T -0.2078
    #>  9:    400451    M  1.0658
    #> 10:      9899    M  1.7897
    
    # Created on 2021-01-29 by the reprex package (v0.3.0.9001)
    

    Regards,