作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 R 中的 data.frame 中有一个带有距离的二维表(从 csv 导入):
CP000036 CP001063 CP001368
CP000036 0 a b
CP001063 a 0 c
CP001368 b c 0
Genome1 Genome2 Dist
CP000036 CP001063 a
CP000036 CP001368 b
CP001063 CP001368 c
Genome1 Genome2 Dist
CP000036 CP000036 0
CP000036 CP001063 a
CP000036 CP001368 b
CP001063 CP000036 a
CP001063 CP001063 0
CP001063 CP001368 c
CP001368 CP000036 b
CP001368 CP001063 c
CP001368 CP001368 0
最佳答案
所以这是使用 melt
的一种解决方案来自包裹reshape2
:
dm <-
data.frame( CP000036 = c( "0", "a", "b" ),
CP001063 = c( "a", "0", "c" ),
CP001368 = c( "b", "c", "0" ),
stringsAsFactors = FALSE,
row.names = c( "CP000036", "CP001063", "CP001368" ) )
# assuming the distance follows a metric we avoid everything below and on the diagonal
dm[ lower.tri( dm, diag = TRUE ) ] <- NA
dm$Genome1 <- rownames( dm )
# finally melt and avoid the entries below the diagonal with na.rm = TRUE
library(reshape2)
dm.molten <- melt( dm, na.rm= TRUE, id.vars="Genome1",
value.name="Dist", variable.name="Genome2" )
print( dm.molten )
Genome1 Genome2 Dist
4 CP000036 CP001063 a
7 CP000036 CP001368 b
8 CP001063 CP001368 c
关于r - 如何将 “flatten” 或 “collapse” 2D 数据帧转换为 R 中的 1D 数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16220732/
我是一名优秀的程序员,十分优秀!