Intellipaat Back

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

Let's say, I am having the following:

  n       s     

1 Alabama NA 

2 New     Hampshire

3 New     York  

and from that I wanted to combine the two columns into one without the NA value:

  n      

1 Alabama 

2 New Hampshire

3 New York 

How I can perform that? I tried using paste() but it in turn also pasted in the NA value as Alabama NA. Please let me know!

1 Answer

0 votes
by (107k points)

For achieving that, in R programming, you can use the paste() but with the coalesce, from the dplyr package:

df$n <- paste(df$n, coalesce(df$s, ""))

You can also use this is.na for a pure base R solution:

df$n <- paste(df$n, ifelse(is.na(df$s), "", df$s))

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...