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

Feature request: for SpatVector, return NULL for non-existent column #1118

Closed
dfriend21 opened this issue Apr 17, 2023 · 1 comment
Closed

Comments

@dfriend21
Copy link
Contributor

If you try to access a non-existent column on a SpatVector using $, an error is thrown. This differs from the default behavior of a data.frame, where NULL is returned. I've noticed this because I typically use is.null(df$column) to check if a column exists on a data frame, and therefore tried to use the same approach for a SpatVector. Personally, I think it'd be nice for the sake of consistency for a SpatVector to behave as a data.frame does in this case. However, there may be reasons to throw an error in this case rather than returning NULL - feel free to close this issue if this isn't how you want a SpatVector to behave.

library(terra)
#> terra 1.7.22

v <- vect(system.file("ex/lux.shp", package = "terra"))
v$not_a_column
#> Error: [$] not_a_column is not a variable name in x

v_df <- as.data.frame(v)
v_df$not_a_column
#> NULL

Created on 2023-04-17 with reprex v2.0.2

@rhijmans
Copy link
Member

rhijmans commented Apr 17, 2023

it'd be nice for the sake of consistency for a SpatVector to behave as a data.frame

Of course, that is the intent. But it is easy to overlook details like this. Thanks for pointing that out.

I now get:

library(terra)
#terra 1.7.25
vect(cbind(0,0))$id
#NULL

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