将几个列值粘贴到R中的一个值中 [英] paste several column values into one value in R

查看:197
本文介绍了将几个列值粘贴到R中的一个值中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,我找不到一个简单的答案。我有一个数据框架,如下所示:

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

ID xyz
1 1 5 25
2 2 6 26
3 3 7 27

我想粘贴不同的值在每列中,使它们形成一个单一的组合值,如:

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

我确信这是很简单,但我只是不知道如何!

解决方案

是的, 正好你想做什么:

  df3 $ xyz< ;  - 与(df3,粘贴(x,y,z,sep =))

#或者,如果您希望结果为数字,而不是字符
df3 $ xyz < - as.numeric(with(df3,paste(x,y,z,sep =)))


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!

解决方案

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="")))

这篇关于将几个列值粘贴到R中的一个值中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆