Sunday, January 1, 2017

Script to convert numeric integer data of data frame column into a digit matrix.

At some point I found the need to manipulate and analyze each digit of a series of integer values, perform statistics with each of them and in some occasions add zeros at the beginning of each number. 

So I gave myself the task of performing the following script where data is the dataframe (array) and Numero is the column of interest.
Several cases are established since if there are numbers with less than five digits, that numbers must be filled with leading zeros.

first we declare the array filled with zeros

ar <- array(data = 0, dim = c(length(data$Numero), 5))

for(m in 1:length(data$Numero)){

le = length(as.numeric(strsplit(as.character(data$Numero[m]), "")[[1]]))

if(le == 5){
    for(l in 1:5){
      ar[m,l] <- as.numeric(strsplit(as.character(data$Numero[m]), "")[[1]])[l]
    if(le == 4){
    for(l in 1:4){
      ar[m,l+1] <- as.numeric(strsplit(as.character(data$Numero[m]), "")[[1]])[l]
  if(le == 3){
    for(l in 1:3){
      ar[m,l+2] <- as.numeric(strsplit(as.character(data$Numero[m]), "")[[1]])[l]
  if(le == 2){
    for(l in 1:2){
      ar[m,l+3] <- as.numeric(strsplit(as.character(data$Numero[m]), "")[[1]])[l]
  if(le == 1){
    for(l in 1:1){
      ar[m,l+4] <- as.numeric(strsplit(as.character(data$Numero[m]), "")[[1]])[l]

visualize the final matrix


So with this, we create an array of n x 5 (dealing with integers of five digits) and now we are able to perform statistics with each digit.

You can get the script in: