Skip to content

let NLset() coerce a numeric worldArray layer to character - #61

Merged
achubaty merged 1 commit into
developmentfrom
relax-character-assignment
Aug 11, 2026
Merged

let NLset() coerce a numeric worldArray layer to character#61
achubaty merged 1 commit into
developmentfrom
relax-character-assignment

Conversation

@achubaty

Copy link
Copy Markdown
Collaborator

Relaxes the restriction added in #59, as requested.

Before

Assigning character values to only some patches of a numeric worldArray layer was an error:

NLset(world = w, agents = cbind(pxcor = 0, pycor = 0), var = "num", val = "oops")
#> Error: cannot assign character values to only some patches of the numeric worldArray layer 'num'.

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:

x <- 1:5; x[1] <- "a"; x
#> "a" "2" "3" "4" "5"

So the layer becomes a character one, and the patches that were not assigned keep their values as characters:

w <- NLset(world = w, agents = cbind(pxcor = 0, pycor = 0), var = "num", val = "oops")
of(world = w, agents = cbind(pxcor = 0, pycor = 0), var = "num")   #> "oops"
of(world = w, agents = cbind(pxcor = 1, pycor = 1), var = "num")   #> "17"

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:

Verification

  • R CMD check --as-cran: 0 errors, 0 warnings, 0 notes
  • devtools::test(): 0 failures, 1231 passing
  • plot() on a coerced world still emits no warnings

🤖 Generated with Claude Code

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>
@achubaty
achubaty merged commit 70afdd5 into development Aug 11, 2026
14 checks passed
@achubaty
achubaty deleted the relax-character-assignment branch August 11, 2026 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant