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

[This article was first published on Data R Value, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

print(ar)


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:
https://github.com/pakinja/Data-R-Value/tree/master/ColumntoMatrix

To leave a comment for the author, please follow the link and comment on their blog: Data R Value.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)