我想reshape
将数字矩阵放入long
数据框,并包含其他条件。
矩阵的行== genes == clusters
矩阵的列== samples == time
# matrix
set.seed(123)
mat <- matrix(rnorm(50), ncol=10)
# factors
genes <- letters[1:nrow(mat)]
clusters <- c(1,1,1,2,2)
samples <- rep(c("A","B"),times=5)
time <- rep(c(0,2,4,5,7),each=2)
我尝试了以下内容:
library(reshape2)
df <- melt(mat, varnames=c("genes","samples"))
df$clusters <- clusters[df$genes] # numeric
df$time <- time[df$samples] # numeric
df$genes <- genes[df$genes] # character
df$samples <- samples[df$samples] # character
> head(df)
genes samples value clusters time
1 a A 0.25331851 1 0
2 b A -0.02854676 1 0
3 c A -0.04287046 1 0
4 d A 1.36860228 2 0
5 e A -0.22577099 2 0
6 a B 1.51647060 1 0
是否有更有效(和更快)的方法来重塑这一点而不强迫字符串成为因素?如果可能的话,我更愿意使用基本功能,但也欢迎任何其他例子。
PS:我理解这个问题可能已在整个网站中进行了广泛的迭代,我已经浏览过,遗憾的是由于对这些功能如何运作的理解有限而未能采用潜在的解决方案,因此我们将非常感谢任何帮助。 / p>