Search code examples
sqlrcsvvsql

How do i convert row.names to a column using R


This is what I get when I

P<- unique(sqlQuery(ch, "Select * FROM TABLE"))

    rownames Maths Science English Art Chinese
1    Hazel    1      1        1     0     1
2    Haley    0      1        0     1     1
3    Shawn    1      1        1     1     1 
4    Kaze     0      0        0     0     0

To convert it likes this converting the row.names to the 1st column.

        Maths Science English Art Chinese
Hazel    1      1        1     0     1
Haley    0      1        0     1     1
Shawn    1      1        1     1     1 
Kaze     0      0        0     0     0

Solution

  • Like this:

    Assign entries from the column "rownames" to the rownames of the data frame P.

    rownames(P) <- P$rownames
    

    Delete rownames column of data frame P.

    P<-P[,-1]