1\name{Reshape}
2\alias{Reshape}
3\title{Reshape data frames or data sets}
4\description{\code{Reshape} is a convenience
5  wrapper around \code{\link[stats]{reshape}} with a somewhat simpler
6  syntax.}
7\usage{
8Reshape(data,\dots,id,within_id,drop,direction)
9}
10\arguments{
11  \item{data}{a data frame or data set to be reshaped.}
12  \item{\dots}{
13    Further arguments that specify the variables in
14    long and in wide format as well as the time variable.
15    The name tags of the arguments given here specify
16    variable names in long format,
17    the arguments themselves specify the variables in wide format
18    (or observations in long vormat)
19    and the variable of the "time" variable.
20    The time variable is usually the last of these arguments.
21    An "automatic" time variable can be specified if only
22    a single argument in \code{\dots} is given.
23  }
24  \item{id}{a variable name or a concatenation of variable names
25      (either as character strings or as unquoted symbols), that identify
26      individual units. Defaults to \code{"id"} or the id variable
27      specified in the \code{"reshapeLong"} attribute of the \code{data}
28      argument. Needed only if the data are reshaped from long to wide
29      format.
30  }
31  \item{within_id}{an optional variable name
32      (either as character string or as unquoted symbol), that identifies
33      individual observations on units.
34      Relevant only if the data are reshaped from long to wide
35      format.}
36  \item{drop}{a variable name or a concatenation of variable names
37      (either as character strings or as unquoted symbols), thast specifies
38      the variables to be dropped before reshaping.
39  }
40  \item{direction}{a character string, should be either equal "long"
41    or "wide".}
42}
43\examples{
44example.data.wide <- data.frame(
45    v  = c(35,42),
46    x1 = c(1.1,2.1),
47    x2 = c(1.2,2.2),
48    x3 = c(1.3,2.3),
49    x4 = c(1.4,2.4),
50    y1 = c(2.5,3.5),
51    y2 = c(2.7,3.7),
52    y3 = c(2.9,3.9))
53example.data.wide
54
55# The following two calls are equivalent:
56example.data.long <- Reshape(data=example.data.wide,
57                             x=c(x1,x2,x3,x4),
58                             # N.B. it is possible to
59                             # specify 'empty' i.e. missing
60                             # measurements
61                             y=c(y1,y2,y3,),
62                             t=1:4,
63                             direction="long")
64
65example.data.long <- Reshape(data=example.data.wide,
66                             list(
67                                 x=c(x1,x2,x3,x4),
68                                 # N.B. it is possible to
69                                 # specify 'empty' i.e. missing
70                                 # measurements
71                                 y=c(y1,y2,y3,)
72                             ),
73                             t=1:4,
74                             direction="long")
75
76example.data.long
77
78# Since the data frame contains an "reshapeLong" attribute
79# an id variable is already specified and part of the data
80# frame.
81example.data.wide <- Reshape(data=example.data.long,
82                             x=c(x1,x2,x3,x4),
83                             y=c(y1,y2,y3,),
84                             t=1:4,
85                             direction="wide")
86example.data.wide
87
88# Here we examine the case where no "reshapeLong" attribute
89# is present:
90example.data.wide <- Reshape(data=example.data.long,
91                             x=c(x1,x2,x3,x4),
92                             y=c(y1,y2,y3,),
93                             t=1:4,
94                             id=v,
95                             direction="wide")
96example.data.wide
97
98
99# Here, an "automatic" time variable is created. This works
100# only if there is a single argument other than the data=
101# and direction= arguments
102example.data.long <- Reshape(data=example.data.wide,
103                             list(
104                                 x=c(x1,x2,x3,x4),
105                                 y=c(y1,y2,y3,)
106                             ),
107                             direction="long")
108
109example.data.long
110
111example.data.wide <- Reshape(data=example.data.long,
112                             list(
113                                 x=c(x1,x2,x3,x4),
114                                 y=c(y1,y2,y3,)
115                             ),
116                             direction="wide")
117example.data.wide
118}
119