Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate char vector with | separator

Tags:

r

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"
))
like image 898
Leehbi Avatar asked Dec 28 '14 10:12

Leehbi


People also ask

How do you combine character vectors?

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.

How do I combine character vectors in R?

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.

How do I combine variables and strings in R?

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.


1 Answers

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" 

Edit

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 = "|")
like image 174
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 22 '22 03:09

A5C1D2H2I1M1N2O1R2T1