-1

This question is a follow up to paste two data.table columns, and I'll therefore use the same example:

Start with a data.table:

dt <- data.table(L=1:5,A=letters[7:11],B=letters[12:16])
   L A B
1: 1 g l
2: 2 h m
3: 3 i n
4: 4 j o
5: 5 k p

I would like to create a new column that is the combination of two columns, dt$A and dt$B specifically.

dt[, new := paste0(A, B)]
dt
   L A B new
1: 1 g l  gl
2: 2 h m  hm
3: 3 i n  in
4: 4 j o  jo
5: 5 k p  kp

As I'm using new as a sort of ID column, and would prefer some human-readability, I want to use a character to separate the contents of dt$A and dt$B, so:

dt[, new := paste0(A, B, collapse = ".")]

But this alters the behaviour unexpectedly:

dt
   L A B            new
1: 1 g l gl.hm.in.jo.kp
2: 2 h m gl.hm.in.jo.kp
3: 3 i n gl.hm.in.jo.kp
4: 4 j o gl.hm.in.jo.kp
5: 5 k p gl.hm.in.jo.kp
Community
  • 1
  • 1
daRknight
  • 253
  • 3
  • 17

2 Answers2

3

Just use sep as parameter to paste() instead of collapse:

dt[, new := paste(A, B, sep = ".")]
dt
#   L A B new
#1: 1 g l g.l
#2: 2 h m h.m
#3: 3 i n i.n
#4: 4 j o j.o
#5: 5 k p k.p

paste0() doesn't honor the sep parameter (see ?paste0).

Uwe
  • 41,420
  • 11
  • 90
  • 134
  • 1
    how can we use external variables having column names, inside the paste in the above example? e.g. ``cols_to_paste = c("A", "B") ``and then something like ``dt[, new := paste(cols_to_paste, sep = ".")]`` – Abhishek Agnihotri Apr 28 '21 at 08:02
  • This is also what I'm trying to solve and what got me to this question. Any pointers? – Jakob Jun 07 '23 at 16:09
0

The above is the expected behaviour of paste0 -- the solution to achieving the above is actually with paste:

Creating the column new as the concatenation of dt$A and dt$B with a character to separate the contents, use paste instead of paste0:

dt <- data.table(L=1:5,A=letters[7:11],B=letters[12:16])
dt[ , new := paste(A, B, collapse = ".")]
dt
   L A B new
1: 1 g l g.l
2: 2 h m h.m
3: 3 i n i.n
4: 4 j o j.o
5: 5 k p k.p

Using paste0 with collapse = "." for example, changes the output of paste0 as the documentation outlines:

If a value is specified for collapse, the values in the result are then concatenated into a single string, with the elements being separated by the value of collapse.

daRknight
  • 253
  • 3
  • 17
  • Just checked the help for `paste0`/`paste` and this appears to be in line with expectations: _If a value is specified for `collapse`, the values in the result are then concatenated into a single string, with the elements being separated by the value of `collapse`_. – daRknight Mar 27 '17 at 19:03