I have the following R data.table
library(data.table)
mtcars = as.data.table(mtcars)
dt = colSums(mtcars)
> dt
mpg cyl disp hp drat wt qsec vs
642.900 198.000 7383.100 4694.000 115.090 102.952 571.160 14.000
am gear carb
13.000 118.000 90.000
I would like to reshape the data.table dt
as follows:
> transpose
column1 column2
mpg 642.900
cyl 198.000
disp 7373.100
hp 4694.000
drat 115.090
wt 102.952
qsec 571.160
vs 14.000
am 13.000
gear 118.000
carb 90.000
The function t()
doesn't appear to work as expected.
transpose = t(dt)
I suspect there's a quick way to do this with melt()
and dcast()
, but I'm not sure how one defines each column, i.e. column1
and column2