Fast reshape/tranpose of data
fast.reshape(
data,
varying,
id,
num,
sep = "",
keep,
idname = "id",
numname = "num",
factor = FALSE,
idcombine = TRUE,
labelnum = FALSE,
labels,
regex = mets.options()$regex,
dropid = FALSE,
...
)
data.frame or matrix
Vector of prefix-names of the time varying variables. Optional for Long->Wide reshaping.
id-variable. If omitted then reshape Wide->Long.
Optional number/time variable
String seperating prefix-name with number/time
Vector of column names to keep
Name of id-variable (Wide->Long)
Name of number-variable (Wide->Long)
If true all factors are kept (otherwise treated as character)
If TRUE and id
is vector of several variables, the unique id is combined from all the variables.
Otherwise the first variable is only used as identifier.
If TRUE varying variables in wide format (going from long->wide) are labeled 1,2,3,... otherwise use 'num' variable. In long-format (going from wide->long) varying variables matching 'varying' prefix are only selected if their postfix is a number.
Optional labels for the number variable
Use regular expressions
Drop id in long format (default FALSE)
Optional additional arguments
m <- lava::lvm(c(y1,y2,y3,y4)~x)
d <- lava::sim(m,5)
d
#> y1 y2 y3 y4 x
#> 1 -0.5374072 0.1922456 0.7401224 -1.63051519 -0.5426985
#> 2 1.7302578 -0.3315447 0.8675281 -0.08805435 1.0227475
#> 3 -2.8325490 -2.4689011 -1.9752101 -2.23098113 -2.0349834
#> 4 -2.4393909 -0.4825135 -1.9842807 -0.97943066 -0.9102260
#> 5 -2.0478220 -1.2542359 -0.6441103 1.90859645 -1.3167978
fast.reshape(d,"y")
#> x y id num
#> 1 -0.5426985 -0.53740716 1 1
#> 2 -0.5426985 0.19224557 1 2
#> 3 -0.5426985 0.74012237 1 3
#> 4 -0.5426985 -1.63051519 1 4
#> 5 1.0227475 1.73025778 2 1
#> 6 1.0227475 -0.33154470 2 2
#> 7 1.0227475 0.86752809 2 3
#> 8 1.0227475 -0.08805435 2 4
#> 9 -2.0349834 -2.83254896 3 1
#> 10 -2.0349834 -2.46890109 3 2
#> 11 -2.0349834 -1.97521005 3 3
#> 12 -2.0349834 -2.23098113 3 4
#> 13 -0.9102260 -2.43939091 4 1
#> 14 -0.9102260 -0.48251354 4 2
#> 15 -0.9102260 -1.98428074 4 3
#> 16 -0.9102260 -0.97943066 4 4
#> 17 -1.3167978 -2.04782202 5 1
#> 18 -1.3167978 -1.25423593 5 2
#> 19 -1.3167978 -0.64411031 5 3
#> 20 -1.3167978 1.90859645 5 4
fast.reshape(fast.reshape(d,"y"),id="id")
#> x1 y1 id num1 x2 y2 num2 x3
#> 1 -0.5426985 -0.5374072 1 1 -0.5426985 0.1922456 2 -0.5426985
#> 2 1.0227475 1.7302578 2 1 1.0227475 -0.3315447 2 1.0227475
#> 3 -2.0349834 -2.8325490 3 1 -2.0349834 -2.4689011 2 -2.0349834
#> 4 -0.9102260 -2.4393909 4 1 -0.9102260 -0.4825135 2 -0.9102260
#> 5 -1.3167978 -2.0478220 5 1 -1.3167978 -1.2542359 2 -1.3167978
#> y3 num3 x4 y4 num4
#> 1 0.7401224 3 -0.5426985 -1.63051519 4
#> 2 0.8675281 3 1.0227475 -0.08805435 4
#> 3 -1.9752101 3 -2.0349834 -2.23098113 4
#> 4 -1.9842807 3 -0.9102260 -0.97943066 4
#> 5 -0.6441103 3 -1.3167978 1.90859645 4
##### From wide-format
(dd <- fast.reshape(d,"y"))
#> x y id num
#> 1 -0.5426985 -0.53740716 1 1
#> 2 -0.5426985 0.19224557 1 2
#> 3 -0.5426985 0.74012237 1 3
#> 4 -0.5426985 -1.63051519 1 4
#> 5 1.0227475 1.73025778 2 1
#> 6 1.0227475 -0.33154470 2 2
#> 7 1.0227475 0.86752809 2 3
#> 8 1.0227475 -0.08805435 2 4
#> 9 -2.0349834 -2.83254896 3 1
#> 10 -2.0349834 -2.46890109 3 2
#> 11 -2.0349834 -1.97521005 3 3
#> 12 -2.0349834 -2.23098113 3 4
#> 13 -0.9102260 -2.43939091 4 1
#> 14 -0.9102260 -0.48251354 4 2
#> 15 -0.9102260 -1.98428074 4 3
#> 16 -0.9102260 -0.97943066 4 4
#> 17 -1.3167978 -2.04782202 5 1
#> 18 -1.3167978 -1.25423593 5 2
#> 19 -1.3167978 -0.64411031 5 3
#> 20 -1.3167978 1.90859645 5 4
## Same with explicit setting new id and number variable/column names
## and seperator "" (default) and dropping x
fast.reshape(d,"y",idname="a",timevar="b",sep="",keep=c())
#> y a num
#> 1 -0.53740716 1 1
#> 2 0.19224557 1 2
#> 3 0.74012237 1 3
#> 4 -1.63051519 1 4
#> 5 1.73025778 2 1
#> 6 -0.33154470 2 2
#> 7 0.86752809 2 3
#> 8 -0.08805435 2 4
#> 9 -2.83254896 3 1
#> 10 -2.46890109 3 2
#> 11 -1.97521005 3 3
#> 12 -2.23098113 3 4
#> 13 -2.43939091 4 1
#> 14 -0.48251354 4 2
#> 15 -1.98428074 4 3
#> 16 -0.97943066 4 4
#> 17 -2.04782202 5 1
#> 18 -1.25423593 5 2
#> 19 -0.64411031 5 3
#> 20 1.90859645 5 4
## Same with 'reshape' list-syntax
fast.reshape(d,list(c("y1","y2","y3","y4")),labelnum=TRUE)
#> x y1 id num
#> 1 -0.5426985 -0.53740716 1 1
#> 2 -0.5426985 0.19224557 1 2
#> 3 -0.5426985 0.74012237 1 3
#> 4 -0.5426985 -1.63051519 1 4
#> 5 1.0227475 1.73025778 2 1
#> 6 1.0227475 -0.33154470 2 2
#> 7 1.0227475 0.86752809 2 3
#> 8 1.0227475 -0.08805435 2 4
#> 9 -2.0349834 -2.83254896 3 1
#> 10 -2.0349834 -2.46890109 3 2
#> 11 -2.0349834 -1.97521005 3 3
#> 12 -2.0349834 -2.23098113 3 4
#> 13 -0.9102260 -2.43939091 4 1
#> 14 -0.9102260 -0.48251354 4 2
#> 15 -0.9102260 -1.98428074 4 3
#> 16 -0.9102260 -0.97943066 4 4
#> 17 -1.3167978 -2.04782202 5 1
#> 18 -1.3167978 -1.25423593 5 2
#> 19 -1.3167978 -0.64411031 5 3
#> 20 -1.3167978 1.90859645 5 4
##### From long-format
fast.reshape(dd,id="id")
#> x1 y1 id num1 x2 y2 num2 x3
#> 1 -0.5426985 -0.5374072 1 1 -0.5426985 0.1922456 2 -0.5426985
#> 2 1.0227475 1.7302578 2 1 1.0227475 -0.3315447 2 1.0227475
#> 3 -2.0349834 -2.8325490 3 1 -2.0349834 -2.4689011 2 -2.0349834
#> 4 -0.9102260 -2.4393909 4 1 -0.9102260 -0.4825135 2 -0.9102260
#> 5 -1.3167978 -2.0478220 5 1 -1.3167978 -1.2542359 2 -1.3167978
#> y3 num3 x4 y4 num4
#> 1 0.7401224 3 -0.5426985 -1.63051519 4
#> 2 0.8675281 3 1.0227475 -0.08805435 4
#> 3 -1.9752101 3 -2.0349834 -2.23098113 4
#> 4 -1.9842807 3 -0.9102260 -0.97943066 4
#> 5 -0.6441103 3 -1.3167978 1.90859645 4
## Restrict set up within-cluster varying variables
fast.reshape(dd,"y",id="id")
#> x y1 id num y2 y3 y4
#> 1 -0.5426985 -0.5374072 1 1 0.1922456 0.7401224 -1.63051519
#> 2 1.0227475 1.7302578 2 1 -0.3315447 0.8675281 -0.08805435
#> 3 -2.0349834 -2.8325490 3 1 -2.4689011 -1.9752101 -2.23098113
#> 4 -0.9102260 -2.4393909 4 1 -0.4825135 -1.9842807 -0.97943066
#> 5 -1.3167978 -2.0478220 5 1 -1.2542359 -0.6441103 1.90859645
fast.reshape(dd,"y",id="id",keep="x",sep=".")
#> x y.1 id y.2 y.3 y.4
#> 1 -0.5426985 -0.5374072 1 0.1922456 0.7401224 -1.63051519
#> 2 1.0227475 1.7302578 2 -0.3315447 0.8675281 -0.08805435
#> 3 -2.0349834 -2.8325490 3 -2.4689011 -1.9752101 -2.23098113
#> 4 -0.9102260 -2.4393909 4 -0.4825135 -1.9842807 -0.97943066
#> 5 -1.3167978 -2.0478220 5 -1.2542359 -0.6441103 1.90859645
#####
x <- data.frame(id=c(5,5,6,6,7),y=1:5,x=1:5,tv=c(1,2,2,1,2))
x
#> id y x tv
#> 1 5 1 1 1
#> 2 5 2 2 2
#> 3 6 3 3 2
#> 4 6 4 4 1
#> 5 7 5 5 2
(xw <- fast.reshape(x,id="id"))
#> id y1 x1 tv1 y2 x2 tv2
#> 1 5 1 1 1 2 2 2
#> 2 6 3 3 2 4 4 1
#> 3 7 5 5 2 NA NA NA
(xl <- fast.reshape(xw,c("y","x"),idname="id2",keep=c()))
#> y x id2 num
#> 1 1 1 1 1
#> 2 2 2 1 2
#> 3 3 3 2 1
#> 4 4 4 2 2
#> 5 5 5 3 1
#> 6 NA NA 3 2
(xl <- fast.reshape(xw,c("y","x","tv")))
#> id y x tv num
#> 1 5 1 1 1 1
#> 2 5 2 2 2 2
#> 3 6 3 3 2 1
#> 4 6 4 4 1 2
#> 5 7 5 5 2 1
#> 6 7 NA NA NA 2
(xw2 <- fast.reshape(xl,id="id",num="num"))
#> id y1 x1 tv1 y2 x2 tv2
#> 1 5 1 1 1 2 2 2
#> 2 6 3 3 2 4 4 1
#> 3 7 5 5 2 NA NA NA
fast.reshape(xw2,c("y","x"),idname="id")
#> id tv1 tv2 y x num
#> 1 5 1 2 1 1 1
#> 2 5 1 2 2 2 2
#> 3 6 2 1 3 3 1
#> 4 6 2 1 4 4 2
#> 5 7 2 NA 5 5 1
#> 6 7 2 NA NA NA 2
### more generally:
### varying=list(c("ym","yf","yb1","yb2"), c("zm","zf","zb1","zb2"))
### varying=list(c("ym","yf","yb1","yb2")))
##### Family cluster example
d <- mets:::simBinFam(3)
d
#> agem agef ageb1 ageb2 xm xf xb1 xb2 ym yf yb1 yb2 id
#> 1 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 0 1 1 1
#> 2 28.99852 30.10294 12.997223 14.579985 1 1 1 1 1 1 0 1 2
#> 3 24.21654 24.58906 5.714862 9.377642 1 1 1 1 1 1 1 1 3
fast.reshape(d,var="y")
#> agem agef ageb1 ageb2 xm xf xb1 xb2 id y num
#> 1 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 1 m
#> 2 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 0 f
#> 3 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 1 b1
#> 4 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 1 b2
#> 5 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 1 m
#> 6 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 1 f
#> 7 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 0 b1
#> 8 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 1 b2
#> 9 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 m
#> 10 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 f
#> 11 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 b1
#> 12 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 b2
fast.reshape(d,varying=list(c("ym","yf","yb1","yb2")))
#> agem agef ageb1 ageb2 xm xf xb1 xb2 id ym num
#> 1 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 1 ym
#> 2 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 0 yf
#> 3 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 1 yb1
#> 4 22.98577 23.76125 13.616006 14.639229 1 1 0 1 1 1 yb2
#> 5 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 1 ym
#> 6 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 1 yf
#> 7 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 0 yb1
#> 8 28.99852 30.10294 12.997223 14.579985 1 1 1 1 2 1 yb2
#> 9 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 ym
#> 10 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 yf
#> 11 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 yb1
#> 12 24.21654 24.58906 5.714862 9.377642 1 1 1 1 3 1 yb2
d <- lava::sim(lava::lvm(~y1+y2+ya),10)
d
#> y1 y2 ya
#> 1 -1.07751966 0.07078768 -1.13476510
#> 2 -0.07170647 1.18883471 0.56192682
#> 3 -0.57401916 1.80565141 -0.82203929
#> 4 1.40098363 1.93306531 -0.07828515
#> 5 1.06150176 1.24960078 0.88573012
#> 6 0.65679574 -1.04701236 0.13618075
#> 7 0.51446894 -0.77793889 -0.44504804
#> 8 1.01551576 0.40196448 0.32596145
#> 9 -0.08513675 -0.55204578 0.64049263
#> 10 -1.22095338 -0.12198455 0.57759366
(dd <- fast.reshape(d,"y"))
#> y id num
#> 1 -1.07751966 1 1
#> 2 0.07078768 1 2
#> 3 -1.13476510 1 a
#> 4 -0.07170647 2 1
#> 5 1.18883471 2 2
#> 6 0.56192682 2 a
#> 7 -0.57401916 3 1
#> 8 1.80565141 3 2
#> 9 -0.82203929 3 a
#> 10 1.40098363 4 1
#> 11 1.93306531 4 2
#> 12 -0.07828515 4 a
#> 13 1.06150176 5 1
#> 14 1.24960078 5 2
#> 15 0.88573012 5 a
#> 16 0.65679574 6 1
#> 17 -1.04701236 6 2
#> 18 0.13618075 6 a
#> 19 0.51446894 7 1
#> 20 -0.77793889 7 2
#> 21 -0.44504804 7 a
#> 22 1.01551576 8 1
#> 23 0.40196448 8 2
#> 24 0.32596145 8 a
#> 25 -0.08513675 9 1
#> 26 -0.55204578 9 2
#> 27 0.64049263 9 a
#> 28 -1.22095338 10 1
#> 29 -0.12198455 10 2
#> 30 0.57759366 10 a
fast.reshape(d,"y",labelnum=TRUE)
#> ya y id num
#> 1 -1.13476510 -1.07751966 1 1
#> 2 -1.13476510 0.07078768 1 2
#> 3 0.56192682 -0.07170647 2 1
#> 4 0.56192682 1.18883471 2 2
#> 5 -0.82203929 -0.57401916 3 1
#> 6 -0.82203929 1.80565141 3 2
#> 7 -0.07828515 1.40098363 4 1
#> 8 -0.07828515 1.93306531 4 2
#> 9 0.88573012 1.06150176 5 1
#> 10 0.88573012 1.24960078 5 2
#> 11 0.13618075 0.65679574 6 1
#> 12 0.13618075 -1.04701236 6 2
#> 13 -0.44504804 0.51446894 7 1
#> 14 -0.44504804 -0.77793889 7 2
#> 15 0.32596145 1.01551576 8 1
#> 16 0.32596145 0.40196448 8 2
#> 17 0.64049263 -0.08513675 9 1
#> 18 0.64049263 -0.55204578 9 2
#> 19 0.57759366 -1.22095338 10 1
#> 20 0.57759366 -0.12198455 10 2
fast.reshape(dd,id="id",num="num")
#> y1 id y2 ya
#> 1 -1.07751966 1 0.07078768 -1.13476510
#> 2 -0.07170647 2 1.18883471 0.56192682
#> 3 -0.57401916 3 1.80565141 -0.82203929
#> 4 1.40098363 4 1.93306531 -0.07828515
#> 5 1.06150176 5 1.24960078 0.88573012
#> 6 0.65679574 6 -1.04701236 0.13618075
#> 7 0.51446894 7 -0.77793889 -0.44504804
#> 8 1.01551576 8 0.40196448 0.32596145
#> 9 -0.08513675 9 -0.55204578 0.64049263
#> 10 -1.22095338 10 -0.12198455 0.57759366
fast.reshape(dd,id="id",num="num",labelnum=TRUE)
#> y1 id y2 y3
#> 1 -1.07751966 1 0.07078768 -1.13476510
#> 2 -0.07170647 2 1.18883471 0.56192682
#> 3 -0.57401916 3 1.80565141 -0.82203929
#> 4 1.40098363 4 1.93306531 -0.07828515
#> 5 1.06150176 5 1.24960078 0.88573012
#> 6 0.65679574 6 -1.04701236 0.13618075
#> 7 0.51446894 7 -0.77793889 -0.44504804
#> 8 1.01551576 8 0.40196448 0.32596145
#> 9 -0.08513675 9 -0.55204578 0.64049263
#> 10 -1.22095338 10 -0.12198455 0.57759366
fast.reshape(d,c(a="y"),labelnum=TRUE) ## New column name
#> ya a id num
#> 1 -1.13476510 -1.07751966 1 1
#> 2 -1.13476510 0.07078768 1 2
#> 3 0.56192682 -0.07170647 2 1
#> 4 0.56192682 1.18883471 2 2
#> 5 -0.82203929 -0.57401916 3 1
#> 6 -0.82203929 1.80565141 3 2
#> 7 -0.07828515 1.40098363 4 1
#> 8 -0.07828515 1.93306531 4 2
#> 9 0.88573012 1.06150176 5 1
#> 10 0.88573012 1.24960078 5 2
#> 11 0.13618075 0.65679574 6 1
#> 12 0.13618075 -1.04701236 6 2
#> 13 -0.44504804 0.51446894 7 1
#> 14 -0.44504804 -0.77793889 7 2
#> 15 0.32596145 1.01551576 8 1
#> 16 0.32596145 0.40196448 8 2
#> 17 0.64049263 -0.08513675 9 1
#> 18 0.64049263 -0.55204578 9 2
#> 19 0.57759366 -1.22095338 10 1
#> 20 0.57759366 -0.12198455 10 2
##### Unbalanced data
m <- lava::lvm(c(y1,y2,y3,y4)~ x+z1+z3+z5)
d <- lava::sim(m,3)
d
#> y1 y2 y3 y4 x z1 z3
#> 1 0.3276725 -1.863180 -0.7273403 -0.6906565 0.7392404 0.9854002 -0.1529171
#> 2 0.7271163 1.225847 2.4705992 1.7261166 -0.3035924 0.9196450 -0.5327047
#> 3 0.6998353 1.039981 -1.2117033 -2.0067813 -1.5486298 0.1712215 1.7735916
#> z5
#> 1 -2.1577183
#> 2 0.5516549
#> 3 -0.7475155
fast.reshape(d,c("y","z"))
#> x y z id num
#> 1 0.7392404 0.3276725 0.9854002 1 1
#> 2 0.7392404 -1.8631800 NA 1 2
#> 3 0.7392404 -0.7273403 -0.1529171 1 3
#> 4 0.7392404 -0.6906565 NA 1 4
#> 5 0.7392404 NA -2.1577183 1 5
#> 6 -0.3035924 0.7271163 0.9196450 2 1
#> 7 -0.3035924 1.2258474 NA 2 2
#> 8 -0.3035924 2.4705992 -0.5327047 2 3
#> 9 -0.3035924 1.7261166 NA 2 4
#> 10 -0.3035924 NA 0.5516549 2 5
#> 11 -1.5486298 0.6998353 0.1712215 3 1
#> 12 -1.5486298 1.0399810 NA 3 2
#> 13 -1.5486298 -1.2117033 1.7735916 3 3
#> 14 -1.5486298 -2.0067813 NA 3 4
#> 15 -1.5486298 NA -0.7475155 3 5
##### not-varying syntax:
fast.reshape(d,-c("x"))
#> x y z id num
#> 1 0.7392404 0.3276725 0.9854002 1 1
#> 2 0.7392404 -1.8631800 NA 1 2
#> 3 0.7392404 -0.7273403 -0.1529171 1 3
#> 4 0.7392404 -0.6906565 NA 1 4
#> 5 0.7392404 NA -2.1577183 1 5
#> 6 -0.3035924 0.7271163 0.9196450 2 1
#> 7 -0.3035924 1.2258474 NA 2 2
#> 8 -0.3035924 2.4705992 -0.5327047 2 3
#> 9 -0.3035924 1.7261166 NA 2 4
#> 10 -0.3035924 NA 0.5516549 2 5
#> 11 -1.5486298 0.6998353 0.1712215 3 1
#> 12 -1.5486298 1.0399810 NA 3 2
#> 13 -1.5486298 -1.2117033 1.7735916 3 3
#> 14 -1.5486298 -2.0067813 NA 3 4
#> 15 -1.5486298 NA -0.7475155 3 5
##### Automatically define varying variables from trailing digits
fast.reshape(d)
#> x y z id num
#> 1 0.7392404 0.3276725 0.9854002 1 1
#> 2 0.7392404 -1.8631800 NA 1 2
#> 3 0.7392404 -0.7273403 -0.1529171 1 3
#> 4 0.7392404 -0.6906565 NA 1 4
#> 5 0.7392404 NA -2.1577183 1 5
#> 6 -0.3035924 0.7271163 0.9196450 2 1
#> 7 -0.3035924 1.2258474 NA 2 2
#> 8 -0.3035924 2.4705992 -0.5327047 2 3
#> 9 -0.3035924 1.7261166 NA 2 4
#> 10 -0.3035924 NA 0.5516549 2 5
#> 11 -1.5486298 0.6998353 0.1712215 3 1
#> 12 -1.5486298 1.0399810 NA 3 2
#> 13 -1.5486298 -1.2117033 1.7735916 3 3
#> 14 -1.5486298 -2.0067813 NA 3 4
#> 15 -1.5486298 NA -0.7475155 3 5
##### Prostate cancer example
data(prt)
head(prtw <- fast.reshape(prt,"cancer",id="id"))
#> country time status zyg id cancer1 cancer2
#> 31 Denmark 96.98833 1 DZ 1 0 0
#> 39 Denmark 68.04498 1 DZ 3 0 0
#> 51 Denmark 78.78068 1 DZ 5 0 0
#> 70 Denmark 100.95488 1 MZ 9 0 0
#> 83 Denmark 104.55035 1 DZ 12 0 1
#> 95 Denmark 95.65324 1 DZ 15 0 0
ftable(cancer1~cancer2,data=prtw)
#> cancer1 0 1
#> cancer2
#> 0 13405 349
#> 1 362 106
rm(prtw)