0

Hello? I would like to ask a question on R code.

First, here is my raw Excel data, which is a CSV file.

enter image description here

As you can see this photo, zeros in List_code and List_code_2 are written at the very front. However, when I load the CSV file in R studio, zeros disappear. I add a photo on R studio result.

enter image description here

I would like to change numbers like Excel shows. For example, '1475' in List_code and '0' in List_code_2 are needed to convert into '01475' and '00', but '45876' and '10' do not need to change the forms. I tried format() function, but it didn't work. Here is my R codes.

Barcode <- read.csv("Fruit_barcode.csv", header=T) View(Barcode) format(Barcode$List_code, format="%Y")

How can I change the code number forms in R data frame? I am looking forward to seeing some answers. Thank you so much.

seulki choi
  • 45
  • 1
  • 8
  • 1
    Or this https://stackoverflow.com/questions/14409084/pad-with-leading-zeros-to-common-width – P.R Jul 18 '17 at 04:14

1 Answers1

1

Use colClasses, to set all columns as text :

read.csv("datafile.csv", colClasses=rep("character", 7))
Remko Duursma
  • 2,741
  • 17
  • 24