【问题标题】:is there an R code to separate coordinates latitudes and longitudes with differing lengths?是否有 R 代码来分隔不同长度的坐标纬度和经度?
【发布时间】:2021-09-24 11:40:05
【问题描述】:

我已经包含了这个最小的例子。

cluster_id<-c(1,2)
lat_long<-c("35.92,0.34;35.98,-0.13;35.73,-1.29","38.98,-0.34;40.23,1.23")
d<-data.frame(cluster_id,lat_long)
d

我希望得到以下输出

cluster_id<-c(1,1,1,2,2) 
latitude<-c(35.92,35.98,35.73,38.98,40.23) 
longitude<-c(0.34,-0.13,-1.29,-0.34,1.23) 
c<-data.frame(cluster_id,latitude,longitude)
c

@Akindele Davies 使用 unsplit 提供了很好的反馈

不过,我对上面的输出 c 很感兴趣

【问题讨论】:

  • @Karthik S 请帮忙
  • 你有lat,long; 还是?为什么有三个值然后是分号?即检查倒数第二个
  • @Onyambu 我已经更新了问题

标签: r dplyr split tidyr latitude-longitude


【解决方案1】:

如果我正确理解了您的问题,那么您有一个字符串,它是纬度-经度对的集合。从您发布的示例中,每个坐标对由分号 (";") 分隔,并且在每一对中,纬度和经度由逗号 (",") 分隔。我们可以使用这个结构来解决这个问题。

foo <- "35.9289842120708,-0.37401629584697;35.9295981311974,-0.370106682789026;35.9289842120708,-0.370106682789026"

# Split into a list coordinate pairs
coord_pairs <- strsplit(foo, split = ";")

# Separate the latitude-longitude components
coords <- strsplit(unlist(coord_pairs), split = ",") # We have to unlist coord_pairs because strsplit() expects a character vector

# coords is a list of two-element vectors (lat and long)
# Combine the elements of coords into a matrix, then coerce to a dataframe

df <- as.data.frame(do.call(rbind, coords)) 

【讨论】:

  • 完美运行。但是,假设我有几行,并且每一行都有自己的纬度和经度集合。如何将拆分对与唯一标识符相关联并转换为数据框?说 cluster_id
  • 所以如果我理解你的话,你现在有一个这样的字符串的向量,而不是拥有一个由纬度-经度对组成的单个字符串?然后让bar为字符串向量,将前面的步骤组合成一个函数parse_line(foo)。然后你可以做do.call(rbind, lapply(seq_along(bar), function(x) cbind(x, parse_line(bar[[x]]))))
  • 我已经更新了问题和预期的解决方案。请帮忙
【解决方案2】:

我已经在对原始答案的评论中回答了您更新后的问题,但我可以理解,作为评论可能很难理解。

首先,我们将我之前列出的步骤组合成一个函数parse_line()。

parse_line <- function(line){
    coord_pairs <- strsplit(line, split = ";")
    # Separate the latitude-longitude components
    coords <- strsplit(unlist(coord_pairs), split = ",") # We have to unlist coord_pairs because strsplit() expects a character vector
    
    # coords is a list of two-element vectors (lat and long)
    # Combine the elements of coords into a matrix, then coerce to a dataframe
    
    df <- as.data.frame(do.call(rbind, coords)) 
}

然后我们将使用parse_line() 作为类似功能parse_lines() 的构建块。

parse_lines <- function(cluster_ids, lines){
  parsed_dfs <- Map(function(x, y) cbind(x, parse_line(y)), cluster_ids, lines) 
# Iterates over all the pairs of cluster_ids and lines
# and adds the cluster_id as a column to the dataframe produced by calling 
# parse_line() on the corresponding line
  combined_df <- do.call(rbind, parsed_dfs) # Combines the list of dataframes into a single dataframe
  colnames(combined_df) <- c("Cluster_ID", "Latitude", "Longitude") # Adds appropriate column names
  return(combined_df)
}

parse_lines(cluster_ids, lat_long)

【讨论】:

  • 非常感谢这个非常有用的评论,最后的澄清,假设我的集群 ID 是唯一的并且没有连续列出,比如 cluster_id
猜你喜欢
  • 2012-02-03
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 2013-04-04
  • 2018-02-27
  • 2015-02-27
  • 1970-01-01
相关资源
最近更新 更多