Search code examples
rdataframer-rownames

Convert first column in data.frame to row index


I have a 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

I would like to convert it to another data.frame, where $target_id will be a row name. Specifically, I want to perform clustering on numerical data (from sample columns) and then be able to access their gene entities (for example: 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

Is it possible to create such data.frame in R?


Solution

  • First your data example.

    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:"))
    

    Now the code.

    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
    

    Simple, no?