我有一个包含三列的表,其中包含我的第一个变量,第二个变量及其估计的相关性(估计来自brmsfit
模型)。
作为示例,让我们考虑下表:
tibble::tribble(
~a, ~b, ~est,
"fro", "fcr", "0.061 [-0.276, 0.38]",
"fro", "nek", "0.115 [-0.218, 0.421]",
"nek", "fcr", "0.154 [-0.169, 0.476]",
"ooi", "fcr", "-0.022 [-0.352, 0.305]",
"ooi", "fro", "0.006 [-0.326, 0.323]",
"ooi", "nek", "0.173 [-0.151, 0.486]",
"ooi", "zyg", "0.039 [-0.29, 0.362]",
"zyg", "fcr", "-0.02 [-0.347, 0.317]",
"zyg", "fro", "0.092 [-0.245, 0.406]",
"zyg", "nek", "0.315 [0.011, 0.613]"
)
我想以相关矩阵的形式重塑它。我知道有些软件包可以执行精确的逆运算(例如corrr::stretch()
函数),但是我不知道将宽数据帧重塑为相关矩阵的简单解决方案。
有什么想法吗?
答案 0 :(得分:1)
这是您要寻找的吗?
library(tidyverse)
library(magrittr)
df %$%
c(a, b) %>%
crossing(a = ., b = .) %>%
left_join(df) %>%
mutate(est = if_else(a == b, '1', est)) %>%
group_by(a) %>%
mutate(rn = row_number() %>% rev()) %>%
nest() %>%
mutate(gn = row_number() %>% rev()) %>%
unnest() %>%
mutate(
cond = gn > rn,
a_temp = a,
a = if_else(cond, b, a),
b = if_else(cond, a_temp, b)
) %>%
select(a, b, est) %>%
filter(!is.na(est)) %>%
spread(a, est)
输出:
b fcr fro nek ooi zyg
<chr> <chr> <chr> <chr> <chr> <chr>
1 fcr 1 0.061 [-0.276, 0.38] 0.154 [-0.169, 0.476] -0.022 [-0.352, 0.305] -0.02 [-0.347, 0.317]
2 fro NA 1 0.115 [-0.218, 0.421] 0.006 [-0.326, 0.323] 0.092 [-0.245, 0.406]
3 nek NA NA 1 0.173 [-0.151, 0.486] 0.315 [0.011, 0.613]
4 ooi NA NA NA 1 0.039 [-0.29, 0.362]
5 zyg NA NA NA NA 1
答案 1 :(得分:0)
使用Wheel::SetValues
的解决方案。 tidyverse
是最终输出,其中第一列为变量名称。其他列的列标题也是变量名。
dat2