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

levels() needs an update #366

Open
PennMGIS opened this issue May 16, 2022 · 0 comments
Open

levels() needs an update #366

PennMGIS opened this issue May 16, 2022 · 0 comments

Comments

@PennMGIS
Copy link

PennMGIS commented May 16, 2022

In lesson: Introduction to Geospatial Raster and Vector Data with R
Episode7: Explore and Plot by Vector Layer Attributes
https://datacarpentry.org/r-raster-vector-geospatial/07-vector-shapefile-attributes-in-r/index.html

levels(lines_HARV$TYPE) should return the unique values within TYPE column containing categorical variables. However, in the exercise this actually returns NULL.

I did a little investigation and for this to work, when reading the spatial data you need to include "stringsAsFactors = T" parameter in str_read.

lines_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp", stringsAsFactors = T)

Or even better, if st_read is not flagged with stringAsFactors =T, use...
levels(as.factor(lines_HARV$TYPE))

The reason for this is due to a change of R code in the way it handles string values & factors. A good explanation for this can be found at:
https://simplystatistics.org/posts/2015-07-24-stringsasfactors-an-unauthorized-biography/

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

1 participant