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 14, 2018 in Data Analytics by BHARANI
• 420 points
1,245 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 14, 2018 by Sahiti
• 6,370 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,250 points
1,542 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,840 points
991 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,840 points
2,300 views
0 votes
1 answer

How to split strings in R?

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

answered Mar 30, 2019 in Data Analytics by Sophie may
• 10,620 points
505 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,949 views
+2 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,370 points
3,779 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
6,497 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP