| 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{ |
| 8 |
Reshape(data,\dots,id,within_id,drop,keep,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{keep}{a variable name or a concatenation of variable names |
| 41 |
(either as character strings or as unquoted symbols), thast specifies |
| 42 |
the variables to be kept after reshaping (including the ones |
| 43 |
used to define the reshaping). |
| 44 |
} |
| 45 |
\item{direction}{a character string, should be either equal "long" |
| 46 |
or "wide".} |
| 47 |
} |
| 48 |
\examples{ |
| 49 |
example.data.wide <- data.frame( |
| 50 |
v = c(35,42), |
| 51 |
x1 = c(1.1,2.1), |
| 52 |
x2 = c(1.2,2.2), |
| 53 |
x3 = c(1.3,2.3), |
| 54 |
x4 = c(1.4,2.4), |
| 55 |
y1 = c(2.5,3.5), |
| 56 |
y2 = c(2.7,3.7), |
| 57 |
y3 = c(2.9,3.9)) |
| 58 |
example.data.wide |
| 59 |
|
| 60 |
# The following two calls are equivalent: |
| 61 |
example.data.long <- Reshape(data=example.data.wide, |
| 62 |
x=c(x1,x2,x3,x4), |
| 63 |
# N.B. it is possible to |
| 64 |
# specify 'empty' i.e. missing |
| 65 |
# measurements |
| 66 |
y=c(y1,y2,y3,), |
| 67 |
t=1:4, |
| 68 |
direction="long") |
| 69 |
|
| 70 |
example.data.long <- Reshape(data=example.data.wide, |
| 71 |
list( |
| 72 |
x=c(x1,x2,x3,x4), |
| 73 |
# N.B. it is possible to |
| 74 |
# specify 'empty' i.e. missing |
| 75 |
# measurements |
| 76 |
y=c(y1,y2,y3,) |
| 77 |
), |
| 78 |
t=1:4, |
| 79 |
direction="long") |
| 80 |
|
| 81 |
example.data.long |
| 82 |
|
| 83 |
# Since the data frame contains an "reshapeLong" attribute |
| 84 |
# an id variable is already specified and part of the data |
| 85 |
# frame. |
| 86 |
example.data.wide <- Reshape(data=example.data.long, |
| 87 |
x=c(x1,x2,x3,x4), |
| 88 |
y=c(y1,y2,y3,), |
| 89 |
t=1:4, |
| 90 |
direction="wide") |
| 91 |
example.data.wide |
| 92 |
|
| 93 |
# Here we examine the case where no "reshapeLong" attribute |
| 94 |
# is present: |
| 95 |
example.data.wide <- Reshape(data=example.data.long, |
| 96 |
x=c(x1,x2,x3,x4), |
| 97 |
y=c(y1,y2,y3,), |
| 98 |
t=1:4, |
| 99 |
id=v, |
| 100 |
direction="wide") |
| 101 |
example.data.wide |
| 102 |
|
| 103 |
|
| 104 |
# Here, an "automatic" time variable is created. This works |
| 105 |
# only if there is a single argument other than the data= |
| 106 |
# and direction= arguments |
| 107 |
example.data.long <- Reshape(data=example.data.wide, |
| 108 |
list( |
| 109 |
x=c(x1,x2,x3,x4), |
| 110 |
y=c(y1,y2,y3,) |
| 111 |
), |
| 112 |
direction="long") |
| 113 |
|
| 114 |
example.data.long |
| 115 |
|
| 116 |
example.data.wide <- Reshape(data=example.data.long, |
| 117 |
list( |
| 118 |
x=c(x1,x2,x3,x4), |
| 119 |
y=c(y1,y2,y3,) |
| 120 |
), |
| 121 |
direction="wide") |
| 122 |
example.data.wide |
| 123 |
} |