问题描述
我想制作这个数据框
进入这个矩阵
我试过了:
x <- read.csv("sample1.csv")
ax <- matrix(c(x[1,1],x[2,x[1,3],x[3,4],x[4,5],x[5,6],x[6,7],2],7]),10,3,byrow=TRUE)
bx <- ax[order(ax[,decreasing = TRUE),]
但它一点也不漂亮,而且如果我得到不同的样本数据,它也会有很多工作要做。 所以我想尽可能简化它,有什么建议吗?
解决方法
这可以通过使用 melt()
包中的 reshape2
函数来实现:
> a = matrix(c(1:9),nrow = 3,ncol = 3,dimnames = list(LETTERS[1:3],letters[1:3]))
> a
a b c
A 1 4 7
B 2 5 8
C 3 6 9
> library(reshape2)
> melt(a,na.rm = TRUE)
Var1 Var2 value
1 A a 1
2 B a 2
3 C a 3
4 A b 4
5 B b 5
6 C b 6
7 A c 7
8 B c 8
9 C c 9