Learn R Programming

OceanView (version 1.0.7)

Reshaping to a crosstable: Converts a dataset from database-format to a cross table

Description

Reshapes data arranged in 3 columns to a ``crosstable'' matrix.

Usage

db2cross (input, row = 1, col = 2, value = 3, subset = NULL,
  df.row = NA, df.col = NA, out.row = NA, out.col = NA, 
  full.out = FALSE)

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.

Arguments

input

A matrix in database format, (x,y,z) .

row

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.

Author

Karline Soetaert <karline.soetaert@nioz.nl>

Details

Uses a simple fortran function.

rows and columns are generated by the unique values in each x- and y-column.

See Also

reshape, the official (slow) R-function

remap to remap a matrix or array to higher or lower resolution

Examples

Run this code
## =======================================================================
## 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
## =======================================================================
if (FALSE) {
 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")
 }

Run the code above in your browser using DataLab