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
454 views
in Technique[技术] by (71.8m points)

dataframe - paste several column values into one value in R

I have a really simple question that I cannot find a straightforward answer for. I have a data.frame that looks like this:

df3 <- data.frame(x=c(1:10),y=c(5:14),z=c(25:34))

ID  x  y  z
1   1  5 25
2   2  6 26
3   3  7 27
etc.

And I want to 'paste' together the different values in each column so that they form a single, combined value, as in:

ID x+y+z
1  1525
2  2626
3  3727

I'm sure that this is very easy to do, but I just don't know how!

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Yep, paste() is exactly what you want to do:

 df3$xyz <- with(df3, paste(x,y,z, sep=""))

 # Or, if you want the result to be numeric, rather than character
 df3$xyz <- as.numeric(with(df3, paste(x,y,z, sep="")))

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

...