Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paste column values to another column

Tags:

dataframe

r

paste

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)
like image 879
user3801415 Avatar asked Feb 16 '26 12:02

user3801415


2 Answers

You just need to trim the string and combine with paste, so something like:

paste0(x, "(", substr(y, 2, nchar(y)), ")")

will give you what you are after

like image 111
csgillespie Avatar answered Feb 19 '26 06:02

csgillespie


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).

like image 41
Backlin Avatar answered Feb 19 '26 04:02

Backlin