【问题标题】:Apply function from sf package to each row while in R在 R 中将 sf 包中的函数应用于每一行
【发布时间】:2018-04-29 14:42:41
【问题描述】:

我正在使用 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.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)

我知道如何为一行执行此操作:

library(sf)
library(dplyr)

x = matrix(as.numeric(c(df[1, c(4, 3)],
             df[1, c(6, 5)])), ncol = 2, byrow = TRUE)
class(x)
typeof(x)
l1 = st_linestring(x = x)
lsf = l1 %>% 
  st_sfc() %>% 
  st_sf(crs = 4326)
plot(lsf) #just to confirm that it is a line

但我真正需要的是为每一行都这样做。我尝试使用 for 循环,但由于某种原因,它与 sf 包类混淆了。所以我假设解决方案将涉及apply(),但我不确定如何。

【问题讨论】:

    标签: r row apply sf


    【解决方案1】:

    如果我们需要对每一行都这样做,那么我们可以使用pmap

    library(purrr)
    library(dplyr)
    df%>%
      select(4, 6, 3, 5) %>% 
      pmap(~ c(...) %>% 
                matrix(., ncol = 2) %>% 
                st_linestring %>%
                st_sfc %>%
                st_sf(crc = 4326))
    

    【讨论】:

    • 不幸的是它不起作用。 Error in is_numeric_matrix(x) : is.numeric(x) &amp;&amp; is.matrix(x) is not TRUE 我认为这是因为在创建矩阵之前将第 4 列和第 3 列转换为向量,将第 6 列和第 5 列转换为向量,并且毕竟转换为数字。不幸的是,这是 sf 包的要求。
    【解决方案2】:

    您可以使用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()
    

    我重新安排了最后一行(plot 之前)以将 10 行观测值折叠成一个几何图形集以便在此处绘图,但您可以将它们单独保留。

    【讨论】:

      【解决方案3】:

      不需要 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-16
        • 2015-12-05
        • 2023-03-17
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多