Concatenate 2 strings in R

0 votes

How to combine two values in R?

For example:

combine = cbind("JET", "XY")
combine
#      [,1]  [,2]
# [1,] "JET" "XY"

I want 'combine' as a single string:

combine_new = "JET,XY"

Which function shall I use?

Apr 13, 2018 in Data Analytics by BHARANI
• 410 points
137 views

1 answer to this question.

0 votes

You can use the paste() command:

You can use paste to do two things:

To concatenate values into a single "string"

example:

 paste("Hey", "Everyone", sep=" ")
[1] "Hey Everyone"

The argument sep specifies the character(s) to be used between the concatenated character vectors.

Refer the example below:

 z <- c("Hello", "Everyone")
 z
[1] "Hello" "Everyone"
paste(z, collapse="--")
[1] "Hello--Everyone"

The argument collapse specifies the character(s) to be used between the elements of the vector to be collapsed.

You can use both sep and collapse together in a single function as follows:

paste(z, "few more text", sep="|-|", collapse="--")
[1] "Hello|-|few more text--Everyone|-|few more text"

I hope this helps!

answered Apr 13, 2018 by Sahiti
• 6,290 points

Related Questions In Data Analytics

0 votes
1 answer

Join multiple strings in R

Joining strings in R is quite an ...READ MORE

answered Jul 19, 2018 in Data Analytics by DataKing99
• 8,150 points
220 views
0 votes
1 answer

Error saying "Error in source("myfirst.R") : myfirst.R:14:2: unexpected '!' 13: 14: ë! ^"

This is a very common issue that ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,820 points
112 views
0 votes
1 answer

Error in source("myfunction.R") : myfunction.R:2:3: unexpected symbol 1: 2: R version

Try formatting your code like this: myfunction <- ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,820 points
736 views
0 votes
1 answer

How to split strings in R?

You can use the  strsplit method to do ...READ MORE

answered Mar 29, 2019 in Data Analytics by Sophie may
• 9,940 points
57 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,420 points
556 views
0 votes
1 answer
0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by Sahiti
• 6,290 points
764 views
0 votes
2 answers

How to count unique values in R?

You can try this way, as.data.frame(v) %>% count(v) READ MORE

answered Aug 8, 2019 in Data Analytics by anonymous
3,580 views