merryberry
merryberry

Reputation: 101

Convert data frame row into new data frame column R

I have a data frame, cul7_expr, with one row and 22 columns. I want to create a new data frame containing one column with all the numerical values from cul7_expr and another column with all the column names (TCGA.BC...). The new data frame would have 2 columns and 22 rows. However, when I try to take the row of cul7_expr, a warning pops up and the data frame is empty.

Sample data (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"))

Upvotes: 0

Views: 84

Answers (2)

Elin
Elin

Reputation: 6755

If I understand what you want correctly you need to get the transpose and the column names and then make a data frame from them.

# 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)

Upvotes: 0

www
www

Reputation: 4224

Try the melt function. To help in the future, this is called changing data from wide to long format.

require(data.table)
melt(df,measure.vars=1:22)

Output:

            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

Upvotes: 1

Related Questions