将命名向量(来自TukeyHSD)转换为表格

时间:2018-11-27 00:42:48

标签: r

我有一个命名向量,它是TukeyHSD输出的一部分:

"a-b" "a-c" "b-c"
  1     2     3

有没有一种很好的方法可以将其转换为

    a  b  c
  -----------
a |    1  2
b | 1     3
c | 2  3  

?还是我只是对其进行破解并拆分字符串等等

预先感谢

这是dput输出:

c( ba = 0.132736594352255, ca = 0.999921265128142, da = 0.801948385014406, ea = 1, fa = 0.999999993422497, ga = 0.999994493135286, ha = 0.210958663031597, ia = 1.8980767408916e-06, ja = 0.973055228316854, ka = 0.999999989980955, ma = 0.999999999847843, na = 0.00176609502582648, pa = 0.738916850855589, cb = 0.0648789689498736, db = 0.00274896267890112, eb { {1}} fb = 0.217149424299426, gb = 0.0563761493313133, hb = 0.830288996389741, ib = 1.37129030743921e-05, jb = 0.292237803866045, kb = 0.00203363512911592, mb = 0.429789314314218, nb {{1 }} pb {= 0.893873188901534, dc = 1.26129400479513e-07, ec = 0.0148404046180679, fc = 0.999413140979136, gc = 0.999983194608954, hc = 0.999999593776059, ic = 0.99450256286738, jc = 0.995948206958786, kc = 2.12711732894277e-06, mc = 0.999999999953707, nc = 0.99849672006282, pc = 0.999945483624277, ed = 0.159133209452148, fd = 0.981199963747256, gd = 0.894189247372541, hd = 0.927465343408835, id { {1}} jd = 0.679255997014174, kd = 1, md = 2.48392421076105e-08, nd = 0.999592315002344, pd = 0.697136248874886, fe = 0.958196387101045, ge = 0.823800464142429,他{{1 }} ie = 0.999990479586194, je = 0.999999999789834, ke = 0.999995787557262, = 0.53201292841468, ne = 1.03081411517714e-05, pe = 0.995077423573089, gf = 0.999999990349601, hf = 0.999999999728218, if = 0.00827803916192571, jf = 0.801239862126994, kf = 0.999679361401266, mf = 0.464857165472924, nf = 4.11179760018143e-07, pf = 0.998610646241627, hg = 0.999980920365226, ig = 0.999999813932987, jg { {1}} kg = 0.00531987647573118, mg = 0.848849590006324, ng = 0.341590849630092, pg = 0.00156077688059131, ih = 0.914056068033521, jh = 0.999999999582086, kh = 0.999999999998442, mh {{1 }} nh = 0.00550711110598001, ph = 0.589694361848451, ji = 1.32359012638972e-10, ki = 0.981457563281897, mi = 0.217452285021171, ni = 0.909323296631876, pi = 0.329509866222374, kj = 0.999896413565643, mj = 2.4844853951933e-09, nj = 4.94704403456447e-05, pj = 0.0101526672020547, mk = 1.28862254200612e-10, nk = 3.63259988322984e-06, pk = 0.923957489008565, nm = 0.998615227094082, pm = 0.0479071931248186, pn` = 0.999990553111872)

1 个答案:

答案 0 :(得分:1)

这是你的追求吗?

library(tidyverse)
stack(x) %>%
    separate(ind, c("x", "y"), sep = "-") %>%
    mutate(
        x = factor(x, levels = sort(union(x, y))),
        y = factor(y, levels = sort(union(x, y)))) %>%
    complete(x, y) %>%
    spread(y, values)
## A tibble: 14 x 15
#   x            a        b        c        d        e        f        g
#   <fct>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
# 1 a     NA       NA       NA       NA       NA       NA       NA
# 2 b      1.33e-1 NA       NA       NA       NA       NA       NA
# 3 c     10.00e-1  6.49e-2 NA       NA       NA       NA       NA
# 4 d      8.02e-1  2.75e-3  9.99e-1 NA       NA       NA       NA
# 5 e      1.00e+0  2.17e-1 10.00e-1  8.94e-1 NA       NA       NA
# 6 f     10.00e-1  5.64e-2 10.00e-1  9.27e-1 10.00e-1 NA       NA
# 7 g     10.00e-1  8.30e-1  9.95e-1  6.79e-1 10.00e-1 10.00e-1 NA
# 8 h      2.11e-1  1.37e-5  9.96e-1  1.00e+0  5.32e-1  4.65e-1  0.342
# 9 i      1.90e-6  2.92e-1  2.13e-6  2.48e-8  1.03e-5  4.11e-7  0.00156
#10 j      9.73e-1  2.03e-3 10.00e-1 10.00e-1  9.95e-1  9.99e-1  0.914
#11 k     10.00e-1  4.30e-1  9.98e-1  6.97e-1 10.00e-1 10.00e-1  1.000
#12 m     10.00e-1  8.94e-1 10.00e-1  9.58e-1 10.00e-1 10.00e-1  1.000
#13 n      1.77e-3  1.26e-7  1.59e-1  8.24e-1  8.28e-3  5.32e-3  0.00551
#14 p      7.39e-1  1.48e-2  9.81e-1 10.00e-1  8.01e-1  8.49e-1  0.590
## ... with 7 more variables: h <dbl>, i <dbl>, j <dbl>, k <dbl>, m <dbl>,
##   n <dbl>, p <dbl>

请注意,这仅填充矩阵的下三角部分。将值复制到上三角部分很简单。


样本数据

x <- c("b-a" =  0.132736594352255,"c-a" =  0.999921265128142,
"d-a" =  0.801948385014406,"e-a" =  1,"f-a" =  0.999999993422497,
"g-a" =  0.999994493135286,"h-a" =  0.210958663031597,
"i-a" =  1.8980767408916e-06,"j-a" =  0.973055228316854,
"k-a" =  0.999999989980955,"m-a" =  0.999999999847843,
"n-a" =  0.00176609502582648,"p-a" =  0.738916850855589,
"c-b" =  0.0648789689498736,"d-b" =  0.00274896267890112,
"e-b" =  0.217149424299426,"f-b" =  0.0563761493313133,
"g-b" =  0.830288996389741,"h-b" =  1.37129030743921e-05,
"i-b" =  0.292237803866045,"j-b" =  0.00203363512911592,
"k-b" =  0.429789314314218,"m-b" =  0.893873188901534,
"n-b" =  1.26129400479513e-07,"p-b" =  0.0148404046180679,
"d-c" =  0.999413140979136,"e-c" =  0.999983194608954,
"f-c" =  0.999999593776059,"g-c" =  0.99450256286738,
"h-c" =  0.995948206958786,"i-c" =  2.12711732894277e-06,
"j-c" =  0.999999999953707,"k-c" =  0.99849672006282,
"m-c" =  0.999945483624277,"n-c" =  0.159133209452148,
"p-c" =  0.981199963747256,"e-d" =  0.894189247372541,
"f-d" =  0.927465343408835,"g-d" =  0.679255997014174,
"h-d" =  1,"i-d" =  2.48392421076105e-08,"j-d" =  0.999592315002344,
"k-d" =  0.697136248874886,"m-d" =  0.958196387101045,
"n-d" =  0.823800464142429,"p-d" =  0.999990479586194,
"f-e" =  0.999999999789834,"g-e" =  0.999995787557262,
"h-e" =  0.53201292841468,"i-e" =  1.03081411517714e-05,
"j-e" =  0.995077423573089,"k-e" =  0.999999990349601,
"m-e" =  0.999999999728218,"n-e" =  0.00827803916192571,
"p-e" =  0.801239862126994,"g-f" =  0.999679361401266,
"h-f" =  0.464857165472924,"i-f" =  4.11179760018143e-07,
"j-f" =  0.998610646241627,"k-f" =  0.999980920365226,
"m-f" =  0.999999813932987,"n-f" =  0.00531987647573118,
"p-f" =  0.848849590006324,"h-g" =  0.341590849630092,
"i-g" =  0.00156077688059131,"j-g" =  0.914056068033521,
"k-g" =  0.999999999582086,"m-g" =  0.999999999998442,
"n-g" =  0.00550711110598001,"p-g" =  0.589694361848451,
"i-h" =  1.32359012638972e-10,"j-h" =  0.981457563281897,
"k-h" =  0.217452285021171,"m-h" =  0.909323296631876,"n-h" =  0.329509866222374,
"p-h" =  0.999896413565643,"j-i" =  2.4844853951933e-09,
"k-i" =  4.94704403456447e-05,"m-i" =  0.0101526672020547,
"n-i" =  1.28862254200612e-10,"p-i" =  3.63259988322984e-06,
"k-j" =  0.923957489008565,"m-j" =  0.998615227094082,
"n-j" =  0.0479071931248186,"p-j" =  0.983802108362677,
"m-k" =  1,"n-k" =  0.00201102356142246,"p-k" =  0.638580957139582,
"n-m" =  0.121254171582127,"p-m" =  0.857057426831322,
"p-n" =  0.999990553111872)