-
Notifications
You must be signed in to change notification settings - Fork 298
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
cbind of sf and df #92
Comments
Please try a pull request, it is hard for me to see what you suggest to change otherwise. |
It turns out the problem is not from sf rather the way cbind dispatch on sf <- st_read(system.file("shape/nc.shp", package="sf"), "nc", crs = 4267)
df <- data.frame( field1=1:nrow(sf) , field2=1:nrow(sf))
weird_obj <- cbind(sf,df)
new_sf <- cbind.sf(sf,df) #works
|
Since it doesn't have named arguments before ..., it can't use standard method dispatch. |
any workaround? |
call |
dplyr has |
|
I don't think additional aliases are necessary. Using old known interfaces such as |
|
Still not working. |
But For |
for |
|
make it work also with
The following change is needed: st_bind_cols <- function (...)
{
arg1 <- list(...)[[1]]
if (inherits(arg1,"sf"))
cbind.sf(...)
else if(inherits(arg1,"sfc"))
cbind.sfc(...)
}
cbind.sfc <- function(...){
sf::st_sf(...)
}
st_bind_cols( st_sfc(st_point(c(1,1))) , a= 1, data.frame(b=2) ) |
The better approach might be to add as.data.frame.sfc = function(x, ...) {
ret = data.frame(row.names = seq_along(x))
ret$geometry = x
ret
} |
sf <- st_read(system.file("shape/nc.shp", package="sf"), "nc", crs = 4267)
df <- data.frame(field1 = 1:nrow(sf) , field2 = 1:nrow(sf))
new_sf <- dplyr::bind_cols(sf, df)
names(new_sf)
[1] "AREA" "PERIMETER" "CNTY_" "CNTY_ID" "NAME" "FIPS" "FIPSNO" "CRESS_ID" "BIR74" "SID74" "NWBIR74" "BIR79" "SID79"
[14] "NWBIR79" "geometry" "field1" "field2" |
Why does the order of columns matter? I would expect |
It matters for example when viewing |
sounds like a GUI issue rather than an R issue. Sinds |
Hi all,
cbind of a sf and a data.frame creates a weird matrix object.
dd <- cbind(sf,df)
but if we do it this way it works:
as
cbind.sf
does the above procedure internally, I think with a minor change,cbind.sf
could become more flexible.The text was updated successfully, but these errors were encountered: