0

I am trying to concatenate some data in a column of a df, with "0000"

I tried to use paste() in a loop, but it becomes very performance heavy, as I have +2.000.000 rows. Thus, it takes forever.

Is there a smart, less performance heavy way to do it?

#DF:
CUSTID    VALUE
103       12
104       10
105       15
106       12
...       ...

#Desired result:
#DF:
CUSTID    VALUE
0000103   12
0000104   10
0000105   15
0000106   12
...       ...

How can this be achieved?

Tue Herlevsen
  • 65
  • 1
  • 1
  • 3

1 Answers1

0

paste is vectorized so it'll work with a vector of values (i.e. a column in a data frame. The following should work:

DF <- data.frame(
  CUSTID = 103:107,
  VALUE = 13:17
)

DF$CUSTID <- paste0('0000', DF$CUSTID)

Should give you

   CUSTID VALUE
1 0000103    13
2 0000104    14
3 0000105    15
4 0000106    16
5 0000107    17
struggles
  • 825
  • 5
  • 10