Reshape memisc 0.99.20.1

Reshape data frames or data sets

Description

Reshape is a convenience wrapper around reshape with a somewhat simpler syntax.

Usage

Reshape(data,spec,direction)

Arguments

data

a data frame or data set to be reshaped.

spec

a list of vectors of variable names that represent repeated measurements and a vector of values that indentify measurement occasions.

direction

a character string, should be either equal “long” or “wide”.

Examples

example.data.wide <- data.frame(
   v  = c(35,42),
   x1 = c(1.1,2.1),
   x2 = c(1.2,2.2),
   x3 = c(1.3,2.3),
   x4 = c(1.4,2.4),
   y1 = c(2.5,3.5),
   y2 = c(2.7,3.7),
   y3 = c(2.9,3.9))
example.data.wide
   v  x1  x2  x3  x4  y1  y2  y3
1 35 1.1 1.2 1.3 1.4 2.5 2.7 2.9
2 42 2.1 2.2 2.3 2.4 3.5 3.7 3.9
example.data.long <- Reshape(data=example.data.wide,
                            list(
                                x=c(x1,x2,x3,x4),
                                # N.B. it is possible to
                                # specify 'empty' i.e. missing
                                # measurements
                                y=c(y1,y2,y3,),
                                t=1:4
                            ),
                            direction="long")
example.data.long
     v t   x   y id
1.1 35 1 1.1 2.5  1
1.2 35 2 1.2 2.7  1
1.3 35 3 1.3 2.9  1
1.4 35 4 1.4  NA  1
2.1 42 1 2.1 3.5  2
2.2 42 2 2.2 3.7  2
2.3 42 3 2.3 3.9  2
2.4 42 4 2.4  NA  2
example.data.wide <- Reshape(data=example.data.long,
                            list(
                                x=c(x1,x2,x3,x4),
                                y=c(y1,y2,y3,),
                                t=1:4
                            ),
                            direction="wide")
example.data.wide
   v id  x1  x2  x3  x4  y1  y2  y3
1 35  1 1.1 1.2 1.3 1.4 2.5 2.7 2.9
2 42  2 2.1 2.2 2.3 2.4 3.5 3.7 3.9
example.data.long <- Reshape(data=example.data.wide,
                            list(
                                x=c(x1,x2,x3,x4),
                                y=c(y1,y2,y3,)
                            ),
                            direction="long")
example.data.long
     v id _t_   x   y
1.1 35  1   1 1.1 2.5
1.2 35  1   2 1.2 2.7
1.3 35  1   3 1.3 2.9
1.4 35  1   4 1.4  NA
2.1 42  2   1 2.1 3.5
2.2 42  2   2 2.2 3.7
2.3 42  2   3 2.3 3.9
2.4 42  2   4 2.4  NA
example.data.wide <- Reshape(data=example.data.long,
                            list(
                                x=c(x1,x2,x3,x4),
                                y=c(y1,y2,y3,)
                            ),
                            direction="wide")
example.data.wide
   v id  x1  x2  x3  x4  y1  y2  y3
1 35  1 1.1 1.2 1.3 1.4 2.5 2.7 2.9
2 42  2 2.1 2.2 2.3 2.4 3.5 3.7 3.9