gpt4 book ai didi

r - 用|连接char向量。分隔器

转载 作者:行者123 更新时间:2023-12-04 09:03:56 24 4
gpt4 key购买 nike

我有一个包含char vectrors的数据结构(请参见下文)。它来自json源,有点困惑。

我需要将经/纬线对用|分隔开,将一个大字符串组合/合并为一个大字符串和经/纬度值,以逗号分隔并删除名称。

即“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列的matrix并粘贴:

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

关于r - 用|连接char向量。分隔器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27675542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com