Avoiding: read.table truncates numeric values ​​starting at 0

I want to import a table (file .txt) into R s read.table(). One column in my table is an identifier with nine digits - some identifiers start with 0, others with 1 or 2.

R truncates the first 0 (012345678 becomes 12345678), which leads to problems when using this identifier to join another table.

Can someone tell me how to solve the problem?

+9
source share
3 answers

As Ben's answer says, colClassesan easier way to do this. Here is an example:

read.table(text = 'col1 col2
           0012 0001245',
           head=T,
           colClasses=c('character','numeric'))

  col1 col2
1 0012 1245      ## col1 keep 00 but not col2
+13
source

, : colClasses read.table(), , , character, numeric. character , sprintf . (, , .)

+3

for, . -hoc- ( 0 ), , :

Take an example of a zip code column. All values ​​must contain 5 digits (for example, 01234), but R deletes the leading zeros (so "01234" becomes "1234"). You can add trailing zero to all cells that contain only 4 characters with this code:

for (i in 1:nrow(df)){
  if(nchar(df$zipCode[i])<5){
    df$zipCode[i]<- paste0('0',df$zipCode[i])
  }
}
0
source

All Articles