as.px {pxR} | R Documentation |
Convert an array or a data.frame into an object of class px
Description
This function converts an array object into a px
object. It uses the
dimnames
of the array to fill the VALUES
list of the
px
object. It can also converts an data.frame object into a
px
object. Optionally it uses an object of type px
as the
skeleton of the new object.
Usage
as.px(x,... )
## S3 method for class 'array'
as.px(x, skeleton.px = NULL, list.keys = NULL, ...)
## S3 method for class 'data.frame'
as.px(x, skeleton.px = NULL,
list.keys = NULL, value.column = NULL, ...)
Arguments
x |
An array or |
skeleton.px |
A |
list.keys |
A optional list of metadata pairs |
value.column |
Optional name to numeric value column in data.frame; defaults to "value" |
... |
Other arguments that can be passed to the function |
Details
A px
object is the internal representation in R of a PC-Axis
file. As such, it contains both data and metadata.
The data to populate the px
object comes from x
, the
first argument of the function. If x
is an array, it needs
to have 'named dimnames
', i.e., its dimnames
attribute
needs to be a list with named entries.
x
can also be a molten data.frame
, i.e., each row contains a single
value and the remaining columns indicate the levels of the variables it refers to.
The ts names
will be used in STUB and HEDING keyword of px objects. The first
column of x
will be used as 'HEADING', and the rest, in reverse, order as 'STUB'.
There are two compatible methods to provide metadata information. The
first one is via skeleton.px
, a px
object whose metadata
(title, etc.) is to be recycled in the new object. In addition to
that, list.keys
can be used to pass metadata in key-value pairs
as shown in the examples below.
Metadata provided in list.keys
has priority over metadata
coming from skeleton.px
. Also, note that the metadata items
used to name the variables and their labels are neither extracted from
skeleton.px
nor px
but from the dimnames
attribute
of x
.
Note that the keywords 'HEADING', 'VALUES' and 'DATA', if present in skeleton.px
or
list.keys
, are ignored. Inconsistent 'CODES' in skeleton.px
will also be
ignored.
Few checks are made to guarantee that metadata keys conform to the PC-Axis standard.
Value
a px
object
Author(s)
Francisco J. Viciana, Carlos J. Gil Bellosta,
See Also
Examples
my.px.object <- read.px(system.file("extdata", "example.px", package = "pxR"))
my.data <- as.array(my.px.object)
my.px.object2 <- as.px(my.data)
my.px.object3 <- as.px(my.data, skeleton.px = my.px.object)
my.px.object4 <- as.px(my.data, list.keys = list(MATRIX = "xxx", CONTENTS = "new data",
NEWKEY = "another key", UNITS = "people", TITLE = "My Title"))
my.px.df <- as.data.frame(my.px.object)
my.px.object5 <- as.px(my.px.df)