Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将data.frame中的第一列转换为行索引_R_Dataframe_Date Formatting - Fatal编程技术网

将data.frame中的第一列转换为行索引

将data.frame中的第一列转换为行索引,r,dataframe,date-formatting,R,Dataframe,Date Formatting,我有一个data.frame: target_id sample1 sample10 sample100 sample101 sample102 sample103 1: ENST00000000233 9 0 3499.51 0 0 0 2: ENST00000000412 0 0 0.00 0 0 0 3: ENST000

我有一个data.frame:

     target_id sample1 sample10 sample100 sample101 sample102 sample103
1: ENST00000000233       9        0   3499.51         0         0         0
2: ENST00000000412       0        0      0.00         0         0         0
3: ENST00000000442       0        0      0.00         0         0         0
4: ENST00000001008       0        0      0.00         0         0         0
5: ENST00000001146       0        0      0.00         0         0         0
6: ENST00000002125       0        0      0.00         0         0         0
我想将其转换为另一个data.frame,其中$target_id将是一个行名称。具体来说,我希望对数值数据(来自样本列)执行聚类,然后能够访问它们的基因实体(例如:ENST0000000233)


可以在R中创建这样的data.frame吗?

首先是数据示例

mydf <-
structure(list(target_id = c("ENST00000000233", "ENST00000000412", 
"ENST00000000442", "ENST00000001008", "ENST00000001146", "ENST00000002125"
), sample1 = c(9L, 0L, 0L, 0L, 0L, 0L), sample10 = c(0L, 0L, 
0L, 0L, 0L, 0L), sample100 = c(3499.51, 0, 0, 0, 0, 0), sample101 = c(0L, 
0L, 0L, 0L, 0L, 0L), sample102 = c(0L, 0L, 0L, 0L, 0L, 0L), sample103 = c(0L, 
0L, 0L, 0L, 0L, 0L)), .Names = c("target_id", "sample1", "sample10", 
"sample100", "sample101", "sample102", "sample103"), class = "data.frame", row.names = c("1:", 
"2:", "3:", "4:", "5:", "6:"))

mydf无需定义新变量即可实现:

df1 <- data.frame(df1[,-1], row.names = df1[,1])


#                 sample1 sample10 sample100 sample101 sample102 sample103 
# ENST00000000233       9        0   3499.51         0         0         0 
# ENST00000000412       0        0      0.00         0         0         0 
# ENST00000000442       0        0      0.00         0         0         0 
# ENST00000001008       0        0      0.00         0         0         0 
# ENST00000001146       0        0      0.00         0         0         0 
# ENST00000002125       0        0      0.00         0         0         0

df1这里有一个使用
tidyverse

library(tidyverse)
df1 %>%
     remove_rownames() %>%
     column_to_rownames(var = 'target_id')
#                sample1 sample10 sample100 sample101 sample102 sample103
#ENST00000000233       9        0   3499.51         0         0         0
#ENST00000000412       0        0      0.00         0         0         0
#ENST00000000442       0        0      0.00         0         0         0
#ENST00000001008       0        0      0.00         0         0         0
#ENST00000001146       0        0      0.00         0         0         0
#ENST00000002125       0        0      0.00         0         0         0

感谢您的建议,但不幸的是,我得到了以下错误:
df[,1]:类型为“closure”的对象不可子集
@OlhaKholod,
类型为“closure”的对象
表示函数。您使用的是
base R
函数的名称,
df
,即
F
分布的密度。更改数据帧的名称。既然您提到了package
data.table
,出于同样的原因,您也应该避免使用
dt
。谢谢您的回答!当我运行
row.names(result)时,我修复了这个错误。我的data.frame也有data.table类,所以我只将其保存为data.frame
df1 <- data.frame(df1[,-1], row.names = df1[,1])


#                 sample1 sample10 sample100 sample101 sample102 sample103 
# ENST00000000233       9        0   3499.51         0         0         0 
# ENST00000000412       0        0      0.00         0         0         0 
# ENST00000000442       0        0      0.00         0         0         0 
# ENST00000001008       0        0      0.00         0         0         0 
# ENST00000001146       0        0      0.00         0         0         0 
# ENST00000002125       0        0      0.00         0         0         0
library(tidyverse)
df1 %>%
     remove_rownames() %>%
     column_to_rownames(var = 'target_id')
#                sample1 sample10 sample100 sample101 sample102 sample103
#ENST00000000233       9        0   3499.51         0         0         0
#ENST00000000412       0        0      0.00         0         0         0
#ENST00000000442       0        0      0.00         0         0         0
#ENST00000001008       0        0      0.00         0         0         0
#ENST00000001146       0        0      0.00         0         0         0
#ENST00000002125       0        0      0.00         0         0         0