我有所有成对的相关性,并想构建 var-covariance 矩阵,以便对该矩阵进行一些标准分析。这是协方差的示例数据,前两列是“ids”,而第三列显示“ids”之间的协方差。
data<-data.frame("id1" = c("a","b","c","a","a","b"),
"id2" = c("a","b","c","b","c","c"),
"cov"=c(1,1,1,0.1,0.3,0.4))
我有所有成对的相关性,并想构建 var-covariance 矩阵,以便对该矩阵进行一些标准分析。这是协方差的示例数据,前两列是“ids”,而第三列显示“ids”之间的协方差。
data<-data.frame("id1" = c("a","b","c","a","a","b"),
"id2" = c("a","b","c","b","c","c"),
"cov"=c(1,1,1,0.1,0.3,0.4))
Base-R 解决方案:
nm <- unique(data$id1) ## row/col names
v <- matrix(NA,length(nm),length(nm),dimnames=list(nm,nm)) ## set up template
v[cbind(data$id1,data$id2)] <- data$cov ## fill in upper triangle
v[is.na(v)] <- t(v)[is.na(v)] ## symmetrize
一个简单的 dplyr 解决方案是在 的帮助下使 data.frame 更宽pivot_wider
,即
data<-data.frame("id1" = c("a","b","c","a","a","b"),
"id2" = c("a","b","c","b","c","c"),
"cov"=c(1,1,1,0.1,0.3,0.4))
tidyr::pivot_wider(data,
id_cols = c(cov, id2),
names_from = id1,
values_from = cov)
产生输出
id2 a b c
<fct> <dbl> <dbl> <dbl>
1 a 1 NA NA
2 b 0.1 1 NA
3 c 0.3 0.4 1
由于协方差矩阵是对称的,所以它完成了。