8

So I have created a data frame in R with this output called 'data'

But I want to convert my data frame to something like the one below

How would I go about doing this?

Gladdys Gotherin
  • 167
  • 1
  • 3
  • 7
  • 3
    You can transpose using `t(data)`. But what is the reason for transposing? Because what you get back is matrix and not data frame. – Nishanth Apr 08 '13 at 07:37

3 Answers3

11

To transpose in R use the function t():

t(data)

Majid
  • 13,853
  • 15
  • 77
  • 113
jmanelsg
  • 154
  • 1
  • 3
4

t(data) returns a matrix.

To convert it to a data.frame from the matrix, wrap the output withas.data.frame()`.

coatless
  • 20,011
  • 13
  • 69
  • 84
Yap
  • 41
  • 3
0

t(data) returns list. If you want to shape your data, you can turn into data frame by coercion: as.data.frame(data)

Better solution: use melt function from dplyr package. It can easily turn your data into tabular form, then you can "effectively" play with your data.

strboul
  • 368
  • 2
  • 10