Paste/Collapse in R

Paste/Collapse in R

For the first question, try the following (which might be more illustrative than choosing to repeat 2 characters).

### Note that R paste's together corresponding elements together...
paste(c("A", "S", "D", "F"),
c("W", "X", "Y", "Z"))

[1] "A W" "S X" "D Y" "F Z"

### Note that with collapse, R converts the above
# result into a length 1 character vector.
paste(c("A", "S", "D", "F"),
c("W", "X", "Y", "Z"), collapse = '')

[1] "A WS XD YF Z"

What you really want to do (to get the "desired" result) is the following:

### "Desired" result:
paste(whales, quails, sep = '', collapse = ' ')

[1] "CD DD CD DD DD"

Note that we are specifying the sep and collapse arguments to different values, which relates to your second question. sep allows each terms to be separated by a character string, whereas collapse allows the entire result to be separated by a character string.

Try

paste(whales, quails, collapse = '', sep = '')

[1] "CDDDCDDDDD"

Alternatively, use a shortcut paste0, which defaults to paste with sep = ''

paste0(whales, quails, collapse = '')

Using Paste to collapse a vector in R

Try

dat <- data.frame(x=c("a","b","c"))
st <- paste(dat$x,collapse="")
st

prints

> st
[1] "abc"

and to make it comma-separated:

> st <- paste(dat$x,collapse=",")
> st
[1] "a,b,c"

Set collapse for paste function in aggregate

Aggregate can be used to paste together the rows with the same value of num as follows:

data_aggr <- data.frame(num=c(1,1,1,2,2), letters=letters[1:5])
aggregate(data_aggr$letters, list(data_aggr$num), FUN=paste, collapse= " ")

# Group.1 x
# 1 1 a b c
# 2 2 d e

paste() collapse with tabs

You don't need to spend time racking your brain on this. Just stick four spaces in collapse.

paste(c("blah","blah"), collapse = "    ")
# [1] "blah blah"

By the way, it would be fine to write to file the way you already have it. If we do

writeLines(paste(c("blah", "blah"), collapse = "\t"), "blah.txt")

the file blah.txt looks like this

blah    blah

In fact, you could simplify it to take paste() out of the equation.

writeLines(c("blah", "blah"), sep = "\t")
# blah blah

Paste and collapse vector and enclose with quotations

We can either use sprintf

sprintf("https://url/api/data/v8.2/table1?$select='%s','%s','%s'", X[1], X[2], X[3])
#[1] "https://url/api/data/v8.2/table1?$select='name','type','target'"

If we have 'n' number of elements in 'X'

s1 <- paste(rep("'%s'", length(X)), collapse=",")
do.call(sprintf, c(fmt = paste0("https://url/api/data/v8.2/table1?$select=", s1), as.list(X) ))
#[1] "https://url/api/data/v8.2/table1?$select='name','type','target'"

or glue

library(glue)
glue("https://url/api/data/v8.2/table1?$select='{X[1]}','{X[2]}', '{X[3]}'")

In the OP's post, if it is a double quote, it is the escape character. We can check with cat

How can I concatenate these strings back together?

You can use paste but you have to specify the collapse argument.

'hello' %>% strsplit('') %>% unlist() %>% paste(collapse = "")

Alternatively you can use str_c from the stringr library:

'hello' %>% strsplit('') %>% unlist() %>% stringr::str_c(collapse = "")

using paste to collapse multiple vectors

You just need option sep=.

paste(a, b, sep=" + ")
# [1] "one + two + 3 + 4"

Pasting two strings using paste function and its collapse argument

paste with more then one argument will paste together term-by-term.

> paste(c("a","b","c"),c("A","B","C"))
[1] "a A" "b B" "c C"

the result being the length of the longest vector, with the shorter term recycled. That enables things like this to work:

> paste("A",c("1","2","BBB"))
[1] "A 1" "A 2" "A BBB"
> paste(c("1","2","BBB"),"A")
[1] "1 A" "2 A" "BBB A"

then sep is used within the elements and collapse to join the elements.

> paste(c("a","b","c"),c("A","B","C"))
[1] "a A" "b B" "c C"
> paste(c("a","b","c"),c("A","B","C"),sep="+")
[1] "a+A" "b+B" "c+C"
> paste(c("a","b","c"),c("A","B","C"),sep="+",collapse="@")
[1] "a+A@b+B@c+C"

Note that once you use collapse you get a single result rather than three.

You seem to not want to combine your two vectors element-wise, so you need to turn them into one vector, which you can do with c(), giving us the solution:

> c(vector_1, vector_2)
[1] "a" "b" "x" "y"
> paste(c(vector_1, vector_2), collapse=" + ")
[1] "a + b + x + y"

Note that sep isn't needed - you are just collapsing the individual elements into one string.



Related Topics



Leave a reply



Submit