Number or name of the column in input, to be used as
rows in the result.
col
Number or name of the column in input, to be used as
columns in the result.
value
Number or name of the column in input, to be used as
values in the result.
subset
Logical expression indicating elements or rows to keep;
missing values are taken as FALSE
df.row, df.col
Maximal distance in row and column values that should
be considered the same. The default is to use each unique row or column
value in input as a row or column value in the crosstable.
Overrruled when out.row or out.col are defined.
out.row, out.col
Values of rows and columns to be used in the cross table.
The default is to use each unique row or column value in input as a
row or column value in the crosstable. Each value in input is mapped to
out.row and out.col to which it is closest.
Overrrules df.row or df.col.
full.out
If TRUE, will also output how the input values were
mapped to the output values. This is only relevant if either of df.row,
df.col, out.row or out.col is not NULL.
Details
Uses a simple fortran function.
rows and columns are generated by the unique values in each
x- and y-column.
Value
a list containing:
x
The values of the rows.
y
The values of the columns.
z
The crosstable, a matrix.
and if full.out = TRUE also
map
The mapping of the x and y values, consisting of
var.input, factor, var.output, with the original values,
how they are mapped, and the resulting values respectively.
Author(s)
Karline Soetaert <karline.soetaert@nioz.nl>
See Also
reshape, the official (slow) R-function
remap to remap a matrix or array to higher or lower resolution
Examples
## =======================================================================
## test the function on a small data set
## =======================================================================
df3 <- data.frame(school = rep(c("a","b","c"), each = 4),
class = rep(9:10, 6),
time = rep(c(1,1,2,2), 3),
score = rnorm(12))
head(df3)
db2cross(df3, val = 4)
## =======================================================================
## Defining the output rows
## =======================================================================
Samples <- data.frame(time = c(1, 1.1, 1.2, 2, 2.1, 2.2, 4, 4.1, 4.2),
var = rep(c("O2", "NO3", "NH3"), 3),
val = 1:9)
Samples
db2cross(Samples)
db2cross(Samples, df.row = 0.5)
db2cross(Samples, out.row = c(1, 2, 4))
db2cross(Samples, out.row = 1:4)
## =======================================================================
## A larger dataset; requires OceanView.Data
## =======================================================================
## Not run:
data (pp.aug2009.db)
crosstab <- db2cross(pp.aug2009.db)
crosstab$z[crosstab$z>1000] <- 1000
crosstab$z[crosstab$z<0] <- NA
image2D(z = crosstab$z, x = crosstab$x, y = crosstab$y,
main = "primary production august 2009 mgC/m2/d",
NAcol = "black")
## End(Not run)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(OceanView)
Loading required package: plot3D
Loading required package: plot3Drgl
Loading required package: rgl
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/OceanView/db2cross.Rd_%03d_medium.png", width=480, height=480)
> ### Name: Reshaping to a crosstable
> ### Title: Converts a dataset from database-format to a cross table
> ### Aliases: db2cross
> ### Keywords: utility
>
> ### ** Examples
>
> ## =======================================================================
> ## test the function on a small data set
> ## =======================================================================
>
> df3 <- data.frame(school = rep(c("a","b","c"), each = 4),
+ class = rep(9:10, 6),
+ time = rep(c(1,1,2,2), 3),
+ score = rnorm(12))
> head(df3)
school class time score
1 a 9 1 -0.5454374
2 a 10 1 -1.3987476
3 a 9 2 2.9634141
4 a 10 2 1.7738502
5 b 9 1 -1.7033760
6 b 10 1 -0.1322221
> db2cross(df3, val = 4)
$x
[1] a b c
Levels: a b c
$y
[1] 9 10
$z
9 10
a 1.208988 0.1875513
b -1.165530 0.6216704
c -0.611236 -0.6438269
>
> ## =======================================================================
> ## Defining the output rows
> ## =======================================================================
> Samples <- data.frame(time = c(1, 1.1, 1.2, 2, 2.1, 2.2, 4, 4.1, 4.2),
+ var = rep(c("O2", "NO3", "NH3"), 3),
+ val = 1:9)
> Samples
time var val
1 1.0 O2 1
2 1.1 NO3 2
3 1.2 NH3 3
4 2.0 O2 4
5 2.1 NO3 5
6 2.2 NH3 6
7 4.0 O2 7
8 4.1 NO3 8
9 4.2 NH3 9
>
> db2cross(Samples)
$x
[1] 1.0 1.1 1.2 2.0 2.1 2.2 4.0 4.1 4.2
$y
[1] NH3 NO3 O2
Levels: NH3 NO3 O2
$z
NH3 NO3 O2
1 NA NA 1
1.1 NA 2 NA
1.2 3 NA NA
2 NA NA 4
2.1 NA 5 NA
2.2 6 NA NA
4 NA NA 7
4.1 NA 8 NA
4.2 9 NA NA
> db2cross(Samples, df.row = 0.5)
$x
[1] 1.1 2.1 4.1
$y
[1] NH3 NO3 O2
Levels: NH3 NO3 O2
$z
NH3 NO3 O2
1.1 3 2 1
2.1 6 5 4
4.1 9 8 7
> db2cross(Samples, out.row = c(1, 2, 4))
$x
[1] 1 2 4
$y
[1] NH3 NO3 O2
Levels: NH3 NO3 O2
$z
NH3 NO3 O2
1 3 2 1
2 6 5 4
4 9 8 7
> db2cross(Samples, out.row = 1:4)
$x
[1] 1 2 3 4
$y
[1] NH3 NO3 O2
Levels: NH3 NO3 O2
$z
NH3 NO3 O2
1 3 2 1
2 6 5 4
3 NA NA NA
4 9 8 7
>
> ## =======================================================================
> ## A larger dataset; requires OceanView.Data
> ## =======================================================================
> ## Not run:
> ##D data (pp.aug2009.db)
> ##D crosstab <- db2cross(pp.aug2009.db)
> ##D crosstab$z[crosstab$z>1000] <- 1000
> ##D crosstab$z[crosstab$z<0] <- NA
> ##D
> ##D image2D(z = crosstab$z, x = crosstab$x, y = crosstab$y,
> ##D main = "primary production august 2009 mgC/m2/d",
> ##D NAcol = "black")
> ##D
> ## End(Not run)
>
>
>
>
>
> dev.off()
null device
1
>