连接char矢量与|分隔器

时间:2014-12-28 10:26:59

标签: r

我有一个包含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"
))

6 个答案:

答案 0 :(得分:7)

我会将您的“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 = "|")

答案 1 :(得分:6)

另一种选择是将矢量重塑为data.frame,

do.call(paste, c(data.frame(matrix(b, ncol=2, byrow=TRUE)), 
        list(sep=",", collapse="|")))

答案 2 :(得分:5)

另一种选择是

paste(tapply(b, gl(length(b)/2, 2), toString), collapse = "|")
# [1] "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"

如果您不想在逗号后面留空格,请执行

paste(tapply(b, gl(length(b)/2, 2), paste, collapse = ","), collapse = "|")

修改 所以@akrun和@SvenHohenstein能够对他们的解决方案进行矢量化,所以这里有一些插图的基准

b <- rep(b, 1e3)

library(microbenchmark)

microbenchmark(
 SH = paste(paste(b[c(TRUE, FALSE)], b[c(FALSE, TRUE)], sep = ","), collapse = "|"),
 akrun1 = paste(c(rbind(b,rep(c(',','|'), length.out = length(b))))[-length(b)*2], collapse = ""),
 akrun2 = paste(vapply(split(b,cumsum(grepl('latitude',names(b)))), paste, collapse=",", character(1L)), collapse="|"),
 akrun3 = as.data.table(matrix(b, ncol=2, byrow=TRUE))[, paste(V1, V2, sep=',',collapse="|")],
 AM = paste(apply(matrix(b, ncol = 2, byrow = TRUE), 1, paste, collapse = ","),  collapse = "|"),
 DA = paste(tapply(b, gl(length(b)/2, 2), paste, collapse = ","), collapse = "|"),
 BA = do.call(paste, c(data.frame(matrix(b, ncol=2, byrow=TRUE)), list(sep=",", collapse="|")))
)

#  Unit: milliseconds
#  expr       min        lq      mean    median        uq        max neval
#    SH  6.207338  6.275886  6.633830  6.472943  6.915140  10.556983   100
#akrun1  8.738792  8.790045  9.301718  9.049665  9.611671  11.899290   100
#akrun2 40.676819 42.329860 45.361688 43.887247 46.427638 109.963421   100
#akrun3  4.648384  4.831599  5.019834  4.901934  5.217579   5.798325   100
#    AM 38.322320 40.905073 43.108411 42.457375 44.875023  56.236726   100
#    DA 47.102466 49.679579 52.092028 51.237417 53.694154  68.123738   100
#    BA  5.227204  5.366769  6.147758  5.494207  5.806313  55.938247   100

答案 3 :(得分:4)

你可以尝试

 paste(sapply(split(b,cumsum(grepl('latitude',names(b)))),
             toString),collapse="|")

如果您不需要space

 paste(sapply(split(b,cumsum(grepl('latitude',names(b)))),
                             paste, collapse=","), collapse="|")

或者使用vapply

faster
 paste(vapply(split(b,cumsum(grepl('latitude',names(b)))),
             paste, collapse=",", character(1L)), collapse="|")

或者

  paste(c(rbind(b,rep(c(',','|'),length.out=length(b))))[
                            -length(b)*2],collapse="")

  library(data.table)
  as.data.table(matrix(b, ncol=2, byrow=TRUE))[,
                  paste(V1, V2, sep=',',collapse="|")]

答案 4 :(得分:4)

您可以使用逻辑索引和矢量回收:

paste(paste(b[c(TRUE, FALSE)], b[c(FALSE, TRUE)], sep = ","), collapse = "|")

答案 5 :(得分:2)

您可以这样做:

tmp <- apply(matrix(b, ncol = 2, byrow = TRUE), MARGIN = 1,  FUN = paste, collapse = ",")
paste(tmp, collapse = "|")
# [1] "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"