Skip to content

Commit

Permalink
turtlesOwn() when tVal = NA consider NA as numerical and not character
Browse files Browse the repository at this point in the history
NA will be considered as character (and the whole column as character) when "NA" is given.
Otherwise, missing data or NA will be considered as numerical
  • Loading branch information
SarahBauduin committed Apr 22, 2024
1 parent 6477da9 commit c5f17a4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/turtle-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2711,6 +2711,8 @@ setMethod(
#' @param tVal Vector representing the values of `tVar`.
#' Must be of length 1 or of length `turtles`.
#' If missing, `NA` is given.
#' If missing or if `NA` is given, the column will be `numeric`.
#' To be a `character` column, `"NA"` must be given.
#'
#' @return `AgentMatrix` representing the `turtles` with the new
#' variable `tVar` added.
Expand Down Expand Up @@ -2755,7 +2757,7 @@ setMethod(
"turtlesOwn",
signature = c("agentMatrix", "character", "ANY"),
definition = function(turtles, tVar, tVal) {
if (inherits(tVal, "numeric") | inherits(tVal, "integer")) {
if (inherits(tVal, "numeric") | inherits(tVal, "integer") | inherits(tVal, "logical")) {
turtles@.Data <- cbind(turtles@.Data, newCol = tVal)
colnames(turtles@.Data)[ncol(turtles@.Data)] <- tVar
} else {
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-turtle-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,9 @@ test_that("turtlesOwn works", {
))
expect_identical(t5@.Data, cbind(t1@.Data, female = c(2, 2, 1, 1, 1)))
expect_equivalent(t5@levels$female, c("FALSE", "TRUE"))
t6 <- turtlesOwn(turtles = t1, tVar = "numericalValue", tVal = NA)
t6 <- NLset(turtles = t6, agents = NLwith(agents = t6, var = "who", val = 3), var = "numericalValue", val = 6)
expect_true(class(of(agents = t6, var = "numericalValue")) == "numeric")
})

test_that("subHeadings works", {
Expand Down

0 comments on commit c5f17a4

Please sign in to comment.