Reshape
Reshape
Reshape
URL http://had.co.nz/reshape
Depends R (>= 2.6.1)
Imports plyr
License MIT + file LICENSE
LazyData true
NeedsCompilation yes
Author Hadley Wickham [aut, cre]
Maintainer Hadley Wickham <[email protected]>
Repository CRAN
Date/Publication 2018-10-23 18:50:03 UTC
R topics documented:
cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
colsplit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
combine_factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
condense.df . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
expand.grid.df . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
French fries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
funstofun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
melt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
melt.array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
melt.data.frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
merge_all . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
namerows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
recast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1
2 cast
rescaler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Smiths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
sort_df . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
sparseby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
stamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
uniquedefault . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
untable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Index 20
Description
Cast a molten data frame into the reshaped or aggregated form you want
Usage
Arguments
Details
Along with melt and recast, this is the only function you should ever need to use. Once you have
melted your data, cast will arrange it into the form you desire based on the specification given by
formula.
The cast formula has the following format: x_variable + x_2 ~ y_variable + y_2 ~ z_variable ~ ... | list_varia
order of the variables makes a difference. The first varies slowest, and the last fastest. There are
a couple of special variables: "..." represents all other variables not used in the formula and "."
represents no variable, so you can do formula=var1 ~ .
Creating high-D arrays is simple, and allows a class of transformations that are hard without apply
and sweep
If the combination of variables you supply does not uniquely identify one row in the original data
set, you will need to supply an aggregating function, fun.aggregate. This function should take a
vector of numbers and return a summary statistic(s). It must return the same number of arguments
regardless of the length of the input vector. If it returns multiple value you can use "result\_variable"
to control where they appear. By default they will appear as the last column variable.
The margins argument should be passed a vector of variable names, eg. c("month","day"). It
will silently drop any variables that can not be margined over. You can also use "grand\_col" and
"grand\_row" to get grand row and column margins respectively.
Subset takes a logical vector that will be evaluated in the context of data, so you can do something
like subset = variable=="length"
All the actual reshaping is done by reshape1, see its documentation for details of the implementa-
tion
Author(s)
Hadley Wickham <[email protected]>
See Also
reshape1, http://had.co.nz/reshape/
Examples
#Air quality example
names(airquality) <- tolower(names(airquality))
aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)
#Tips example
cast(melt(tips), sex ~ smoker, mean, subset=variable=="total_bill")
cast(melt(tips), sex ~ smoker | variable, mean)
Description
This function can be used to split up a column that has been pasted together.
Usage
colsplit(x, split="", names)
Arguments
x character vector or factor to split up
split regular expression to split on
names names for output columns
Author(s)
Hadley Wickham <[email protected]>
combine_factor 5
Description
Convenience function to make it easy to combine multiple levels
Usage
combine_factor(fac, variable=levels(fac), other.label="Other")
Arguments
fac factor variable
variable either a vector of . See examples for more details.
other.label label for other level
Author(s)
Hadley Wickham <[email protected]>
Examples
df <- data.frame(a = LETTERS[sample(5, 15, replace=TRUE)], y = rnorm(15))
combine_factor(df$a, c(1,2,2,1,2))
combine_factor(df$a, c(1:4, 1))
(f <- reorder(df$a, df$y))
percent <- tapply(abs(df$y), df$a, sum)
combine_factor(f, c(order(percent)[1:3]))
Description
Condense
Usage
condense.df(data, variables, fun, ...)
Arguments
data data frame
variables character vector of variables to condense over
fun function to condense with
... arguments passed to condensing function
6 expand.grid.df
Author(s)
Description
Usage
expand.grid.df(..., unique=TRUE)
Arguments
Details
Creates new data frame containing all combination of rows from data.frames in ...
Author(s)
Examples
expand.grid.df(data.frame(a=1,b=1:2))
expand.grid.df(data.frame(a=1,b=1:2), data.frame())
expand.grid.df(data.frame(a=1,b=1:2), data.frame(c=1:2, d=1:2))
expand.grid.df(data.frame(a=1,b=1:2), data.frame(c=1:2, d=1:2), data.frame(e=c("a","b")))
French fries 7
Description
This data was collected from a sensory experiment conducted at Iowa State University in 2004. The
investigators were interested in the effect of using three different fryer oils had on the taste of the
fries.
Variables:
Usage
data(french_fries)
Format
A data frame with 696 rows and 9 variables
Description
Combine multiple functions to a single function returning a named vector of outputs
Usage
funstofun(...)
Arguments
... functions to combine
8 melt
Details
Author(s)
Examples
funstofun(min, max)(1:10)
funstofun(length, mean, var)(rnorm(100))
melt Melt
Description
Usage
melt(data, ...)
Arguments
Details
This the generic melt function. See the following functions for specific details for different data
structures:
Author(s)
Description
This function melts a high-dimensional array into a form that you can use cast with.
Usage
Arguments
Details
Author(s)
Examples
Description
Melt a data frame into form suitable for easy casting.
Usage
## S3 method for class 'data.frame'
melt(data, id.vars, measure.vars,
variable_name = "variable", na.rm = !preserve.na, preserve.na = TRUE, ...)
Arguments
data Data set to melt
id.vars Id variables. If blank, will use all non measure.vars variables. Can be integer
(variable position) or string (variable name)
measure.vars Measured variables. If blank, will use all non id.vars variables. Can be integer
(variable position) or string (variable name)
variable_name Name of the variable that will store the names of the original variables
na.rm Should NA values be removed from the data set?
preserve.na Old argument name, now deprecated
... other arguments ignored
Details
You need to tell melt which of your variables are id variables, and which are measured variables.
If you only supply one of id.vars and measure.vars, melt will assume the remainder of the
variables in the data set belong to the other. If you supply neither, melt will assume factor and
character variables are id variables, and all others are measured.
Value
molten data
Author(s)
Hadley Wickham <[email protected]>
See Also
http://had.co.nz/reshape/
merge_all 11
Examples
head(melt(tips))
names(airquality) <- tolower(names(airquality))
melt(airquality, id=c("month", "day"))
names(ChickWeight) <- tolower(names(ChickWeight))
melt(ChickWeight, id=2:4)
Description
Usage
merge_all(dfs, ...)
Arguments
Details
Author(s)
See Also
merge_recurse
12 recast
Description
Add variable to data frame containing rownames
Usage
namerows(df, col.name = "id")
Arguments
df data frame
col.name name of new column containing rownames
Details
This is useful when the thing that you want to melt by is the rownames of the data frame, not an
explicit variable
Author(s)
Hadley Wickham <[email protected]>
recast Recast
Description
melt and cast data in a single step
Usage
recast(data, formula, ..., id.var, measure.var)
Arguments
data Data set to melt
formula Casting formula, see cast for specifics
... Other arguments passed to cast
id.var Identifying variables. If blank, will use all non measure.var variables
measure.var Measured variables. If blank, will use all non id.var variables
rename 13
Details
This conveniently wraps melting and casting a data frame into one step.
Author(s)
See Also
http://had.co.nz/reshape/
Examples
rename Rename
Description
Rename an object
Usage
rename(x, replace)
Arguments
x object to be renamed
replace named vector specifying new names
Details
The rename function provide an easy way to rename the columns of a data.frame or the items in a
list.
Author(s)
Examples
rename(mtcars, c(wt = "weight", cyl = "cylinders"))
a <- list(a = 1, b = 2, c = 3)
rename(a, c(b = "a", c = "b", a="c"))
rescaler Rescaler
Description
Convenient methods for rescaling data
Usage
rescaler(x, type="sd", ...)
Arguments
x object to rescale
type type of rescaling to use (see description for details)
... other options (only pasesed to rank)
Details
Provides methods for vectors, matrices and data.frames
Currently, five rescaling options are implemented:
• I: do nothing
• range: scale to [0, 1]
• rank: convert values to ranks
• robust: robust version of sd, substract median and divide by median absolute deviation
• sd: subtract mean and divide by standard deviation
Smiths 15
Author(s)
Hadley Wickham <[email protected]>
See Also
rescaler.default
Description
A small demo dataset describing John and Mary Smith. Used in the introductory vignette.
Usage
data(smiths)
Format
A data frame with 2 rows and 5 variables
Description
Convenience method for sorting a data frame using the given variables.
Usage
sort_df(data, vars=names(data))
Arguments
data data frame to sort
vars variables to use for sorting
Details
Simple wrapper around order
Author(s)
Hadley Wickham <[email protected]>
16 sparseby
Description
Function sparseby is a modified version of by for tapply applied to data frames. It always returns
a new data frame rather than a multi-way array.
Usage
sparseby(data, INDICES = list(), FUN, ..., GROUPNAMES = TRUE)
Arguments
data an R object, normally a data frame, possibly a matrix.
INDICES a variable or list of variables indicating the subgroups of data
FUN a function to be applied to data frame subsets of data.
... further arguments to FUN.
GROUPNAMES a logical variable indicating whether the group names should be bound to the
result
Details
A data frame or matrix is split by row into data frames or matrices respectively subsetted by the
values of one or more factors, and function FUN is applied to each subset in turn.
sparseby is much faster and more memory efficient than by or tapply in the situation where the
combinations of INDICES present in the data form a sparse subset of all possible combinations.
Value
A data frame or matrix containing the results of FUN applied to each subgroup of the matrix. The
result depends on what is returned from FUN:
If FUN returns NULL on any subsets, those are dropped.
If it returns a single value or a vector of values, the length must be consistent across all subgroups.
These will be returned as values in rows of the resulting data frame or matrix.
If it returns data frames or matrices, they must all have the same number of columns, and they will
be bound with rbind into a single data frame or matrix.
Names for the columns will be taken from the names in the list of INDICES or from the results of
FUN, as appropriate.
Author(s)
Duncan Murdoch
stamp 17
See Also
tapply, by
Examples
x <- data.frame(index=c(rep(1,4),rep(2,3)),value=c(1:7))
x
sparseby(x,x$index,nrow)
stamp Stamp
Description
Stamp is like reshape but the "stamping" function is passed the entire data frame, instead of just a
few variables.
Usage
stamp(data, formula = . ~ ., fun.aggregate, ..., margins=NULL,
subset=TRUE, add.missing=FALSE)
Arguments
data data.frame (no molten)
formula formula that describes arrangement of result, columns ~ rows, see reshape for
more information
fun.aggregate aggregation function to use, should take a data frame as the first argument
... arguments passed to the aggregation function
margins margins to compute (character vector, or TRUE for all margins), can contain
grand_row or grand_col to inclue grand row or column margins respectively.
subset logical vector by which to subset the data frame, evaluated in the context of the
data frame so you can
add.missing fill in missing combinations?
Details
It is very similar to the by function except in the form of the output which is arranged using the
formula as in reshape
Note that it’s very easy to create objects that R can’t print with this function. You will probably
want to save the results to a variable and then use extract the results. See the examples.
18 Tips
Author(s)
Description
One waiter recorded information about each tip he received over a period of a few months working
in one restaurant. He collected several variables:
• tip in dollars,
• bill in dollars,
• sex of the bill payer,
• whether there were smokers in the party,
• day of the week,
• time of day,
• size of the party.
In all he recorded 244 tips. The data was reported in a collection of case studies for business
statistics (Bryant & Smith 1995).
Usage
data(tips)
Format
References
Bryant, P. G. and Smith, M (1995) Practical Data Analysis: Case Studies in Business Statistics.
Homewood, IL: Richard D. Irwin Publishing:
uniquedefault 19
Description
Convenience function for setting default if not unique
Usage
uniquedefault(values, default)
Arguments
values vector of values
default default to use if values not uniquez
Details
Used by ggplot2
Author(s)
Hadley Wickham <[email protected]>
Description
Inverse of table
Usage
untable(df, num)
Arguments
df matrix or data.frame to untable
num vector of counts (of same length as df)
Details
Given a tabulated dataset (or matrix) this will untabulate it by repeating each row by the number of
times it was repeated
Author(s)
Hadley Wickham <[email protected]>
Index
expand.grid.df, 6
French fries, 7
20