0

Im trying to combine the integer from different columns in a new column. However, I didn't find really helpful stuff about it. And now I'm hoping to get some help here.

Just a simple example:

df 
> df
  t1 t2 t3 t4
1 2  3  4  NA
2 3  4  NA NA
3 5  6  7  8

Now, I want to merge these columns for getting this:

df 
> df
  t1 t2 t3 t4 t_c
1 2  3  4  NA 234
2 3  4  NA NA 34
3 5  6  7  8  5678

I would like to combine the integers without considering the NA's.

1 Answers1

0

You can first replace NA with "", and then use paste0

d[is.na(d)] <- ""
d$t_c <- paste0(d$t1,d$t2,d$t3,d$t4)

  t1 t2 t3 t4  t_c
1  2  3  4     234
2  3  4         34
3  5  6  7  8 5678

Date used

d <- read.table(text="
  t1 t2 t3 t4
1 2  3  4  NA
2 3  4  NA NA
3 5  6  7  8", header=T)
milan
  • 4,782
  • 2
  • 21
  • 39