将列值粘贴到另一列 [英] paste column values to another column

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

问题描述

我有一个简单的问题,可以通过粘贴解决
我的数据框架如下所示:

  x <-c(3,6,7)
y <-c(0.25,0.35,0.62)
dta1< -data.frame x,y)
xy
1 3 0.25
2 6 0.35
3 7 0.62

我想将这些值粘贴在一列中。
同时添加或删除一些字符。
它将如下所示:

  x 
1 3(.25)
2 6(.35)
3 7(.62)


解决方案>

您可以使用粘贴 paste0 ,但我发现 sprintf 更容易阅读

  sprintf(%i(。%i),dta1 $ x,round * dta1 $ y))

其中%i 标记在以下参数( dta1 $ x 等)中给出的整数。


I have a simple questions that possibly can be solved with paste My data frame looks like this:

x<-c(3,6,7)
y<-c(0.25,0.35,0.62)
dta1<-data.frame(x,y)
  x    y
1 3 0.25
2 6 0.35
3 7 0.62

I want to paste these values together in one column. And add or remove some characters at the same time. it will looks like this :

       x
1 3(.25)
2 6(.35)
3 7(.62)

解决方案

You could use paste or paste0, but I find sprintf easier to read

sprintf("%i(.%i)", dta1$x, round(100*dta1$y))

where %i marks integer numbers given in the following arguments (dta1$x and so on).

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

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