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

st_geometry null replacement: coercing to data.frame can change variable names #298

Closed
bocinsky opened this issue Apr 12, 2017 · 1 comment

Comments

@bocinsky
Copy link

bocinsky commented Apr 12, 2017

@edzer thanks for the great package! It's already simplifying and improving my R/GIS workflows.

When dropping the geometry column with st_geometry(obj) <- NULL, the brute-force coercing of the table to a data.frame using data.frame(x) (sf.R line 147) will change the names. Consider using as.data.frame(x) instead, or setting check.names = FALSE.

> test_df <- data.frame(`Some letters` = letters[1:10],
+                       x = 1:10,
+                       y = 11:20,
+                       check.names = F)
> test_df
   Some letters  x  y
1             a  1 11
2             b  2 12
3             c  3 13
4             d  4 14
5             e  5 15
6             f  6 16
7             g  7 17
8             h  8 18
9             i  9 19
10            j 10 20
> 
> test_df %<>% sf::st_as_sf(coords = c("x", "y"))
> test_df
Simple feature collection with 10 features and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: 1 ymin: 11 xmax: 10 ymax: 20
epsg (SRID):    NA
proj4string:    NA
   Some letters     geometry
1             a  POINT(1 11)
2             b  POINT(2 12)
3             c  POINT(3 13)
4             d  POINT(4 14)
5             e  POINT(5 15)
6             f  POINT(6 16)
7             g  POINT(7 17)
8             h  POINT(8 18)
9             i  POINT(9 19)
10            j POINT(10 20)
> 
> sf::st_geometry(test_df) <- NULL
> test_df
   Some.letters
1             a
2             b
3             c
4             d
5             e
6             f
7             g
8             h
9             i
10            j
> 
> # Demonstrating as.data.frame(x)
> test_df <- data.frame(`Some letters` = letters[1:10],
+                       x = 1:10,
+                       y = 11:20,
+                       check.names = F)
> data.frame(test_df)
   Some.letters  x  y
1             a  1 11
2             b  2 12
3             c  3 13
4             d  4 14
5             e  5 15
6             f  6 16
7             g  7 17
8             h  8 18
9             i  9 19
10            j 10 20
> as.data.frame(test_df)
   Some letters  x  y
1             a  1 11
2             b  2 12
3             c  3 13
4             d  4 14
5             e  5 15
6             f  6 16
7             g  7 17
8             h  8 18
9             i  9 19
10            j 10 20
@edzer edzer closed this as completed in d4e2721 Apr 12, 2017
@edzer
Copy link
Member

edzer commented Apr 12, 2017

Thanks, great suggestion!

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