store worldArray character layers as levels - #59
Merged
Conversation
achubaty
force-pushed
the
issue-49-worldarray-levels
branch
from
August 10, 2026 18:22
27766ba to
d5533ab
Compare
An array holds a single type, so stacking a character worldMatrix next to a numeric one turned every layer into character, and the character values were then coerced back to NA on the way out (#49): w3 <- stackWorlds(w1, w2) # w1 numeric, w2 character of(world = w3, agents = cbind(pxcor = 1, pycor = 1), var = "w2") #> NA #> Warning: NAs introduced by coercion worldArray now keeps character layers as integer codes and records their categories in a new levels slot, the same way agentMatrix already stores its character columns, so the array itself stays numeric. Accessors report the characters rather than the codes. of() decodes, and returns a data.frame when a mix of coded and numeric layers is asked for, since a matrix cannot hold both. NLwith() compares against the characters and NLset() encodes them, extending the levels when it meets a category the layer has not seen. [[ and $ hand back a character worldMatrix, and [[<- re-encodes so that swapping layers in and out cannot leave the array or the levels inconsistent. world2spatRast() passes the characters to terra, which builds a categorical layer from them, which is what plot() on a standalone character worldMatrix has always produced. Numeric layers stacked beside a character one now keep their own type: of() on one used to report "17" and now reports 17 again. Turning only some patches of a numeric layer into characters is rejected, because the numbers left in the unassigned patches would silently become codes. Assigning the whole layer is fine, as is world[["x"]] <- charWorld. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
achubaty
force-pushed
the
issue-49-worldarray-levels
branch
from
August 10, 2026 18:32
d5533ab to
ebdb079
Compare
This was referenced Aug 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #49.
The problem
An
arrayholds a single type. Stacking a characterworldMatrixnext to a numeric one therefore turned every layer into character, and the character values were then coerced back toNAon the way out:The numeric layer was collateral damage too —
of(w3, var = "w1")returned the string"17"rather than17.The fix
worldArraygains alevelsslot. Character layers are stored as integer codes with their categories recorded there, exactly asagentMatrixalready does for its character columns, so the array itself stays numeric.Accessors report the characters, never the codes:
of(w3, var = "w2")NA+ warning"sea"of(w3, var = "w1")"17"(character)17(numeric)of(w3, var = c("w1","w2"))data.frameplot(w3)NAw3[["w2"]]worldMatrixworldMatrixNLwith()compares against the characters,NLset()encodes them and extends the levels when it meets a new category, and[[<-re-encodes so swapping layers cannot leave the array and the levels inconsistent.world2spatRast()hands terra the characters and lets terra build the categorical layer — which is exactly what a standalone characterworldMatrixhas always produced, so the two paths now agree.An all-numeric
worldArrayis untouched by any of this: emptylevels, matrix returns, same types.One deliberate restriction
Turning some patches of a numeric layer into characters is rejected:
The numbers left in the unassigned patches would silently be reinterpreted as codes. Assigning the whole layer is allowed, as is
world[["num"]] <- aCharacterWorld.Note that
cbind(num = 1, hab = "ice")is already a character matrix beforeNLset()ever sees it, so numbers arrive at a numeric layer as strings. Those are converted back when they parse cleanly, rather than being treated as new categories. Pass adata.frameto keep the column types distinct.Rebased onto #57 and #58
Both prerequisites are merged, and this branch has been rebased onto them. Conflicts resolved:
R/world-functions.R— kept drop the quickPlot dependency #57'sncols=/nrows=fix and this PR's decodedvals, in the sameterra::rast()call.NEWS.mdandtests/testthat/test-NetLogoR-classes.R— both PRs' entries and test blocks retained.With #57 in the base,
plot(w3)is now completely silent: itsnrow/ncolpartial-match warnings are gone and theNAs introduced by coercionwarning this PR removes is gone, so the two fixes compose as intended.Verification
R CMD check --as-cran: 0 errors, 0 warnings, 0 notesdevtools::test(): 0 failures, 1225 passing (rebased onto drop the quickPlot dependency #57, which replaced the quickPlot plotting tests)of()returns"sea", andplot(w3)emits no warnings at all🤖 Generated with Claude Code