longToWide | R Documentation |
Given a data frame or matrix in long format, convert it to wide format based on
the levels of two variables in the data frame. This is a simplified version of
the R function reshape
with the argument direction="wide"
.
longToWide(x, data.var, row.var, col.var,
row.labels = levels(factor(x[, row.var])),
col.labels = levels(factor(x[, col.var])),
paste.row.name = FALSE, paste.col.name = FALSE, sep = ".",
check.names = FALSE, ...)
x |
data frame or matrix to convert to wide format. Must have at least 3 columns corresponding to the data variable, row variable, and column variable, respectively. |
data.var |
character string or numeric scalar indicating column variable name in |
row.var |
character string or numeric scalar indicating column variable name in |
col.var |
character string or numeric scalar indicating column variable name in |
row.labels |
optional character vector indicating labels to use for rows. The default value is the levels
of the variable indicated by |
col.labels |
optional character vector indicating labels to use for columns. The default value is the levels
of the variable indicated by |
paste.row.name |
logical scalar indicating whether to paste the name of the variable used to define the row names
(i.e., the value of |
paste.col.name |
logical scalar indicating whether to paste the name of the variable used to define the column names
(i.e., the value of |
sep |
character string separator used when |
check.names |
argument to |
... |
other arguments to |
The combination of values in x[, row.var]
and x[, col.var]
must yield
n
unique values, where n
is the number of rows in x
.
longToWide
returns a matrix when x
is a matrix and a data frame when x
is a data frame. The number of rows is equal to the number of
unique values in x[, row.var]
and the number of columns is equal to the number of
unique values in x[, col.var]
.
Steven P. Millard (EnvStats@ProbStatInfo.com), based on a template from Phil Dixon.
reshape
, data.frame
, matrix
.
EPA.09.Ex.10.1.nickel.df
# Month Well Nickel.ppb
#1 1 Well.1 58.8
#2 3 Well.1 1.0
#3 6 Well.1 262.0
#4 8 Well.1 56.0
#5 10 Well.1 8.7
#6 1 Well.2 19.0
#7 3 Well.2 81.5
#8 6 Well.2 331.0
#9 8 Well.2 14.0
#10 10 Well.2 64.4
#11 1 Well.3 39.0
#12 3 Well.3 151.0
#13 6 Well.3 27.0
#14 8 Well.3 21.4
#15 10 Well.3 578.0
#16 1 Well.4 3.1
#17 3 Well.4 942.0
#18 6 Well.4 85.6
#19 8 Well.4 10.0
#20 10 Well.4 637.0
longToWide(EPA.09.Ex.10.1.nickel.df,
"Nickel.ppb", "Month", "Well", paste.row.name = TRUE)
# Well.1 Well.2 Well.3 Well.4
#Month.1 58.8 19.0 39.0 3.1
#Month.3 1.0 81.5 151.0 942.0
#Month.6 262.0 331.0 27.0 85.6
#Month.8 56.0 14.0 21.4 10.0
#Month.10 8.7 64.4 578.0 637.0
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.