calcNewField: Simple calculation of new Fields

View source: R/calcNewField.R

calcNewFieldR Documentation

Simple calculation of new Fields

Description

Basic calculation of new Fields by operation '+', '-', '*' or '/'

Usage

calcNewField(
  x,
  y,
  op,
  x.col = NULL,
  y.col = NULL,
  quant = NULL,
  verbose = TRUE
)

Arguments

x

The first Field

y

The second Field

op

which arithmetic should be performed: addition ('+'), substraction ('-'), multiplication ('*') or division ('/').

x.col

the column of the first Field. If empty or NULL all columns are used.

y.col

the column of the second Field. If empty or NULL all columns are used.

quant

new Quantity definition to use, if NULL it will be guessed.

verbose

print some messages.

Value

A new Field.

Author(s)

Joerg Steinkamp joerg.steinkamp@senckenberg.de

Examples


 
# First define a Source and read a Field
test.dir <- system.file("extdata", "LPJ-GUESS_Runs", "CentralEurope", package = "DGVMTools")
test.Source <- defineSource(name = "Run 1", dir = test.dir,  format = GUESS)
test.Field <- getField(source = test.Source, quant = "lai", year.aggregate.method = "mean")
test.Source.2 <- defineSource(name = "Run 2", dir = test.dir,  format = GUESS)
test.Field.2 <- getField(source = test.Source.2, quant = "lai", year.aggregate.method = "mean")

# division, result should be 1 or NA everywhere since Fields are the same 
# (NAs come wherever the Field is zero)
test.division <- calcNewField(x=test.Field, y = test.Field.2, op = "/")
print(plotSpatial(test.division))

# subtraction, result should be 0 everywhere since Fields are the same
test.subtraction <- calcNewField(x=test.Field, y = test.Field.2, op = "-", verbose = FALSE)
print(plotSpatial(test.subtraction))

# addition, select Total only and compare to original
test.addition <- calcNewField(x=test.Field, y = test.Field.2,
                              x.col = "Total", y.col = "Total", op = "+")
print(plotSpatial(list(test.addition, test.Field), layers = "Total"))

# multiplication, select a couple of Layers only and compare to original
test.multiplication <- calcNewField(x=test.Field, y = test.Field.2, x.col = c("C3G", "TeBS"), 
                                    y.col = c("C3G", "TeBS"), op = "*", verbose = FALSE)
print(plotSpatial(list(test.multiplication, test.Field), layers =  c("C3G", "TeBS")))



MagicForrest/DGVMTools documentation built on Aug. 23, 2024, 8:05 a.m.