I have a data structure containing char vectrors (see below). It's a bit messy as it came from json source.
I need to combine/concatenate to one big string with lat/long pairs seperated by | and lat/long values separated by comma with the names removed.
i.e."53.193418,-2881248|53.1905138631287,-2.89043889005541|etc.."
I've tried
piped.data<-unname(paste(b, sep="|", collapse=","))
This gets me so far as pairing the values with comma and removing the names.
I just need to add the pipe to the individual pairs
Any ideas?
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"
))
The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.
To concatenate a vector or multiple vectors in R use c() function. This c() function is used to combine the objects by taking two or multiple vectors as input and returning the vector with the combined elements from all vectors.
Concatenating two or more strings using cat() function. We can perform concatenation by inserting the variable name of the string and specifying the separator. we can insert more than two variables also and specify different types of parameter.
I would convert your "b" to a 2-column matrix
and paste with that:
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"
I guess I misread your question.
If it's a single long string you want, first separated by a comma, and then by a pipe, you'll need paste twice:
paste(apply(matrix(b, ncol = 2, byrow = TRUE), 1, paste, collapse = ","),
collapse = "|")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With