SNT
SNT

Reputation: 1423

Dataframe to matrix during transpose in R

I have a dataframe which i am trying to transpose .But when I do that it converts it into a matrix. How do I transpose a dataframe without changing it to a matrix.

Actual Dataframe:

  Month_considered Exclusive Loyal Interacting loyal left Non loyal Switcher Total
1            Apr-17              11               632  479       910      229  1782
2            Aug-17              35               613  830      1014      241  1903
3            Dec-17              35               683  757      1205      122  2045
4            Feb-18               0                 0    0         0        0     0
5            Jan-18              36               708  684      1085      297  2126
6            Jul-17              30               560  631      1072      231  1893

df%>%t()

                 [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]     [,10]    [,11]   
Month_considered  "Apr-17" "Aug-17" "Dec-17" "Feb-18" "Jan-18" "Jul-17" "Jun-17" "May-17" "Nov-17" "Oct-17" "Sep-17"
Exclusive Loyal   "11"     "35"     "35"     " 0"     "36"     "30"     "29"     "34"     "43"     "42"     "32"    
Interacting loyal "632"    "613"    "683"    "  0"    "708"    "560"    "493"    "640"    "577"    "572"    "605"   
left              "479"    "830"    "757"    "  0"    "684"    "631"    "609"    "503"    "667"    "719"    "645"  

Upvotes: 0

Views: 92

Answers (1)

a--on-
a--on-

Reputation: 153

You can use transpose from the data.table package.

data.table::transpose(mydf)

this should work.

Upvotes: 1

Related Questions