在R中时将sf包中的函数应用于每一行
我正在使用R中的sf包,为此,我尝试使用它们的一个函数来创建行。目标是将它们的函数应用于下面示例数据框中的每一行(列4:3和6:5)在R中时将sf包中的函数应用于每一行,r,row,apply,sf,R,Row,Apply,Sf,我正在使用R中的sf包,为此,我尝试使用它们的一个函数来创建行。目标是将它们的函数应用于下面示例数据框中的每一行(列4:3和6:5) df <- read.table(text = " from to from_lat from_lon to_lat to_lon travel_time 1 8015345 8849023 50.77083 6.105277 50.71896 6.041269 7.000000 2 8200100 8200101 49.60000 6.13333
df <- read.table(text = " from to from_lat from_lon to_lat to_lon travel_time
1 8015345 8849023 50.77083 6.105277 50.71896 6.041269 7.000000
2 8200100 8200101 49.60000 6.133333 49.63390 6.136765 8.000000
3 8200100 8200110 49.60000 6.133333 49.74889 6.106111 16.000000
4 8200100 8200510 49.60000 6.133333 49.61111 6.050000 4.857143
5 8200100 8200940 49.60000 6.133333 49.55129 5.845025 28.236842
6 8200100 8866001 49.60000 6.133333 49.68053 5.809972 37.000000
7 8200100 8869054 49.60000 6.133333 49.64396 5.904150 14.000000
8 8200101 8200100 49.63390 6.136765 49.60000 6.133333 7.000000
9 8200101 8200110 49.63390 6.136765 49.74889 6.106111 11.000000
10 8200110 8200100 49.74889 6.106111 49.60000 6.133333 17.074074", header = TRUE)
但我真正需要的是对每一行都这样做。我尝试使用for循环,但由于某种原因,它会把
sf
包类搞砸。因此,我假设解决方案将涉及apply()
,但我不确定如何执行。如果我们需要在每行上执行此操作,那么我们可以使用pmap
library(purrr)
library(dplyr)
df%>%
select(4, 6, 3, 5) %>%
pmap(~ c(...) %>%
matrix(., ncol = 2) %>%
st_linestring %>%
st_sfc %>%
st_sf(crc = 4326))
如果我们需要对每行执行此操作,那么我们可以使用
pmap
library(purrr)
library(dplyr)
df%>%
select(4, 6, 3, 5) %>%
pmap(~ c(...) %>%
matrix(., ncol = 2) %>%
st_linestring %>%
st_sfc %>%
st_sf(crc = 4326))
您可以使用
dplyr::rowwise()
进行行分组
df %>% rowwise() %>%
mutate(line_sf = list(matrix(c(from_lon, to_lon, from_lat, to_lat), ncol = 2) %>%
st_linestring()) ) %>%
with(st_sfc(line_sf, crs = 4326)) %>%
plot()
我重新安排了最后一行(在绘图之前
),将10行观测值折叠为一个几何图形集,以便在此处绘图,但您可以将它们单独放置
您可以使用
dplyr::rowwise()
进行行分组
df %>% rowwise() %>%
mutate(line_sf = list(matrix(c(from_lon, to_lon, from_lat, to_lat), ncol = 2) %>%
st_linestring()) ) %>%
with(st_sfc(line_sf, crs = 4326)) %>%
plot()
我重新安排了最后一行(在绘图之前
),将10行观测值折叠为一个几何图形集,以便在此处绘图,但您可以将它们单独放置
不需要dplyr的解决方案:
lsf <- mapply(function(a, b, c, d) {
list(matrix(c(a, b, c, d), ncol = 2) %>%
st_linestring())
}, df$from_lon, df$to_lon, df$from_lat, df$to_lat) %>%
st_sfc(crs = 4326)
plot(lsf)
lsf%
st_linestring())
},df$from_lon,df$to_lon,df$from_lat,df$to_lat)%>%
st_证监会(crs=4326)
绘图(lsf)
不需要dplyr的解决方案:
lsf <- mapply(function(a, b, c, d) {
list(matrix(c(a, b, c, d), ncol = 2) %>%
st_linestring())
}, df$from_lon, df$to_lon, df$from_lat, df$to_lat) %>%
st_sfc(crs = 4326)
plot(lsf)
lsf%
st_linestring())
},df$from_lon,df$to_lon,df$from_lat,df$to_lat)%>%
st_证监会(crs=4326)
绘图(lsf)
不幸的是,它不起作用<代码>is_numeric_matrix(x)中的错误:is.numeric(x)&&is.matrix(x)不是真的我假设这是因为在创建矩阵之前,缺少将第4列和第3列转换为向量、将第6列和第5列转换为数字的步骤。不幸的是,这是sf软件包的一个要求。不幸的是,它不起作用<代码>is_numeric_matrix(x)中的错误:is.numeric(x)&&is.matrix(x)不是真的我假设这是因为在创建矩阵之前,缺少将第4列和第3列转换为向量、将第6列和第5列转换为数字的步骤。不幸的是,这是sf包的要求。