let NLset() coerce a numeric worldArray layer to character - #61
Merged
Conversation
Assigning character values to only some patches of a numeric layer used
to be an error, on the grounds that the numbers left in the unassigned
patches would be reread as codes. Follow base R instead, where assigning
a string into a numeric vector turns the whole vector into character:
w <- NLset(world = w, agents = cbind(pxcor = 0, pycor = 0),
var = "num", val = "oops")
of(world = w, agents = cbind(pxcor = 1, pycor = 1), var = "num")
#> "17"
The values already in the layer become categories in their own right
before the assignment is applied, so none of them are lost and none are
reinterpreted. Values that merely arrive as strings but are numbers, as
they do out of cbind(a = 1, b = "x"), are still left numeric.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Relaxes the restriction added in #59, as requested.
Before
Assigning character values to only some patches of a numeric
worldArraylayer was an error:The reasoning was that the numbers left in the unassigned patches would be reread as codes and silently corrupted.
After
It follows base R instead, where assigning a string into a numeric vector coerces the whole vector:
So the layer becomes a character one, and the patches that were not assigned keep their values as characters:
Simply deleting the guard would have caused exactly the corruption it was there to prevent. The values already in the layer are turned into categories in their own right before the assignment is applied, so all of them survive and none is reinterpreted. Verified across a whole layer: every one of the 25 original values is present afterwards, alongside the new one.
Unchanged:
cbind(num = 1, hab = "ice")still works and does not turnnuminto categoriesVerification
R CMD check --as-cran: 0 errors, 0 warnings, 0 notesdevtools::test(): 0 failures, 1231 passingplot()on a coerced world still emits no warnings🤖 Generated with Claude Code