Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

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

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
question from:https://stackoverflow.com/questions/5559467/how-to-merge-two-columns-in-r-with-a-specific-symbol

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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='_'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...