Skip to contents

Fast reshape/tranpose of data

Usage

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,
  ...
)

Arguments

data

data.frame or matrix

varying

Vector of prefix-names of the time varying variables. Optional for Long->Wide reshaping.

id

id-variable. If omitted then reshape Wide->Long.

num

Optional number/time variable

sep

String seperating prefix-name with number/time

keep

Vector of column names to keep

idname

Name of id-variable (Wide->Long)

numname

Name of number-variable (Wide->Long)

factor

If true all factors are kept (otherwise treated as character)

idcombine

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.

labelnum

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.

labels

Optional labels for the number variable

regex

Use regular expressions

dropid

Drop id in long format (default FALSE)

...

Optional additional arguments

Author

Thomas Scheike, Klaus K. Holst

Examples

m <- lava::lvm(c(y1,y2,y3,y4)~x)
d <- lava::sim(m,5)
d
#>            y1         y2         y3         y4          x
#> 1  1.14678017  2.2066312  2.2134704 0.06503073  1.4738812
#> 2 -0.89181369  1.6238541 -0.3861889 1.59328782  0.6772685
#> 3  0.01251193  0.3843614  0.6261735 0.18868374  0.3799627
#> 4  1.17163650 -0.5451207 -0.4822978 0.61048479 -0.1927984
#> 5  1.24361043  1.0481963 -0.6869976 3.46536626  1.5778918
fast.reshape(d,"y")
#>             x           y id num
#> 1   1.4738812  1.14678017  1   1
#> 2   1.4738812  2.20663122  1   2
#> 3   1.4738812  2.21347041  1   3
#> 4   1.4738812  0.06503073  1   4
#> 5   0.6772685 -0.89181369  2   1
#> 6   0.6772685  1.62385413  2   2
#> 7   0.6772685 -0.38618892  2   3
#> 8   0.6772685  1.59328782  2   4
#> 9   0.3799627  0.01251193  3   1
#> 10  0.3799627  0.38436139  3   2
#> 11  0.3799627  0.62617353  3   3
#> 12  0.3799627  0.18868374  3   4
#> 13 -0.1927984  1.17163650  4   1
#> 14 -0.1927984 -0.54512073  4   2
#> 15 -0.1927984 -0.48229779  4   3
#> 16 -0.1927984  0.61048479  4   4
#> 17  1.5778918  1.24361043  5   1
#> 18  1.5778918  1.04819629  5   2
#> 19  1.5778918 -0.68699756  5   3
#> 20  1.5778918  3.46536626  5   4
fast.reshape(fast.reshape(d,"y"),id="id")
#>           x1          y1 id num1         x2         y2 num2         x3
#> 1  1.4738812  1.14678017  1    1  1.4738812  2.2066312    2  1.4738812
#> 2  0.6772685 -0.89181369  2    1  0.6772685  1.6238541    2  0.6772685
#> 3  0.3799627  0.01251193  3    1  0.3799627  0.3843614    2  0.3799627
#> 4 -0.1927984  1.17163650  4    1 -0.1927984 -0.5451207    2 -0.1927984
#> 5  1.5778918  1.24361043  5    1  1.5778918  1.0481963    2  1.5778918
#>           y3 num3         x4         y4 num4
#> 1  2.2134704    3  1.4738812 0.06503073    4
#> 2 -0.3861889    3  0.6772685 1.59328782    4
#> 3  0.6261735    3  0.3799627 0.18868374    4
#> 4 -0.4822978    3 -0.1927984 0.61048479    4
#> 5 -0.6869976    3  1.5778918 3.46536626    4

##### From wide-format
(dd <- fast.reshape(d,"y"))
#>             x           y id num
#> 1   1.4738812  1.14678017  1   1
#> 2   1.4738812  2.20663122  1   2
#> 3   1.4738812  2.21347041  1   3
#> 4   1.4738812  0.06503073  1   4
#> 5   0.6772685 -0.89181369  2   1
#> 6   0.6772685  1.62385413  2   2
#> 7   0.6772685 -0.38618892  2   3
#> 8   0.6772685  1.59328782  2   4
#> 9   0.3799627  0.01251193  3   1
#> 10  0.3799627  0.38436139  3   2
#> 11  0.3799627  0.62617353  3   3
#> 12  0.3799627  0.18868374  3   4
#> 13 -0.1927984  1.17163650  4   1
#> 14 -0.1927984 -0.54512073  4   2
#> 15 -0.1927984 -0.48229779  4   3
#> 16 -0.1927984  0.61048479  4   4
#> 17  1.5778918  1.24361043  5   1
#> 18  1.5778918  1.04819629  5   2
#> 19  1.5778918 -0.68699756  5   3
#> 20  1.5778918  3.46536626  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   1.14678017 1   1
#> 2   2.20663122 1   2
#> 3   2.21347041 1   3
#> 4   0.06503073 1   4
#> 5  -0.89181369 2   1
#> 6   1.62385413 2   2
#> 7  -0.38618892 2   3
#> 8   1.59328782 2   4
#> 9   0.01251193 3   1
#> 10  0.38436139 3   2
#> 11  0.62617353 3   3
#> 12  0.18868374 3   4
#> 13  1.17163650 4   1
#> 14 -0.54512073 4   2
#> 15 -0.48229779 4   3
#> 16  0.61048479 4   4
#> 17  1.24361043 5   1
#> 18  1.04819629 5   2
#> 19 -0.68699756 5   3
#> 20  3.46536626 5   4
## Same with 'reshape' list-syntax
fast.reshape(d,list(c("y1","y2","y3","y4")),labelnum=TRUE)
#>             x          y1 id num
#> 1   1.4738812  1.14678017  1   1
#> 2   1.4738812  2.20663122  1   2
#> 3   1.4738812  2.21347041  1   3
#> 4   1.4738812  0.06503073  1   4
#> 5   0.6772685 -0.89181369  2   1
#> 6   0.6772685  1.62385413  2   2
#> 7   0.6772685 -0.38618892  2   3
#> 8   0.6772685  1.59328782  2   4
#> 9   0.3799627  0.01251193  3   1
#> 10  0.3799627  0.38436139  3   2
#> 11  0.3799627  0.62617353  3   3
#> 12  0.3799627  0.18868374  3   4
#> 13 -0.1927984  1.17163650  4   1
#> 14 -0.1927984 -0.54512073  4   2
#> 15 -0.1927984 -0.48229779  4   3
#> 16 -0.1927984  0.61048479  4   4
#> 17  1.5778918  1.24361043  5   1
#> 18  1.5778918  1.04819629  5   2
#> 19  1.5778918 -0.68699756  5   3
#> 20  1.5778918  3.46536626  5   4

##### From long-format
fast.reshape(dd,id="id")
#>           x1          y1 id num1         x2         y2 num2         x3
#> 1  1.4738812  1.14678017  1    1  1.4738812  2.2066312    2  1.4738812
#> 2  0.6772685 -0.89181369  2    1  0.6772685  1.6238541    2  0.6772685
#> 3  0.3799627  0.01251193  3    1  0.3799627  0.3843614    2  0.3799627
#> 4 -0.1927984  1.17163650  4    1 -0.1927984 -0.5451207    2 -0.1927984
#> 5  1.5778918  1.24361043  5    1  1.5778918  1.0481963    2  1.5778918
#>           y3 num3         x4         y4 num4
#> 1  2.2134704    3  1.4738812 0.06503073    4
#> 2 -0.3861889    3  0.6772685 1.59328782    4
#> 3  0.6261735    3  0.3799627 0.18868374    4
#> 4 -0.4822978    3 -0.1927984 0.61048479    4
#> 5 -0.6869976    3  1.5778918 3.46536626    4
## Restrict set up within-cluster varying variables
fast.reshape(dd,"y",id="id")
#>            x          y1 id num         y2         y3         y4
#> 1  1.4738812  1.14678017  1   1  2.2066312  2.2134704 0.06503073
#> 2  0.6772685 -0.89181369  2   1  1.6238541 -0.3861889 1.59328782
#> 3  0.3799627  0.01251193  3   1  0.3843614  0.6261735 0.18868374
#> 4 -0.1927984  1.17163650  4   1 -0.5451207 -0.4822978 0.61048479
#> 5  1.5778918  1.24361043  5   1  1.0481963 -0.6869976 3.46536626
fast.reshape(dd,"y",id="id",keep="x",sep=".")
#>            x         y.1 id        y.2        y.3        y.4
#> 1  1.4738812  1.14678017  1  2.2066312  2.2134704 0.06503073
#> 2  0.6772685 -0.89181369  2  1.6238541 -0.3861889 1.59328782
#> 3  0.3799627  0.01251193  3  0.3843614  0.6261735 0.18868374
#> 4 -0.1927984  1.17163650  4 -0.5451207 -0.4822978 0.61048479
#> 5  1.5778918  1.24361043  5  1.0481963 -0.6869976 3.46536626

#####
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:::sim_BinFam(3)
d
#>       agem     agef     ageb1     ageb2 xm xf xb1 xb2 ym yf yb1 yb2 id
#> 1 21.58220 25.79082  6.779497 10.767830  1  1   0   0  1  1   1   0  1
#> 2 28.66809 32.76566  6.648911  9.553596  1  1   1   0  1  1   1   1  2
#> 3 22.06146 26.89432 10.652690 12.938116  1  1   0   0  0  1   1   1  3
fast.reshape(d,var="y")
#>        agem     agef     ageb1     ageb2 xm xf xb1 xb2 id y num
#> 1  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1 1   m
#> 2  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1 1   f
#> 3  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1 1  b1
#> 4  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1 0  b2
#> 5  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2 1   m
#> 6  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2 1   f
#> 7  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2 1  b1
#> 8  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2 1  b2
#> 9  22.06146 26.89432 10.652690 12.938116  1  1   0   0  3 0   m
#> 10 22.06146 26.89432 10.652690 12.938116  1  1   0   0  3 1   f
#> 11 22.06146 26.89432 10.652690 12.938116  1  1   0   0  3 1  b1
#> 12 22.06146 26.89432 10.652690 12.938116  1  1   0   0  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  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1  1  ym
#> 2  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1  1  yf
#> 3  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1  1 yb1
#> 4  21.58220 25.79082  6.779497 10.767830  1  1   0   0  1  0 yb2
#> 5  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2  1  ym
#> 6  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2  1  yf
#> 7  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2  1 yb1
#> 8  28.66809 32.76566  6.648911  9.553596  1  1   1   0  2  1 yb2
#> 9  22.06146 26.89432 10.652690 12.938116  1  1   0   0  3  0  ym
#> 10 22.06146 26.89432 10.652690 12.938116  1  1   0   0  3  1  yf
#> 11 22.06146 26.89432 10.652690 12.938116  1  1   0   0  3  1 yb1
#> 12 22.06146 26.89432 10.652690 12.938116  1  1   0   0  3  1 yb2

d <- lava::sim(lava::lvm(~y1+y2+ya),10)
d
#>            y1         y2         ya
#> 1  -1.9049554 -0.7151284  1.4418204
#> 2  -0.8111702  0.8652231 -0.6975383
#> 3   1.3240043  1.0744410 -0.3881675
#> 4   0.6156368  1.8956548  0.6525365
#> 5   1.0916690 -0.6029973  1.1247724
#> 6   0.3066049 -0.3908678 -0.7721108
#> 7  -0.1101588 -0.4162220 -0.5080862
#> 8  -0.9243128 -0.3756574  0.5236206
#> 9   1.5929138 -0.3666309  1.0177542
#> 10  0.0450106 -0.2956775 -0.2511646
(dd <- fast.reshape(d,"y"))
#>             y id num
#> 1  -1.9049554  1   1
#> 2  -0.7151284  1   2
#> 3   1.4418204  1   a
#> 4  -0.8111702  2   1
#> 5   0.8652231  2   2
#> 6  -0.6975383  2   a
#> 7   1.3240043  3   1
#> 8   1.0744410  3   2
#> 9  -0.3881675  3   a
#> 10  0.6156368  4   1
#> 11  1.8956548  4   2
#> 12  0.6525365  4   a
#> 13  1.0916690  5   1
#> 14 -0.6029973  5   2
#> 15  1.1247724  5   a
#> 16  0.3066049  6   1
#> 17 -0.3908678  6   2
#> 18 -0.7721108  6   a
#> 19 -0.1101588  7   1
#> 20 -0.4162220  7   2
#> 21 -0.5080862  7   a
#> 22 -0.9243128  8   1
#> 23 -0.3756574  8   2
#> 24  0.5236206  8   a
#> 25  1.5929138  9   1
#> 26 -0.3666309  9   2
#> 27  1.0177542  9   a
#> 28  0.0450106 10   1
#> 29 -0.2956775 10   2
#> 30 -0.2511646 10   a
fast.reshape(d,"y",labelnum=TRUE)
#>            ya          y id num
#> 1   1.4418204 -1.9049554  1   1
#> 2   1.4418204 -0.7151284  1   2
#> 3  -0.6975383 -0.8111702  2   1
#> 4  -0.6975383  0.8652231  2   2
#> 5  -0.3881675  1.3240043  3   1
#> 6  -0.3881675  1.0744410  3   2
#> 7   0.6525365  0.6156368  4   1
#> 8   0.6525365  1.8956548  4   2
#> 9   1.1247724  1.0916690  5   1
#> 10  1.1247724 -0.6029973  5   2
#> 11 -0.7721108  0.3066049  6   1
#> 12 -0.7721108 -0.3908678  6   2
#> 13 -0.5080862 -0.1101588  7   1
#> 14 -0.5080862 -0.4162220  7   2
#> 15  0.5236206 -0.9243128  8   1
#> 16  0.5236206 -0.3756574  8   2
#> 17  1.0177542  1.5929138  9   1
#> 18  1.0177542 -0.3666309  9   2
#> 19 -0.2511646  0.0450106 10   1
#> 20 -0.2511646 -0.2956775 10   2
fast.reshape(dd,id="id",num="num")
#>            y1 id         y2         ya
#> 1  -1.9049554  1 -0.7151284  1.4418204
#> 2  -0.8111702  2  0.8652231 -0.6975383
#> 3   1.3240043  3  1.0744410 -0.3881675
#> 4   0.6156368  4  1.8956548  0.6525365
#> 5   1.0916690  5 -0.6029973  1.1247724
#> 6   0.3066049  6 -0.3908678 -0.7721108
#> 7  -0.1101588  7 -0.4162220 -0.5080862
#> 8  -0.9243128  8 -0.3756574  0.5236206
#> 9   1.5929138  9 -0.3666309  1.0177542
#> 10  0.0450106 10 -0.2956775 -0.2511646
fast.reshape(dd,id="id",num="num",labelnum=TRUE)
#>            y1 id         y2         y3
#> 1  -1.9049554  1 -0.7151284  1.4418204
#> 2  -0.8111702  2  0.8652231 -0.6975383
#> 3   1.3240043  3  1.0744410 -0.3881675
#> 4   0.6156368  4  1.8956548  0.6525365
#> 5   1.0916690  5 -0.6029973  1.1247724
#> 6   0.3066049  6 -0.3908678 -0.7721108
#> 7  -0.1101588  7 -0.4162220 -0.5080862
#> 8  -0.9243128  8 -0.3756574  0.5236206
#> 9   1.5929138  9 -0.3666309  1.0177542
#> 10  0.0450106 10 -0.2956775 -0.2511646
fast.reshape(d,c(a="y"),labelnum=TRUE) ## New column name
#>            ya          a id num
#> 1   1.4418204 -1.9049554  1   1
#> 2   1.4418204 -0.7151284  1   2
#> 3  -0.6975383 -0.8111702  2   1
#> 4  -0.6975383  0.8652231  2   2
#> 5  -0.3881675  1.3240043  3   1
#> 6  -0.3881675  1.0744410  3   2
#> 7   0.6525365  0.6156368  4   1
#> 8   0.6525365  1.8956548  4   2
#> 9   1.1247724  1.0916690  5   1
#> 10  1.1247724 -0.6029973  5   2
#> 11 -0.7721108  0.3066049  6   1
#> 12 -0.7721108 -0.3908678  6   2
#> 13 -0.5080862 -0.1101588  7   1
#> 14 -0.5080862 -0.4162220  7   2
#> 15  0.5236206 -0.9243128  8   1
#> 16  0.5236206 -0.3756574  8   2
#> 17  1.0177542  1.5929138  9   1
#> 18  1.0177542 -0.3666309  9   2
#> 19 -0.2511646  0.0450106 10   1
#> 20 -0.2511646 -0.2956775 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
#> 1 -1.9499089 -1.2302866  0.04980754 -1.299483941 -2.0332856 -0.02499764
#> 2  2.3435215  0.5693329  2.24674727 -0.006776464  0.5009636  0.59298472
#> 3  0.8300267 -2.3645116 -2.24232347 -1.286174221 -1.5317981 -0.19819542
#>            z3         z5
#> 1  0.89200839  0.6463594
#> 2 -0.02571507 -0.4338327
#> 3 -0.64766045  1.7726112
fast.reshape(d,c("y","z"))
#>             x            y           z id num
#> 1  -2.0332856 -1.949908875 -0.02499764  1   1
#> 2  -2.0332856 -1.230286573          NA  1   2
#> 3  -2.0332856  0.049807544  0.89200839  1   3
#> 4  -2.0332856 -1.299483941          NA  1   4
#> 5  -2.0332856           NA  0.64635942  1   5
#> 6   0.5009636  2.343521501  0.59298472  2   1
#> 7   0.5009636  0.569332896          NA  2   2
#> 8   0.5009636  2.246747268 -0.02571507  2   3
#> 9   0.5009636 -0.006776464          NA  2   4
#> 10  0.5009636           NA -0.43383274  2   5
#> 11 -1.5317981  0.830026745 -0.19819542  3   1
#> 12 -1.5317981 -2.364511562          NA  3   2
#> 13 -1.5317981 -2.242323474 -0.64766045  3   3
#> 14 -1.5317981 -1.286174221          NA  3   4
#> 15 -1.5317981           NA  1.77261118  3   5

##### not-varying syntax:
fast.reshape(d,-c("x"))
#>             x            y           z id num
#> 1  -2.0332856 -1.949908875 -0.02499764  1   1
#> 2  -2.0332856 -1.230286573          NA  1   2
#> 3  -2.0332856  0.049807544  0.89200839  1   3
#> 4  -2.0332856 -1.299483941          NA  1   4
#> 5  -2.0332856           NA  0.64635942  1   5
#> 6   0.5009636  2.343521501  0.59298472  2   1
#> 7   0.5009636  0.569332896          NA  2   2
#> 8   0.5009636  2.246747268 -0.02571507  2   3
#> 9   0.5009636 -0.006776464          NA  2   4
#> 10  0.5009636           NA -0.43383274  2   5
#> 11 -1.5317981  0.830026745 -0.19819542  3   1
#> 12 -1.5317981 -2.364511562          NA  3   2
#> 13 -1.5317981 -2.242323474 -0.64766045  3   3
#> 14 -1.5317981 -1.286174221          NA  3   4
#> 15 -1.5317981           NA  1.77261118  3   5

##### Automatically define varying variables from trailing digits
fast.reshape(d)
#>             x            y           z id num
#> 1  -2.0332856 -1.949908875 -0.02499764  1   1
#> 2  -2.0332856 -1.230286573          NA  1   2
#> 3  -2.0332856  0.049807544  0.89200839  1   3
#> 4  -2.0332856 -1.299483941          NA  1   4
#> 5  -2.0332856           NA  0.64635942  1   5
#> 6   0.5009636  2.343521501  0.59298472  2   1
#> 7   0.5009636  0.569332896          NA  2   2
#> 8   0.5009636  2.246747268 -0.02571507  2   3
#> 9   0.5009636 -0.006776464          NA  2   4
#> 10  0.5009636           NA -0.43383274  2   5
#> 11 -1.5317981  0.830026745 -0.19819542  3   1
#> 12 -1.5317981 -2.364511562          NA  3   2
#> 13 -1.5317981 -2.242323474 -0.64766045  3   3
#> 14 -1.5317981 -1.286174221          NA  3   4
#> 15 -1.5317981           NA  1.77261118  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)