当列和行名称匹配时,如何根据矩阵值获取百分比值

时间:2015-11-17 22:43:10

标签: r

我有一个矩阵如下

    structure(c(4407, 0, 53, 46, 50, 0, 4407, 0, 0, 0, 53, 0, 742, 
408, 411, 46, 0, 408, 688, 464, 50, 0, 411, 464, 672, 40, 0, 
447, 359, 382, 72, 0, 439, 342, 356, 35, 0, 350, 321, 344, 45, 
0, 365, 341, 364, 42, 0, 498, 372, 376, 102, 0, 304, 333, 340, 
52, 0, 396, 354, 343, 52, 0, 377, 371, 374, 80, 0, 458, 374, 
385, 54, 0, 509, 367, 365, 52, 0, 416, 322, 352, 47, 0, 505, 
368, 384, 85, 0, 338, 358, 374, 45, 0, 396, 328, 330, 45, 0, 
351, 381, 358, 53, 0, 362, 293, 301, 52, 0, 396, 340, 346, 55, 
0, 385, 343, 340, 52, 0, 493, 406, 406, 49, 0, 516, 384, 416, 
29, 0, 344, 330, 335, 32, 0, 326, 299, 314, 50, 0, 520, 372, 
376, 33, 0, 348, 336, 340, 33, 0, 380, 329, 358, 31, 0, 354, 
337, 349, 33, 0, 320, 301, 336, 33, 0, 341, 316, 347, 52, 0, 
340, 316, 329, 48, 0, 522, 388, 389, 56, 0, 300, 362, 334, 57, 
0, 303, 327, 301, 44, 0, 487, 401, 400, 51, 0, 439, 362, 373, 
38, 0, 361, 336, 353, 38, 0, 328, 315, 337, 52, 0, 561, 403, 
402, 44, 0, 459, 386, 393, 37, 0, 381, 340, 337, 37, 0, 405, 
344, 343, 27, 0, 335, 346, 347, 31, 0, 370, 370, 385, 31, 0, 
369, 340, 348, 54, 0, 326, 305, 335, 39, 0, 368, 343, 357, 38, 
0, 379, 347, 367, 42, 0, 527, 389, 393, 56, 0, 382, 504, 471, 
56, 0, 382, 504, 471, 51, 0, 459, 379, 394, 41, 0, 507, 393, 
381, 68, 0, 475, 396, 398, 30, 0, 386, 362, 371, 34, 0, 378, 
363, 379, 24, 0, 341, 317, 350, 35, 0, 378, 348, 364, 34, 0, 
334, 327, 345, 28, 0, 323, 311, 322, 47, 0, 486, 365, 378, 52, 
0, 479, 395, 414, 40, 0, 391, 327, 350, 49, 0, 363, 344, 370, 
22, 0, 342, 329, 338, 51, 0, 381, 463, 440, 46, 0, 389, 460, 
476, 46, 0, 487, 371, 385, 35, 0, 331, 307, 330, 47, 0, 522, 
388, 405, 39, 0, 359, 352, 365, 42, 0, 369, 342, 356, 27, 0, 
364, 352, 364, 38, 0, 377, 327, 342, 29, 0, 369, 346, 380, 37, 
0, 382, 330, 355, 29, 0, 291, 245, 275, 30, 0, 354, 332, 358, 
31, 0, 338, 310, 326, 37, 0, 347, 325, 356, 46, 0, 505, 385, 
377, 44, 0, 504, 411, 430, 34, 0, 315, 310, 333, 37, 0, 392, 
367, 373, 60, 0, 339, 324, 352, 48, 0, 475, 385, 402, 48, 0, 
376, 342, 314, 1, 0, 0, 0, 0), .Dim = c(5L, 91L), .Dimnames = list(
    c("chr", "leftPos", "OC_AH_026C.res", "OC_AH_026C.1.res", 
    "OC_AH_026C.2.res"), c("chr", "leftPos", "OC_AH_026C.res", 
    "OC_AH_026C.1.res", "OC_AH_026C.2.res", "OC_AH_026T.res", 
    "OC_AH_058T.res", "OC_AH_084C.res", "OC_AH_084T.res", "OC_AH_086C.res", 
    "OC_AH_086C.1.res", "OC_AH_086C.2.res", "OC_AH_086C.3.res", 
    "OC_AH_086T.res", "OC_AH_088C.res", "OC_AH_088T.res", "OC_AH_094C.res", 
    "OC_AH_094C.1.res", "OC_AH_094C.2.res", "OC_AH_094C.3.res", 
    "OC_AH_094C.4.res", "OC_AH_094C.5.res", "OC_AH_094C.6.res", 
    "OC_AH_094C.7.res", "OC_AH_094T.res", "OC_AH_096C.res", "OC_AH_096T.res", 
    "OC_AH_100C.res", "OC_AH_100C.1.res", "OC_AH_100T.res", "OC_AH_127C.res", 
    "OC_AH_127T.res", "OC_AH_133C.res", "OC_AH_133T.res", "OC_ED_008C.res", 
    "OC_ED_008C.1.res", "OC_ED_008C.2.res", "OC_ED_008C.3.res", 
    "OC_ED_008T.res", "OC_ED_016C.res", "OC_ED_016T.res", "OC_ED_031C.res", 
    "OC_ED_031T.res", "OC_ED_036C.res", "OC_ED_036T.res", "OC_GS_001C.res", 
    "OC_GS_001T.res", "OC_QE_062C.res", "OC_QE_062T.res", "OC_RS_010C.res", 
    "OC_RS_010T.res", "OC_RS_027C.res", "OC_RS_027C.1.res", "OC_RS_027C.2.res", 
    "OC_RS_027T.res", "OC_SH_051C.res", "OC_SH_051T.res", "OC_ST_014C.res", 
    "OC_ST_014C.1.res", "OC_ST_014T.res", "OC_ST_016T.res", "OC_ST_020C.res", 
    "OC_ST_020T.res", "OC_ST_024C.res", "OC_ST_024T.res", "OC_ST_033C.res", 
    "OC_ST_033T.res", "OC_ST_034C.res", "OC_ST_034C.1.res", "OC_ST_034C.2.res", 
    "OC_ST_034T.res", "OC_ST_035C.res", "OC_ST_035T.res", "OC_ST_036C.res", 
    "OC_ST_036T.res", "OC_ST_037T.res", "OC_ST_040C.res", "OC_ST_040T.res", 
    "OC_WG_001T.res", "OC_WG_002C.res", "OC_WG_002T.res", "OC_WG_005C.res", 
    "OC_WG_005T.res", "OC_WG_006C.res", "OC_WG_006T.res", "OC_WG_009T.res", 
    "OC_WG_019C.res", "OC_WG_019T.res", "Means.res", "sd.res", 
    "count")))

我希望在rowname和columnname匹配时从每列获取值,并在计算列方式百分比时使用此值作为分母

到目前为止,我有这个:

 NewMatrix <- t(sapply(row.names(res_matrix),
                        function(x) substr(x, 1, 6) == substr(colnames(res_matrix), 1, 6)))

这给了我匹配的真或假,但是我如何获取真值并用它来为该列中的其他值做百分比?

1 个答案:

答案 0 :(得分:-1)

你想要这样的东西:

library(dplyr)
library(tidyr)

long_data = 
  data %>%
  as.data.frame %>%
  mutate(rowname = rownames(.) ) %>%
  gather(variable, value, OC_AH_026C.res:OC_WG_019T.res)

matches = 
  long_data %>%
  filter(rowname == variable) %>%
  select(rowname, control_value = value)

long_result =
  long_data %>%
  left_join(matches) %>%
  mutate(ratio = value / control_value)

wide_result = 
  long_result %>%
  select(-value, -control_value) %>%
  spread(variable, ratio)