I have a (large) matrix of strings of two numbers. I want to convert the matrix to a numeric matrix where the strings are separated into different columns while preserving the row order and string order of the original matrix. I am doing this in R, in which I am mostly a novice/self-taught. Example input and desired output below
input
test <- matrix[1:5,1:5]
test
col1 col2 col3 col4 col5
row1 "0,0" "0,0" "0,0" "0,0" "0,1"
row2 "0,0" "0,0" "0,0" "0,0" "0,0"
row3 "0,0" "0,0" "0,2" "0,0" "0,0"
row4 "0,0" "0,0" "0,2" "0,0" "0,0"
row5 "0,0" "0,0" "0,0" "0,0" "1,0"
desired output
col1 col1.1 col2 col2.1 col3 col3.1 col4 col4.1 col5 col5.1
row1 0 0 0 0 0 0 0 0 0 1
row2 0 0 0 0 0 0 0 0 0 0
row3 0 0 0 0 0 2 0 0 0 0
row4 0 0 0 0 0 2 0 0 0 0
row5 0 0 0 0 0 0 0 0 1 0
So far I've tried using strsplit and lapply/unlist, and I can make a matrix that doesn't preserve structure of the original matrix, but I need the original structure for downstream applications.
bad output
> matrix(as.numeric(unlist(strsplit(test,","))),nrow=nrow(test))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0 0 2 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 1 0
[3,] 0 0 0 0 0 2 0 0 0 0
[4,] 0 0 0 0 0 0 0 0 0 1
[5,] 0 0 0 0 0 0 0 0 0 0