bhylu.com

Advertisements

R Arrays

Name the axes of a higher-dimensional measurement block.

How it works

An R array adds dimensions to an atomic vector. A three-dimensional array can represent desk, period and day, but those meanings come from your data design, not from array itself. dimnames make selections easier to read. As with matrices, dropped dimensions can surprise a function that expects a stable shape.

Run this R example

These examples run locally with R, not PHP, Python or JavaScript. The reviewed baseline is R 4.5.2 on Windows x64/UCRT, using only packages shipped with R. This is a tested version, not a newest-release claim. Install your own R from the official R project; an IDE is optional. Save the complete source as arrays.R in a separate learning folder and run Rscript --vanilla arrays.R. Keep code and fictional fixtures separate from a real project.

Bhylu has no R browser runner. There is no Try in playground link because the existing Python and JavaScript editors do not execute R. No package installation, account, network dataset, Apache, MySQL or PHP setting is needed to run these base-R sources.

The complete download uses a local scope, restores its temporary options and treats unexpected warnings as errors. Examples that handle an expected warning do so narrowly. Random examples change only their R process’s RNG state. File examples own their temporary output; figure examples leave their named PNG in the current learning folder and refuse to overwrite it. Use a new empty folder to rerun a figure.

Download the complete arrays.R · Download all source files with hash checks

For the optional collection downloader, save download-examples.mjs in an empty folder and run node download-examples.mjs. Local preview before publishing uses node download-examples.mjs http://127.0.0.1:8000/examples/r/. It refuses an existing bhylu-r-examples folder and does not install or execute R.

Example

arrays.R

local({
  previous_options <- options(warn = 2, digits = 8, stringsAsFactors = FALSE)
  on.exit(options(previous_options), add = TRUE)
  counts <- array(1:8, dim = c(2, 2, 2),
    dimnames = list(desk = c("A", "B"), period = c("AM", "PM"), day = c("Mon", "Tue")))
  cat("shape:", paste(dim(counts), collapse = "x"), "\n")
  cat("A PM Tue:", counts["A", "PM", "Tue"], "\n")
  cat("AM shape:", paste(dim(counts[, "AM", , drop = FALSE]), collapse = "x"), "\n")
  invisible(NULL)
})

Focus on the lesson operations

This excerpt omits the complete script’s option/resource setup. Use the full download to execute it.

Operation excerpt

counts <- array(1:8, dim = c(2, 2, 2),
  dimnames = list(desk = c("A", "B"), period = c("AM", "PM"), day = c("Mon", "Tue")))
cat("shape:", paste(dim(counts), collapse = "x"), "\n")
cat("A PM Tue:", counts["A", "PM", "Tue"], "\n")
cat("AM shape:", paste(dim(counts[, "AM", , drop = FALSE]), collapse = "x"), "\n")

Expected result

shape: 2x2x2
A PM Tue: 7
AM shape: 2x1x2

The example assigns a distinct meaning to each axis. The selected AM block keeps a one-element period dimension. These are synthetic values for indexing practice, not observations collected from real desks.

For API behaviour, consult the official Introduction to R and your installed help pages. These small examples do not establish real-world statistical conclusions or production performance.

Advertisements