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

January 1, 2017
By

(This article was first published on Data R Value, and kindly contributed to R-bloggers)

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:

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...