Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two columns in R with a specific symbol?

Tags:

merge

symbols

r

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
like image 444
Catherine Avatar asked Apr 05 '11 22:04

Catherine


People also ask

How do I combine two columns with a comma in R?

To combine two columns in R data frame with comma separation, we can use paste function with sep argument.

How do I combine two columns from a different Dataframe in R?

Method 1 : Using plyr package rbind. fill() method in R is an enhancement of the rbind() method in base R, is used to combine data frames with different columns. The column names are number may be different in the input data frames. Missing columns of the corresponding data frames are filled with NA.

How do you merge objects in R?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.


2 Answers

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='_'))
like image 147
csgillespie Avatar answered Oct 17 '22 02:10

csgillespie


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.

like image 15
Konrad Avatar answered Oct 17 '22 02:10

Konrad