0

I'm trying to change the format of my data. I have a centre-number which is going from 1-15 and a participant-number which is going from 1-~3000 I would like them to start with zeros, so that the centre-number will have two digits and the participant-number will have 4 digits. (For example participant number 1 would then be 0001).

Thank you!

Inken
  • 21
  • 1

1 Answers1

1

You can use the str_pad function in the 'stringr' package.

library(stringr)
values <- c(1, 5, 23, 123, 43, 7)
str_pad(values, 3, pad='0')

Output:

[1] "001" "005" "023" "123" "043" "007"

In your case as you have two parts to your strings, you can apply the function like this to pad your strings correctly.

# dummy data
centre_participants <- c('1-347', '13-567', '9-7', '15-2507')

# split the strings on "-"
centre_participants <- strsplit(centre_participants, '-')

# apply the right string padding to each component and join together
centre_participants <- sapply(centre_participants, function(x) 
  paste0(str_pad(x[1], 2, pad='0'),'-',str_pad(x[2], 4, pad='0')))

Output:

[1] "01-0347" "13-0567" "09-0007" "15-2507"
Muon
  • 1,294
  • 1
  • 9
  • 31
  • Thank you for your answer! The code works fine, but I can't get it back into my dataframe. I need it to merge the data.frame with another one, where I have the data in the xx-xxxx (centre-participant) format. But as soon as I use the str_pad command, I can't combine the centre with the participant information anymore. As I have no information which both lists include, I don't have any variable to merge by. Is there an option to keep the data.frame and only change the digits in one column? – Inken Apr 14 '20 at 12:06
  • Ah I see what you mean, I'll edit my answer. – Muon Apr 14 '20 at 12:27
  • Does that better answer your question? – Muon Apr 14 '20 at 12:38
  • yes, thank you very much! – Inken Apr 14 '20 at 13:58