43

I have a table read in R as follows:

column1 column2
A        B

What is the command to be used to match two columns together as follows?

Column 3
A_B
Saksham
  • 9,037
  • 7
  • 45
  • 73
Catherine
  • 5,345
  • 11
  • 30
  • 28

2 Answers2

65

I'm a bit unsure what you mean by "merge", but is this what you mean?

> DF = data.frame(A = LETTERS[1:10], B = LETTERS[11:20])
> DF$C = paste(DF$A, DF$B, sep="_")
> head(DF)
  A B  C
1 A K A_K
2 B L B_L
3 C M C_M
4 D N D_N

Or equivalently, as @daroczig points out:

 within(DF, C <- paste(A, B, sep='_'))
csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • To return NA in cases where one or both elements is missing, you could use an `ifelse()` statement as follows: `within(DF, C <- ifelse(is.na(A)==TRUE | is.na(B)==TRUE, NA, paste(A, B, sep='_')))`. – ulfelder Jul 17 '15 at 13:37
  • 1
    Is there a way in Paste to auto ignore NA Values? – myloginid Aug 27 '15 at 11:33
15

My personal favourite involves making use of the unite in tidyr:

set.seed(1)
df <- data.frame(colA = sample(LETTERS, 10),
                 colB = sample(LETTERS, 10))
# packs: pipe + unite
require(magrittr); require(tidyr)


# Unite
df %<>%
  unite(ColAandB, colA, colB, remove = FALSE)

Results

> head(df, 3)
  ColAandB colA colB
1      G_F    G    F
2      J_E    J    E
3      N_Q    N    Q

Side notes

Personally, I find the remove = TRUE / FALSE functionality of unite very useful. In addition tidyr firs the dplyr workflow very well and plays well with separate in case you change your mind about the columns being merged. On the same lines, if NAs are the problem introducing na.omit to your workflow would enable you to conveniently drop the undesirable rows before creating the desired column.

Konrad
  • 17,740
  • 16
  • 106
  • 167