copyatts: Copy all attributes from one variable to another

Description Usage Arguments Details Value Examples

View source: R/copyatts.R

Description

This function sets the attributes of the argument to to match the attributes of from. The special attributes dim, dimnames, names, and row.names are skipped to preserve dimensionality. Existing attributes with the same name are overwritten by default.

Usage

1
copyatts(from, to, clobber = TRUE)

Arguments

from

The variable to copy attributes from.

to

The variable whose attribute are to be set.

clobber

Logical: if true (default), from@att will overwrite to@att. If false, attributes on to take priority.

Details

Attribute ordering is mostly preserved. After copying, special attributes will come first, in alphabetical order. Attributes of the variable with priority (default: the variable being copied from) come next, in their original order. Any additional attributes (default: the variable being copied to) come last, again in their original order.

This function exists because you can't simply assign attributes(x) <- attributes(y) if x and y are arrays with different dimensions. You can avoid this problem by using mostattributes(x) <- attributes(y) instead, but that removes any dimensions on x, flattening it to a vector. Like mostattributes<-, it is principally intended for arrays, and should be used with care on classed objects.

Value

A copy of to with the attributes of from.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
x <- array(data=rnorm(120), dim=c(5,2,12))
x@dimnames <- list(lon=c(-92:-88), lat=c(45,46), time=month.abb)
x@name  <- "example"
x@units <- "none"
str(x)

y <- x[1:3,,1]
y@name <- "January subset"
y@month <- "Jan"
str(y)

z <- copyatts(x, y)
str(z)

w <- copyatts(x, y, clobber=FALSE)
str(w)

sethmcg/climod documentation built on Nov. 19, 2021, 11:12 p.m.