add_dimnames | R Documentation |
add_dimnames
adds (or re-assigns) dimension names to arrays.
add_dimnames(
x,
dnames = c("row", "col", "tab"),
prefix = c("r", "c", "t"),
sep = ""
)
x |
An array to which dimension names are to be added. |
dnames |
A character vector of dimension names.
If |
prefix |
A character vector of prefixes (whose
length must match the number of dimensions of |
sep |
A separator (as character) between prefix and numeric indices.
Default: |
Although add_dimnames
is intended for arrays,
it also works for atomic vectors (yielding a named vector)
and data frames (yielding new row and column names)
but not other lists.
As R objects of class matrix
, table
,
and xtabs
are also arrays add_dimnames
works for them as well.
See provideDimnames
(from base R)
for a related function.
Input x
with dimension names.
provideDimnames
is a more basic version.
Other array functions:
ctable()
,
expand_freq_table()
,
flatten_array()
,
subtable()
a <- array(1:24, dim = c(3, 4, 2))
add_dimnames(a)
add_dimnames(a, dnames = "dim", sep = "_")
add_dimnames(a, dnames = c("X", "Y", "Layer", "XYZ"), prefix = c("x", "y", "l"), sep = "_")
# More dimensions:
b <- array(1:2^5, dim = rep(2, 5))
add_dimnames(b)
add_dimnames(b, dnames = "D")
add_dimnames(b, dnames = "dim", prefix = c("r", "c", "l", "m", "n"), sep = "_")
# For a table:
add_dimnames(UCBAdmissions)
# For data frames:
c <- data.frame(let = letters[1:4], num = 5:8)
add_dimnames(c, prefix = c("r", "c"))
add_dimnames(c)
# For atomic vectors:
d <- 1:4
add_dimnames(d, prefix = "nr") # adding named elements
add_dimnames(d, prefix = letters[1:4], sep = "_")
# For scalars:
add_dimnames(NA)
# For lists (that are NOT data frames):
l <- list(1:2, letters[1:3])
add_dimnames(l) # return NA
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.