I have a dataframe and want the number variable to be four digits long, in order to do this I need to add between 1-3 leading zeroes, the method I chose to do this is the sprintf function, as it is immaterial that the number is converted to character class. Unfortunately the results are not coming out in the order I want
The test data frame is made as follows and the leading 0 column added on as a third column to allow easy comparison. As can be seen by running the code the order that the leading zero numbers are pasted in does not correspond to the original number order
test <- as.data.frame(cbind(letters,seq(from=1, to=26)))
test[,3]<-sprintf("%04d", test[,2])
by rearranging the data frame order alphabetically by classing the original number column as characters, the sprintf number are now in ascending order although the number series is not.
test.two <- as.data.frame(cbind(letters,seq(from=1, to=26)))
test.two <- test.two[i <-order(as.character(test.two[,2])),]
test.two[,3]<-sprintf("%04d", test.two[,2])
I can create the desired data set by Frankensteining it togther.
test.three <- as.data.frame(cbind(letters,seq(from=1, to=26)))
test.three[,3]<-test.two[,3]
However I would like to know what I am doing wrong and what method would give me the result I expected to get from what I thought was a simple operation!