Converts a dataset from database-format to a cross table

Share:

Description

Reshapes data arranged in 3 columns to a “crosstable” matrix.

Usage

1
2
3
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) 

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.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
## =======================================================================
## 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)