连接char矢量与|分隔器

我有一个包含char vectrors的数据结构(见下文).它来自json源,有点乱.

我需要组合/连接到一个大的字符串,其中lat / long对由|分隔和逗号分隔的lat / long值和删除的名称.

即 “53.193418,-2881248 | 53.1905138631287,-2.89043889005541 |等.”

我试过了

piped.data<-unname(paste(b, sep="|", collapse=","))

这让我想到将值与逗号配对并删除名称.

我只需要将管道添加到各个对中

有任何想法吗?

dput(b)

structure(c("53.193418", "-2.881248", "53.1905138631287", "-2.89043889005541", 
"53.186744", "-2.890165", "53.189836", "-2.893896", "53.1884117", 
"-2.88802", "53.1902965", "-2.8919373", "53.1940384", "-2.8972299", 
"53.1934748", "-2.8814698", "53.1894004", "-2.8886692", "53.1916771", 
"-2.8846099"), .Names = c("location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude", 
"location.coordinate.latitude", "location.coordinate.longitude"
))
我会将你的“b”转换为2列矩阵并粘贴:

apply(matrix(b, ncol = 2, byrow = TRUE), 1, paste, collapse = "|")
#  [1] "53.193418|-2.881248"              "53.1905138631287|-2.89043889005541"
#  [3] "53.186744|-2.890165"              "53.189836|-2.893896"               
#  [5] "53.1884117|-2.88802"              "53.1902965|-2.8919373"             
#  [7] "53.1940384|-2.8972299"            "53.1934748|-2.8814698"             
#  [9] "53.1894004|-2.8886692"            "53.1916771|-2.8846099"

编辑

我想我误解了你的问题.

如果它是你想要的一个长字符串,首先用逗号分隔,然后用管道分隔,你需要粘贴两次:

paste(apply(matrix(b, ncol = 2, byrow = TRUE), 1, paste, collapse = ","), 
      collapse = "|")
相关文章
相关标签/搜索