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

subset(SpatVector, NSE = TRUE) fails when the logical expression includes an object x #1600

Closed
AramburuMerlos opened this issue Sep 9, 2024 · 2 comments

Comments

@AramburuMerlos
Copy link
Contributor

Maybe I should just avoid using the same column names or the NSE in these cases, but:

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

subset(v, NAME_1 == "Diekirch", NSE=TRUE) |> nrow()
# 5 

x <- data.frame(NAME_1 = "Diekirch")
subset(v, NAME_1 == x$NAME_1, NSE=TRUE) |> nrow()
# 12
subset(v, NAME_1 %in% x$NAME_1, NSE=TRUE) |> nrow()
# 12

# It doesn't happen with other classes
xl <- list(NAME_1 = "Diekirch")
subset(v, NAME_1 %in% xl$NAME_1, NSE=TRUE) |> nrow()
# 5
xv <- subset(v, NAME_1 == "Diekirch", NSE=TRUE)
subset(v, NAME_1 %in%  xv$NAME_1, NSE=TRUE) |> nrow()
# 5
@rhijmans
Copy link
Member

rhijmans commented Sep 10, 2024

The underlying problem is that you use the variable name x. That leads to a mix up because the first argument of subset is also x. With NSE you then effectively get

subset(x, x$NAME_1 == x$NAME_1)

While it works as expected with another variable name, like this

y <- data.frame(NAME_1 = "Diekirch")
subset(v, NAME_1 == y$NAME_1, NSE=TRUE) |> nrow() 
[1] 5

@AramburuMerlos AramburuMerlos changed the title subset(SpatVector, NSE = TRUE) fails when the logical expression includes a data.frame with same column name as in SpatVector subset(SpatVector, NSE = TRUE) fails when the logical expression includes an object x Sep 10, 2024
@rhijmans
Copy link
Member

rhijmans commented Oct 3, 2024

This now works for your examples; so I am hopeful it does now newly break other cases. Thank you for reporting this.

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