The starting point of any Formal Concept Analysis (FCA) workflow is the Formal Context. A formal context is a triple \(K = (G, M, I)\), where \(G\) is a set of objects, \(M\) is a set of attributes, and \(I \subseteq G \times M\) is a binary relation between them.
In fcaR, formal contexts are managed by the
FormalContext R6 class. This vignette demonstrates the
multiple ways to create or import a FormalContext
object.
The most direct way to create a formal context is by passing a binary matrix (0/1 or TRUE/FALSE) or a data frame to the constructor.
You can manually define a matrix where rows represent objects and columns represent attributes.
# Create a binary matrix
M <- matrix(c(1, 0, 1,
1, 1, 0,
0, 1, 1),
nrow = 3,
byrow = TRUE)
# Assign row and column names (Objects and Attributes)
rownames(M) <- c("Object1", "Object2", "Object3")
colnames(M) <- c("Attribute1", "Attribute2", "Attribute3")
# Create the FormalContext object
fc <- FormalContext$new(M)
# Print the context
fc
#> FormalContext with 3 objects and 3 attributes.
#> Attribute1 Attribute2 Attribute3
#> Object1 X X
#> Object2 X X
#> Object3 X XYou can also use a data.frame. fcaR will
attempt to coerce it into a binary matrix. This is useful if your data
is already loaded in R from another source.
df <- data.frame(
has_wings = c(TRUE, TRUE, FALSE),
can_fly = c(TRUE, FALSE, FALSE),
has_legs = c(TRUE, TRUE, TRUE),
row.names = c("Eagle", "Penguin", "Dog")
)
fc_df <- FormalContext$new(df)
fc_df
#> FormalContext with 3 objects and 3 attributes.
#> has_wings can_fly has_legs
#> Eagle X X X
#> Penguin X X
#> Dog XfcaR supports several standard file formats used in the
FCA community. The FormalContext$new() constructor
automatically detects the format based on the file extension.
.cxt)The Burmeister format (.cxt) is the standard for many
classic FCA tools like ConExp.
# We use an example file included in the package
cxt_file <- system.file("contexts", "lives_in_water.cxt", package = "fcaR")
# Load the context
fc_cxt <- FormalContext$new(cxt_file)
fc_cxt
#> FormalContext with 8 objects and 9 attributes.
#> needs water to live lives in water lives on land needs chlorophyll
#> fish leech X X
#> bream X X
#> frog X X X
#> dog X X
#> water weeds X X X
#> reed X X X X
#> bean X X X
#> corn X X X
#> Other attributes are: dicotyledon, monocotyledon, can move, has limbs, breast
#> feeds.csv)You can load a context from a Comma-Separated Values file. It assumes the first column contains object names and the header contains attribute names.
fcaR includes a powerful feature to download and load
curated datasets directly from the online FCA
Repository (fcarepository.org).
You don’t need to know the filenames by heart. You can browse the available contexts and see their metadata (dimensions, description, source) directly from the console.
Once you have identified a context of interest (e.g.,
"planets_en.cxt" or "animals_en.cxt"), you can
download it using fetch_context(). This function handles
the download, parsing, and error checking for you.
For a more visual and user-friendly experience, fcaR
includes an RStudio Addin. This tool provides a
Graphical User Interface (GUI) to explore the repository without writing
code initially.
You have two options:
fetch_context).This is the recommended way to discover new datasets and start working quickly.
| Source | Function / Method | Description |
|---|---|---|
| R Matrix / Data Frame | FormalContext$new(x) |
Direct creation from in-memory objects. |
| Local File (.cxt) | FormalContext$new("file.cxt") |
Loads Burmeister format files. |
| Local File (.csv) | FormalContext$new("file.csv") |
Loads CSV files (expects binary data). |
| FCA Repository (Code) | fetch_context("name.cxt") |
Downloads and loads from the web. |
| FCA Repository (GUI) | select_repository_context_addin() |
Graphical interface to search and load. |