将数据框的第一列转换为行索引。

5

我有一个数据框:

     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

我想将它转换为另一个数据框,其中 $target_id 将成为行名称。具体而言,我想对数字数据(来自样品列)执行聚类,然后能够访问其基因实体(例如:ENST00000000233)。

                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

在R中是否有可能创建这样的数据框?

3个回答

10

可以在不定义新变量的情况下实现:

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

谢谢您的建议,但不幸的是,我遇到了以下错误:Error in df[, 1] : object of type 'closure' is not subsettable - Olha Kholod
1
@OlhaKholod,“类型为'closure'的对象”表示一个函数。您正在使用“基本R”函数df的名称,它是“F”分布的密度。更改您的“data.frame”的名称。由于您提到了“data.table”包,因此出于同样的原因,您还应避免使用“dt”。 - Rui Barradas

6

这里有一种使用 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

6

首先是数据示例。

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:"))

现在开始编写代码。
result <- mydf[-1]
row.names(result) <- mydf$target_id
result
                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

简单易懂,对吧?

谢谢你的回答! 当我运行row.names(result) <- mydf$target_id时,出现了错误:Error in row.names<-.data.frame(*tmp*, value = c("ENST00000000233", : invalid 'row.names' length - Olha Kholod
我修复了这个错误。我的数据框也有data.table类,所以我只将它保存为数据框。 - Olha Kholod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接