R - changing my data frame (converting columns to rows and vice versa)

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

But I want to convert my data frame to something like

How can I do it?

+7
source share
3 answers

To transpose in R, use the t () function:

t (data)

+11
source

t(data) returns a matrix .

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

+4
source

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

Best solution: use the melt function from the dplyr package. It can easily turn your data into a tabular form, after which you can "effectively" play with your data.

0
source

All Articles