Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to transpose data.table

Tags:

r

data.table

[UPDATE: there is now a native transpose() function in data.table package]

I often need to transpose a data.table, every time it takes several lines of code and I am wondering if there's any better solution than mine.

if we take sample table

library(data.table) mydata <- data.table(col0=c("row1","row2","row3"),                      col1=c(11,21,31),                      col2=c(12,22,32),                      col3=c(13,23,33))  mydata # col0 col1 col2 col3 # row1   11   12   13 # row2   21   22   23 # row3   31   32   33 

and just transpose it with t(), it will be transposed to the matrix with conversion to character type, while applying data.table to such matrix will lose row.names:

t(mydata) # [,1]   [,2]   [,3]   # col0 "row1" "row2" "row3" # col1 "11"   "21"   "31"   # col2 "12"   "22"   "32"   # col3 "13"   "23"   "33"    data.table(t(mydata)) #   V1   V2   V3 # row1 row2 row3 #   11   21   31 #   12   22   32 #   13   23   33 

so I had to write a function for this:

tdt <- function(inpdt){   transposed <- t(inpdt[,-1,with=F]);   colnames(transposed) <- inpdt[[1]];   transposed <- data.table(transposed, keep.rownames=T);   setnames(transposed, 1, names(inpdt)[1]);   return(transposed); }   tdt(mydata) # col0 row1 row2 row3 # col1   11   21   31 # col2   12   22   32 # col3   13   23   33 

is there anything I could optimize here or do it in "nicer" way?

like image 433
Vasily A Avatar asked Feb 22 '15 02:02

Vasily A


People also ask

How do you transpose data in a table?

We put the cursor anywhere in the Excel worksheet and right-click the menu. We click on the command Paste Special (CTRL+ALT+V). In the window that appears we put the tick near the TRANSPOSE. The rest we left as is and click OK.

How do you quickly transpose a table in Excel?

on the Home tab, or press CONTROL+C. Note: Make sure you copy the data to do this. Using the Cut command or CONTROL+X won't work. Select the first cell where you want to paste the data, and on the Home tab, click the arrow next to Paste, and then click Transpose.

How do you dynamically transpose data in Excel?

Select the original data and copy it. Select the top left cell of the destination range. On the Ribbon's Home tab, click the Paste drop down arrow. Click Transpose.


Video Answer


2 Answers

Why not just melt and dcast the data.table?

require(data.table)  dcast(melt(mydata, id.vars = "col0"), variable ~ col0) #    variable row1 row2 row3 # 1:     col1   11   21   31 # 2:     col2   12   22   32 # 3:     col3   13   23   33 
like image 85
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 05 '22 21:10

A5C1D2H2I1M1N2O1R2T1


The current docs show a builtin transpose method.

Specifically, you can do:

transpose(mydata, keep.names = "col", make.names = "col0") ##     col row1 row2 row3 ## 1: col1   11   21   31 ## 2: col2   12   22   32 ## 3: col3   13   23   33 
like image 44
abalter Avatar answered Oct 05 '22 20:10

abalter