Search code examples
rgenetics

Transposing a data frame


I am trying to transpose a data frame in R, but having very little luck.

The data frame contains an epigenetic data set, with 300,000+ CpG sites in the first column. The 74 additional columns are split between the control and experimental groups (cancer = 69, normal = 5).

I have converted the data frame into a matrix so I can transpose the data and convert it back to numerical values. However, every time I try to convert the data back to a data frame, it ends up as a list.

I'm attempting to do the following:

  1. Make the first column the headers.
  2. Make the name of the headers the values in the first column (except for X, which I would like to remove).

    data[1:10, 1:10]

        X Tumor.33 Normal.01 Tumor.01 Tumor.34 Tumor.35 Tumor.02 Tumor.03
    
    
    cg00000029  0.29224   0.32605  0.58762  0.32397  0.23482  0.24012  0.22941
    
    cg00000108  0.91243   0.89785  0.92337  0.90080  0.91220  0.92256  0.92709
    
    cg00000109  0.77676   0.73910  0.81545  0.73603  0.76276  0.85808  0.85142
    
    cg00000165  0.34261   0.30960  0.56392  0.32363  0.33980  0.61755  0.70855
    
    cg00000236  0.84688   0.80654  0.84423  0.80935  0.85600  0.87766  0.83509
    
    cg00000289  0.61535   0.60874  0.62496  0.66421  0.60556  0.66824  0.65243
    
    cg00000292  0.72491   0.63333  0.55031  0.69690  0.73547  0.71826  0.62223
    
    cg00000321  0.31650   0.29422  0.37737  0.28428  0.28417  0.70437  0.34829
    
    cg00000363  0.26309   0.31460  0.29135  0.26339  0.20117  0.60604  0.60548
    
    cg00000622  0.03325   0.02190  0.03293  0.04032  0.02815  0.03494  0.04126
    

Solution

  • data <- as.data.frame(t(data))

    This ensures that the numeric values are not converted to string values.