How to convert data to Matrix format

I imported the raw data (10,000 rows and 392 columns) into R via read.csv. I am wondering how I can convert this to Matrix format. Thank you so much for your help!

+5
source share
3 answers

Your question will probably be ported to StackOverflow. However, the answer is relatively simple, and I provided it. As a result, read.csv creates data.frame. If all your values ​​are one and the same basic element (e.g. a variable), i.e. a numeric, symbol, etc. Then you can represent them in the Matrix data structure. You can do this with the as.matrix function.

eg.

mydataframe <- data.frame(a=c(1,2),b=c(2,3))    
mymatrix <- as.matrix(mydataframe)
+7
source

as.matrix() data.matrix(), () . :

d <- data.frame(1:10, letters[1:10])
as.matrix(d)
data.matrix(d)

> as.matrix(d)
      X1.10 letters.1.10.
 [1,] " 1"  "a"          
 [2,] " 2"  "b"          
 [3,] " 3"  "c"          
 [4,] " 4"  "d"          
 [5,] " 5"  "e"          
 [6,] " 6"  "f"          
 [7,] " 7"  "g"          
 [8,] " 8"  "h"          
 [9,] " 9"  "i"          
[10,] "10"  "j"          
> data.matrix(d)
      X1.10 letters.1.10.
 [1,]     1             1
 [2,]     2             2
 [3,]     3             3
 [4,]     4             4
 [5,]     5             5
 [6,]     6             6
 [7,]     7             7
 [8,]     8             8
 [9,]     9             9
[10,]    10            10

, as.matrix() , data.matrix() , .

+5

read.csv, matrix, scan, , skip=1, .

m = matrix(scan("file.csv", what=numeric(), skip=1), nrow=392)

read.csv .

+3

All Articles