在 R 中,我尝试使用 sf 包中的 st_point() 创建点。
我的输入是一个 data.frame,其中一列是 x 坐标,另一列是 y 坐标:
# Code to generate input
library(sf)
N <- 10
df <- data.frame(x=rnorm(N),y=rnorm(N))
Run Code Online (Sandbox Code Playgroud)
我想做的只是
# Code to generate examplar output
L <- list()
for (i in 1:N)
{
L[[i]] <- st_point(c(df$x[i],df$y[i]))
}
st_sfc(L)
Run Code Online (Sandbox Code Playgroud)
但我尝试用 mapply(.) 来代替循环
mapply(function(x,y) sum(c(x,y)),df$x,df$y)
mapply(function(x,y) st_point(c(x,y)),df$x,df$y)
Run Code Online (Sandbox Code Playgroud)
它适用于添加列,但不适用于创建空间点。
我的问题有两个:(1)为什么使用 mapply 会失败?(2) 有效的方法是什么?