对于某些对象,属性标识了一个特殊的列,例如在sf
对象中的几何列。为了在dplyr
中进行一些计算,很好能够轻松地确定这些列。我正在寻找一种创建帮助识别此列的函数的方法。在下面的示例中,我可以创建一个标识此列的函数,但仍需要使用rlang
扩展运算符(!!!
)。
require(sf)
require(dplyr)
n<-4
df = st_as_sf(data.frame(x = 1:n, y = 1:n, cat=gl(2,2)), coords = 1:2, crs = 3857) %>% group_by(cat)
# this is the example I start from however the geometry column is not guaranteed to have that name
df %>% mutate(d=st_distance(geometry, geometry[row_number()==1]))
#> Simple feature collection with 4 features and 2 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: 1 ymin: 1 xmax: 4 ymax: 4
#> Projected CRS: WGS 84 / Pseudo-Mercator
#> # A tibble: 4 × 3
#> # Groups: cat [2]
#> cat geometry d[,1]
#> * <fct> <POINT [m]> [m]
#> 1 1 (1 1) 0
#> 2 1 (2 2) 1.41
#> 3 2 (3 3) 0
#> 4 2 (4 4) 1.41
# this works, however the code does not get easier to read
df %>% mutate(d=st_distance(!!!syms(attr(., "sf_column")), (!!!syms(attr(., "sf_column")))[row_number()==1]))
#> Simple feature collection with 4 features and 2 fields
#> ...
#> 4 2 (4 4) 1.41
# this works and is already better:
geometry_name<-function(x) syms(attr(x, 'sf_column'))
df %>% mutate(d=st_distance(!!!geometry_name(.), (!!!geometry_name(.))[row_number()==1]))
#> Simple feature collection with 4 features and 2 fields
#> ...
#> 4 2 (4 4) 1.41
理想情况下,我希望找到一个函数,使得下面的代码能够工作,这样对用户来说是最简单的:
df %>% mutate(d=st_distance(geometry_name(), geometry_name()[row_number()==1]))
magrittr
管道 (%>%
),而不能使用默认管道 (|>
)。我尝试使用基本 R 管道的_
占位符来使其工作,但由于其功能完全不同且仅是占位符,我不确定是否可能实现。谢谢! - Bart