Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste/Collapse in R

I'm confused by paste, and thought it was just simple concatenating.

whales <- c("C","D","C","D","D")  quails <- c("D","D","D","D","D")  results <-paste(whales, quails, collapse = '') 

Why would this return "C DD DC DD DD D" instead of CD DD CD DD DD?

Moreover, why would

results <-paste(whales[1], quails[1], collapse = '') 

return

"C D" ?

with a space?

Thanks, D

EDIT

OK, I see that

results <-paste(whales, quails, collapse = NULL, sep='') 

will get me what I want, but an explanation of why the previous code didn't work? And also thank you to the answerers.

like image 375
DSG Avatar asked Aug 27 '13 20:08

DSG


People also ask

What is collapse in paste in R?

When you pass a paste argument to a vector, the separator parameter will not work. Hence here comes the collapse parameter, which is highly useful when you are dealing with the vectors. It represents the symbol or values which separate the elements in the vector.

How do I paste without space in R?

When pasting strings, R adds a space in between. We can use sep="" to remove the added space. In the context of vectors of the same length, paste() merges corresponding elements of the vectors. To merge all output elements into one long string without added spaces, we can use collapse="" .

What does Paste () do in R?

paste() method in R programming is used to concatenate the two string values by separating with delimiters.


2 Answers

For those who like visuals, here is my take at explaining how paste works in R:

enter image description here

sep creates element-wise sandwich stuffed with the value in the sep argument:

enter image description here

collapse creates ONE big sandwich with the value of collapse argument added between the sandwiches produced by using the sep argument:

enter image description here

like image 195
Ashirwad Avatar answered Sep 20 '22 20:09

Ashirwad


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 = '') 
like image 22
Andreas Avatar answered Sep 20 '22 20:09

Andreas