我有一个数据框cul7_expr,有一行和22列。我想创建一个新的数据框,其中包含一个列,其中包含来自cul7_expr的所有数值和另一个包含所有列名称的列(TCGA.BC ...)。新数据框将有2列和22行。但是,当我尝试使用cul7_expr行时,会弹出一个警告并且数据框为空。
示例数据(cul7_expr):
df <- structure(list(TCGA.BC.A10Q.11A=819.3685,TCGA.BC.A10Q.01A=2757.486,
TCGA.DD.A1EB.11A=698.5818,TCGA.DD.A1EB.01A=1625.094,TCGA.DD.A1EG.11A=409.9332,
TCGA.DD.A1EG.01A=2221.012,TCGA.DD.A1EH.11A=391.0916,TCGA.DD.A1EH.01A=2122.782,
TCGA.DD.A1EI.11A=717.2073,TCGA.DD.A1EI.01A=768.7468,TCGA.DD.A3A6.11A=464.6395,
TCGA.DD.A3A6.01A=1175.928,TCGA.DD.A3A8.11A=934.9738,TCGA.DD.A3A8.01A=931.8955,
TCGA.ES.A2HT.11A=599.736,TCGA.ES.A2HT.01A=894.8324,TCGA.FV.A23B.11A=970.1805,
TCGA.FV.A23B.01A=3018.075,TCGA.FV.A3I0.11A=337.222,TCGA.FV.A3I0.01A=3895.477,
TCGA.FV.A3R2.11A=912.8499,TCGA.FV.A3R2.01A=2226.921),
.Names=c("TCGA.BC.A10Q.11A","TCGA.BC.A10Q.01A","TCGA.DD.A1EB.11A",
"TCGA.DD.A1EB.01A","TCGA.DD.A1EG.11A","TCGA.DD.A1EG.01A","TCGA.DD.A1EH.11A",
"TCGA.DD.A1EH.01A","TCGA.DD.A1EI.11A","TCGA.DD.A1EI.01A","TCGA.DD.A3A6.11A",
"TCGA.DD.A3A6.01A","TCGA.DD.A3A8.11A","TCGA.DD.A3A8.01A","TCGA.ES.A2HT.11A",
"TCGA.ES.A2HT.01A","TCGA.FV.A23B.11A","TCGA.FV.A23B.01A","TCGA.FV.A3I0.11A",
"TCGA.FV.A3I0.01A","TCGA.FV.A3R2.11A","TCGA.FV.A3R2.01A"),row.names = c(NA, -1L),
class = c("data.table","data.frame"))
答案 0 :(得分:1)
尝试融化功能。为了将来的帮助,这称为将数据从宽格式更改为长格式。
require(data.table)
melt(df,measure.vars=1:22)
输出:
variable value
1: TCGA.BC.A10Q.11A 819.3685
2: TCGA.BC.A10Q.01A 2757.4860
3: TCGA.DD.A1EB.11A 698.5818
4: TCGA.DD.A1EB.01A 1625.0940
5: TCGA.DD.A1EG.11A 409.9332
6: TCGA.DD.A1EG.01A 2221.0120
7: TCGA.DD.A1EH.11A 391.0916
8: TCGA.DD.A1EH.01A 2122.7820
9: TCGA.DD.A1EI.11A 717.2073
10: TCGA.DD.A1EI.01A 768.7468
11: TCGA.DD.A3A6.11A 464.6395
12: TCGA.DD.A3A6.01A 1175.9280
13: TCGA.DD.A3A8.11A 934.9738
14: TCGA.DD.A3A8.01A 931.8955
15: TCGA.ES.A2HT.11A 599.7360
16: TCGA.ES.A2HT.01A 894.8324
17: TCGA.FV.A23B.11A 970.1805
18: TCGA.FV.A23B.01A 3018.0750
19: TCGA.FV.A3I0.11A 337.2220
20: TCGA.FV.A3I0.01A 3895.4770
21: TCGA.FV.A3R2.11A 912.8499
22: TCGA.FV.A3R2.01A 2226.9210
答案 1 :(得分:0)
如果我理解你想要的正确,你需要获得转置和列名称,然后从中创建数据框。
# set up data
x <- data.frame(1, 2, 3, 4)
names <- c("A", "B", "C", "D")
colnames(x) <- names
#convert
names <- colnames(x)
data.frame(t(x), names)