是否有R代码来分隔不同长度的坐标纬度和经度?

is there an R code to separate coordinates latitudes and longitudes with differing lengths?

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

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很感兴趣

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

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)) 

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

首先,我们将把我之前列出的步骤合并到一个函数中 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)