Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1D data frame to 2D format in R

Tags:

r

My data is in a 1 column x 40000 row data frame. I would like to make it a 200 column x 200 row data frame. When I was using all numeric values, I used the code below to transform my vector into a matrix.

spematrix=data.matrix(spe) 
matrix = matrix(spematrix, nrow = 200, ncol=200) 

I cannot use this now though because my data contains non-numeric values (numbers separated by commas).

I am new to R and would appreciate any help.

like image 601
Mary R Avatar asked Nov 27 '25 12:11

Mary R


1 Answers

Sounds like you already know what to do. You can scrub your data with gsub to remove the , characters:

spe <- gsub(",","",spe[,1]) # returns a character vector 
spe <- as.numeric(spe) # convert to numeric

Then,

library(Matrix)
spematrix <- data.matrix(spe) 
matrix = Matrix(spematrix, nrow = 200, ncol=200)
like image 188
Brandon Bertelsen Avatar answered Nov 30 '25 19:11

Brandon Bertelsen