Answer by Japa19 for best way to transpose data.table
df <- as.data.frame(t(mydata)) is what I tried and df is a data.frame and the column names on mydata are now row names on df
View ArticleAnswer by PeterAU for best way to transpose data.table
The tdt function which I provide below should be faster tdt <- function(DT, transpose.col, ...) { # The transpose function is efficient, but lacks the keeping of row and colnames new.row.names <-...
View ArticleAnswer by Amy M for best way to transpose data.table
Here's a solution that uses a wrapper to tidy up the output of the data.table transpose function. With really large data sets this seems to be more efficient than the dcast/melt approach (I tested it...
View ArticleAnswer by abalter for best way to transpose data.table
The current docs show a builtin transpose method. I don't know when it was added, but apparently it was needed!
View ArticleAnswer by shadow for best way to transpose data.table
Here's an alternative solution that only uses data.table and that is closer to the original idea of using t to transpose. mydata[, data.table(t(.SD), keep.rownames=TRUE), .SDcols=-"col0"] ## rn V1 V2...
View ArticleAnswer by A5C1D2H2I1M1N2O1R2T1 for best way to transpose data.table
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
View Articlebest way to transpose data.table
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 <-...
View Article