transpose is an efficient way to transpose
data frames or
transpose(l, fill=NA, ignore.empty=FALSE, keep.names=NULL, make.names=NULL)
A list, data.frame or data.table.
The name of the first column in the result containing the names of the input; e.g.
The name or number of a column in the input to use as names of the output; e.g.
The list elements (or columns of
data.table) should be all
atomic. If list elements are of unequal lengths, the value provided in
fill will be used so that the resulting list always has all elements of identical lengths. The class of input object is also preserved in the transposed result.
ignore.empty argument can be used to skip or include length-0 elements.
This is particularly useful in tasks that require splitting a character column and assigning each part to a separate column. This operation is quite common enough that a function
tstrsplit is exported.
factor columns are converted to
character type. Attributes are not preserved at the moment. This may change in the future.
ll = list(1:5, 6:8) transpose(ll)#> [] #>  1 6 #> #> [] #>  2 7 #> #> [] #>  3 8 #> #> [] #>  4 NA #> #> [] #>  5 NA #>setDT(transpose(ll, fill=0))#> V1 V2 V3 V4 V5 #> 1: 1 2 3 4 5 #> 2: 6 7 8 0 0DT = data.table(x=1:5, y=6:10) transpose(DT)#> V1 V2 V3 V4 V5 #> 1: 1 2 3 4 5 #> 2: 6 7 8 9 10