将数据帧转换为矩阵,其中第1列的df为矩阵的rownames [英] convert data frame to a matrix with column 1 of df as rownames of matrix
问题描述
我想将数据帧转换为矩阵,并使用现有数据帧的第1列作为新矩阵,并将数据框的列名称保持为矩阵的列名。
我也想自动化此过程以用于类似数据帧但不同的维度。那么,如果这个解决方案需要知道行数和/或列的数量,那么如何在不必查看显示器的情况下将这些信息传入代码呢?
我看过data.matrix和reshape2,但似乎无法弄清楚如何做我想要的。
使用您的样本数据
X< c(AT1G01040,AT1G01270,AT1G01471,AT1G01680),log2.fold_change._Mer7_2.1_Mer7_2.2 = c(0,0,0,0),log2.fold_change._Mer7_1.2_W29_S226A_1 = c 0,0,-1.14,0),log2.fold_change._Mer7_1.2_W29_1 = c(0,0,0,0)),.Names = c(gene,log2.fold_change._Mer7_2.1_Mer7_2.2 ,log2.fold_change._Mer7_1.2_W29_S226A_1,log2.fold_change._Mer7_1.2_W29_1),row.names = c(NA,4L),class =data.frame)
您可以编写一个简单的帮助函数来创建一个矩阵并设置正确的名称
matrix.please< -function(x){
m< -as.matrix(x [, - 1])$ b $ b rownames(m) [,1]
m
}
你会使用它像
M< - ma trix.please(X)
str(M)
#num [1:4,1:3] 0 0 0 0 0 0 -1.14 0 0 0 ...
# - attr (*,dimnames)= 2
#..的列表$:chr [1:4]AT1G01040AT1G01270AT1G01471AT1G01680
#.. $:chr [1 :3]log2.fold_change._Mer7_2.1_Mer7_2.2
#log2.fold_change._Mer7_1.2_W29_S226A_1log2.fold_change._Mer7_1.2_W29_1
所以我们有一个4x3矩阵,具有正确的行和列名。
I have a data frame with dimensions 3695 X 20. The first column contains alphanumeric identifiers, the other 19 columns are all numeric. So, rownames(df) provides the numbers 1-3695, and colnames(df) gives the names of the columns. df[,1] provides the alphanumeric identifiers.
I would like to convert the data frame to a matrix and use column 1 of the existing data frame to be the rownames of the new matrix and maintain the column names of the data frame as the column names of the matrix.
I would also like to automate this process for use with data frames of similar but different dimensions. So, if the solution to this requires knowing the number of rows and/or columns, how can I get this information into the code without me having to look at the monitor ?
I have looked at data.matrix and reshape2 but can not seem to figure out how to do what I want.
With your sample data
X<-structure(list(gene = c("AT1G01040", "AT1G01270", "AT1G01471", "AT1G01680"), log2.fold_change._Mer7_2.1_Mer7_2.2 = c(0, 0, 0, 0), log2.fold_change._Mer7_1.2_W29_S226A_1 = c(0, 0, -1.14, 0 ), log2.fold_change._Mer7_1.2_W29_1 = c(0, 0, 0, 0)), .Names = c("gene", "log2.fold_change._Mer7_2.1_Mer7_2.2", "log2.fold_change._Mer7_1.2_W29_S226A_1", "log2.fold_change._Mer7_1.2_W29_1"), row.names = c(NA, 4L), class = "data.frame")
You can write a simple helper function to create a matrix and set the right names
matrix.please<-function(x) {
m<-as.matrix(x[,-1])
rownames(m)<-x[,1]
m
}
and you would use it like
M <- matrix.please(X)
str(M)
# num [1:4, 1:3] 0 0 0 0 0 0 -1.14 0 0 0 ...
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:4] "AT1G01040" "AT1G01270" "AT1G01471" "AT1G01680"
# ..$ : chr [1:3] "log2.fold_change._Mer7_2.1_Mer7_2.2"
# "log2.fold_change._Mer7_1.2_W29_S226A_1" "log2.fold_change._Mer7_1.2_W29_1"
So we have a 4x3 matrix with the correct row and col names.
这篇关于将数据帧转换为矩阵,其中第1列的df为矩阵的rownames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!