在R中转置期间将数据帧转换为矩阵

时间:2018-08-07 15:05:55

标签: r dataframe transpose

我有一个要转置的数据框。但是当我这样做时,它会将其转换成矩阵。如何转置数据框而不将其更改为矩阵。

实际数据框:

  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"  

1 个答案:

答案 0 :(得分:1)

您可以使用transpose软件包中的data.table

data.table::transpose(mydf)

这应该有效。