0 votes
1 view
in R Programming by (44.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 (95.3k 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))

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...