Transpose a data frame

I need to transfer a large data frame, and so I used:

df.aree <- t(df.aree) df.aree <- as.data.frame(df.aree) 

This is what I get:

 df.aree[c(1:5),c(1:5)] 10428 10760 12148 11865 name M231T3 M961T5 M960T6 M231T19 GS04.A 5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04 GS16.A 5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04 GS20.A 5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04 GS40.A 3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04 

My problem is the names of the new columns (10428, 10760, 12148, 11865) that I need to eliminate because I need to use the first row as the column names.

I tried with the col.names() function, but I did not get what I needed.

Do you have any suggestions?

EDIT

Thank you for your offer!!! Using it, I get:

 df.aree[c(1:5),c(1:5)] M231T3 M961T5 M960T6 M231T19 GS04.A 5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04 GS16.A 5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04 GS20.A 5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04 GS40.A 3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04 GS44.A 1.225938e+04 2.681887e+03 1.154924e+04 4.202394e+04 

Now I need to convert the row names (GS ..) to the factor column ....

+72
r dataframe
Jul 21 '11 at 15:48
source share
3 answers

Itโ€™s better not to rearrange the data.frame while the name column is in it - all numeric values โ€‹โ€‹then turn into strings!

Here's a solution that stores numbers as numbers:

 # first remember the names n <- df.aree$name # transpose all but the first column (name) df.aree <- as.data.frame(t(df.aree[,-1])) colnames(df.aree) <- n df.aree$myfactor <- factor(row.names(df.aree)) str(df.aree) # Check the column types 
+71
Jul 21 '11 at 16:48
source share
 df.aree <- as.data.frame(t(df.aree)) colnames(df.aree) <- df.aree[1, ] df.aree <- df.aree[-1, ] df.aree$myfactor <- factor(row.names(df.aree)) 
+42
Jul 21 '11 at 16:16
source share

You can use the transpose function in the data.table library. A simple and quick solution that saves numeric values โ€‹โ€‹as numeric .

 library(data.table) # get data data("mtcars") # transpose t_mtcars <- transpose(mtcars) # get row and colnames in order colnames(t_mtcars) <- rownames(mtcars) rownames(t_mtcars) <- colnames(mtcars) 
+11
May 21 '17 at 9:31 a.m.
source share



All Articles