Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spatVector values can't be kept as ordered factor #1277

Closed
bennotkin opened this issue Sep 13, 2023 · 1 comment
Closed

spatVector values can't be kept as ordered factor #1277

bennotkin opened this issue Sep 13, 2023 · 1 comment

Comments

@bennotkin
Copy link

bennotkin commented Sep 13, 2023

I can convert the values of spatVector to ordered factor class, but I can't assign this to the spatVector. Is this as intended?

First, the values can be ordered:

library(terra)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)

ordered(v$NAME_1, levels = c("Diekirch", "Grevenmacher", "Luxembourg")) |> is.ordered()
# [1] TRUE
ordered(v$NAME_1, levels = c("Diekirch", "Grevenmacher", "Luxembourg")) |> class()
# [1] "ordered" "factor"

However, if I reassign these values to the spatVector, they are do not persist as ordered:

v$NAME_1 <- ordered(v$NAME_1, levels = c("Diekirch", "Grevenmacher", "Luxembourg"))
is.ordered(v$NAME_1)
# [1] FALSE
class(v$NAME_1)
[1] "factor"

This is also true using tidyterra::mutate

v <- tidyterra::mutate(v, NAME_1 = ordered(NAME_1, levels = c("Diekirch", "Grevenmacher", "Luxembourg")))
is.ordered(v$NAME_1)
# [1] FALSE
class(v$NAME_1)
[1] "factor"

Using terra_1.7-39.

@rhijmans
Copy link
Member

rhijmans commented Sep 15, 2023

That was not intended, but since terra stores these in its own C++ object it needs to be explicitly added. I have now done that and I now see:

library(terra)
#terra 1.7.48
v <- vect(system.file("ex/lux.shp", package="terra"))
v$test <- ordered(v$NAME_1, levels = c("Diekirch", "Grevenmacher", "Luxembourg"))
is.ordered(v$NAME_1)
#[1] FALSE
is.ordered(v$test)
#[1] TRUE

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

No branches or pull requests

2 participants