Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

For example, if I have this:

 n = c(2, 3, 5) 

s = c("aa", "bb", "cc") 

b = c(TRUE, FALSE, TRUE) 

df = data.frame(n, s, b)

  n  s   b

1 2 aa  TRUE

2 3 bb FALSE

3 5 cc  TRUE

Then how do I combine the two columns n and s into a new column named x such that it looks like this:

  n  s   b x

1 2 aa  TRUE 2 aa

2 3 bb FALSE  3 bb

3 5 cc  TRUE 5 cc

1 Answer

0 votes
by (108k points)

You just have to use paste.

df$x <- paste(df$n,df$s)

 df

#   n s     b x

# 1 2 aa  TRUE 2 aa

# 2 3 bb FALSE 3 bb

# 3 5 cc  TRUE 5 cc

31k questions

32.9k answers

500 comments

693 users

Browse Categories

...