我有一个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将是一个行名。具体来说,我想对数值数据(来自样本列)进行聚类,然后能够访问他们的基因实体(例如: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?
中创建此类data.frame谢谢!
答案 0 :(得分:4)
首先是您的数据示例。
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
简单,不是吗?
答案 1 :(得分:3)
可以在不定义新变量的情况下实现:
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
答案 2 :(得分:2)
以下是使用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