Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算横断面内线段的长度_R_Spatial - Fatal编程技术网

R 计算横断面内线段的长度

R 计算横断面内线段的长度,r,spatial,R,Spatial,我有纬度、经度和基质类型的横断面数据。下面我提供了一个脚本,该脚本创建了一个假设数据,其中包含3种基质类型,沿着一条直线样带,从经度-24.5开始,到-23.2结束。该样带内有3种基质类型(a、b和c),但基质类型“a”出现4次,基质类型“b”出现两次。我想计算横断面中每个“a”、“b”和“c”基质类型段的总长度(米)。例如,基板段“a”在第一次观察到“b”基板类型的位置结束,基板段c在第四个“a”基板类型段开始的位置结束。我想知道这件衣服的长度。我已经研究了sp和Rdistance软件包,但我

我有纬度、经度和基质类型的横断面数据。下面我提供了一个脚本,该脚本创建了一个假设数据,其中包含3种基质类型,沿着一条直线样带,从经度-24.5开始,到-23.2结束。该样带内有3种基质类型(a、b和c),但基质类型“a”出现4次,基质类型“b”出现两次。我想计算横断面中每个“a”、“b”和“c”基质类型段的总长度(米)。例如,基板段“a”在第一次观察到“b”基板类型的位置结束,基板段c在第四个“a”基板类型段开始的位置结束。我想知道这件衣服的长度。我已经研究了sp和Rdistance软件包,但我真的被卡住了。事先致谢

假设示例:每个框表示我要计算其长度的每个段


Alon我怀疑以米为单位的长度取决于您的投影,因此这里我以度为单位计算长度,并将转换留给您。首先,我按经度排序(因为你们的纬度都一样)

最后,我使用
diff
计算每次运行的开始经度和结束经度之间的差异

# Calculate difference in longitude
data.frame(ID = df_seg$ID[1:(nrow(df_seg)-1)], diff_lon = abs(diff(df_seg$lon)))

# Check data frame
#   ID diff_lon
# 1  A     0.19
# 2  B     0.11
# 3  A     0.10
# 4  B     0.15
# 5  A     0.15
# 6  C     0.15
# 7  A     0.20

关于订购站 我希望我有一个很好的解决办法,但我没有。所以,在我做一些可怕的事情之前我会道歉

library(dplyr)
library(RANN)

# Temporary data frame
df_stations <- pos 

# Function for finding order of stations
station_order <- function(){
  # If only one row, return it (i.e., it's the final station)
  if(nrow(df_stations) == 1)return(df_station)
  # Find the nearest neighbour for the first station
  r <- nn2(data = df_stations %>% select(lon, lat), k = 2)$nn.idx[1,2]
  # Bump the nearest neighbour to first in the data frame
  # This also deletes the first entry
  df_stations[1, ] <<- df_stations[r, ]
  # Drop the nearest neighbour elsewhere in the data frame
  df_stations <<- df_stations %>%  distinct
  # Return the nearest neighbour
  return(df_stations[1, ])
}

# Initialise data frame
res <- df_stations[1,]

# Loop over data frame
for(i in 2:nrow(df_stations))res[i, ] <- station_order()

非常有用,谢谢。我将添加转换。一个简单的问题。显然,在我的真实数据集中,横断面的纬度也会发生变化。我在想,当纬度和经度都在变化时,我应该如何排列它们。例如,如果样带遵循的是纬度而不是经度,我应该按纬度排序吗?@StefánÁkiRagnarsson这是个好问题。我不认为单独按纬度或经度排序在所有情况下都有效,所以我在排序中添加了一个最近邻解决方案。很像我自己,它可以工作,但并不漂亮。再次感谢你的帮助。你的解决方案看起来不错。我根据您的建议得出了第三种解决方案,即获得“df_seg”段
# Pull out start and end points of segments
df_seg <- pos[pmin(nrow(pos), c(1, cumsum(rle(pos$ID)$lengths) + 1)),]
# Calculate difference in longitude
data.frame(ID = df_seg$ID[1:(nrow(df_seg)-1)], diff_lon = abs(diff(df_seg$lon)))

# Check data frame
#   ID diff_lon
# 1  A     0.19
# 2  B     0.11
# 3  A     0.10
# 4  B     0.15
# 5  A     0.15
# 6  C     0.15
# 7  A     0.20
library(dplyr)
library(RANN)

# Temporary data frame
df_stations <- pos 

# Function for finding order of stations
station_order <- function(){
  # If only one row, return it (i.e., it's the final station)
  if(nrow(df_stations) == 1)return(df_station)
  # Find the nearest neighbour for the first station
  r <- nn2(data = df_stations %>% select(lon, lat), k = 2)$nn.idx[1,2]
  # Bump the nearest neighbour to first in the data frame
  # This also deletes the first entry
  df_stations[1, ] <<- df_stations[r, ]
  # Drop the nearest neighbour elsewhere in the data frame
  df_stations <<- df_stations %>%  distinct
  # Return the nearest neighbour
  return(df_stations[1, ])
}

# Initialise data frame
res <- df_stations[1,]

# Loop over data frame
for(i in 2:nrow(df_stations))res[i, ] <- station_order()
#    ID    lon lat
# 1   A -23.20  64
# 2   A -23.30  64
# 3   A -23.40  64
# 4   C -23.47  64
# 5   C -23.50  64
# 6   C -23.55  64
# 7   A -23.60  64
# 8   A -23.70  64
# 9   B -23.80  64
# 10  B -23.85  64
# 11  A -23.95  64
# 12  B -24.00  64
# 13  B -24.03  64
# 14  B -24.06  64
# 15  A -24.10  64
# 16  A -24.15  64
# 17  A -24.20  64
# 18  A -24.25  64