CopyAttributes: Copy attributes from one object to another

View source: R/properties.R

CopyAttributesR Documentation

Copy attributes from one object to another

Description

Copies attributes such as "label", "name", "question" and "questiontype" from one object to another. If both objects are lists (data frames), elements (columns) in the recipient list (data frame) will also receive attributes from elements (columns) with the same name in the donor list (data frame). The function is recursive, and will copy attributes when the inputs are nested lists.

Usage

CopyAttributes(
  data.without.attributes,
  data.with.attributes,
  attr.to.not.copy = c("dimnames", "names", "row.names", "dim", "class", "levels")
)

Arguments

data.without.attributes

an object to receive attributes from, such as a data.frame, list, or matrix

data.with.attributes

an object to copy attributes from

attr.to.not.copy

character vector of attribute names appearing in data.with.attributes that should not be copied

Details

In the case when both arguments are data frames, any attributes in the columns of data.with.attributes will also be copied to data.without.attributes excluding class and levels Names are used when copying attributes in each component. Nothing will be copied for the case of lists with NULL names attribute.

In the case when data.without.attributes is not a data frame and data.with.attributes is a data frame, the attributes of data.with.attributes are copied over to data.without.attributes and the columns are not changed.

Similarly, if data.without.attributes is a data frame and and data.with.attributes is not a data frame the attributes of data.with.attributes are copied over to data.without.attributes and the columns are also not changed.

Value

A copy of data.without.attributes with all the attributes of data.with.attributes.

Examples

v1 <- 1:10
v2 <- 11:20
attr(v1, "label") <- "label for v1"
CopyAttributes(v2, v1) # returns v2 with the label attribute from v1

df1 <- data.frame(a = 1:10, b = 11:20)
df2 <- data.frame(a = 21:30, b = 31:40)
attr(df1, "label") <- "label for df1"
attr(df1$a, "label") <- "label for column a of df1"
attr(df1$b, "label") <- "label for column b of df1"
CopyAttributes(df2, df1) # returns df2 with label attributes (including column attributes) from df1

CopyAttributes(df2, v1) # returns df2 with label attribute from v1 (columns not changed)
CopyAttributes(v2, df1) # returns v2 with label attribute from df1 (columns not changed)

NumbersInternational/flipU documentation built on March 2, 2024, 10:55 a.m.