0

a = 5 b = 10

How can i get as output "10.5" or "10_5" ?

This must be general as values of a and b change at every iteration

thank you

3 Answers3

0

You are looking for paste0

paste0(b,".",a)
paste0(b,"_",a)
Triss
  • 561
  • 3
  • 11
0

Perhaps

my_a_b <- data.frame(a = seq(1, 10, 2), b = seq(1, 30, 6))
str(my_a_b)
'data.frame':   5 obs. of  2 variables:
 $ a: num  1 3 5 7 9
 $ b: num  1 7 13 19 25

make a new column and populate it with your b & a, here separated with '.'

my_a_b['b_a'] <- as.character(paste(my_a_b$b,my_a_b$a, sep = '.')) 

> str(my_a_b)
'data.frame':   5 obs. of  3 variables:
 $ a  : num  1 3 5 7 9
 $ b  : num  1 7 13 19 25
 $ b_a: chr  "1.1" "7.3" "13.5" "19.7" ...
Chris
  • 1,647
  • 1
  • 18
  • 25
0

We can use str_c

library(stringr)
str_c(b, ".",  a)
str_c(b, "_", a) 
akrun
  • 874,273
  • 37
  • 540
  • 662