Reputation: 1139
I have a matrix written to a csv file.
I'm trying to read the file and get the data as a matrix. I have used as.matrix and data.matrix functions. However I'm unable to load the data as a matrix.
My goal is to use this distance matrix for hierarchical clustering.
Upvotes: 4
Views: 10229
Reputation: 887951
We can use read.csv
to read the .csv
file, set the first column as row names (row.names=1
), and converting to matrix
(as.matrix
) should work fine.
d1 <- read.csv('Test_Matrix.csv', row.names=1)
m1 <- as.matrix(d1)
m1
# A B C D
#A 0 1 2 3
#B 1 0 4 5
#C 2 4 0 6
#D 3 5 6 0
is.matrix(m1)
#[1] TRUE
Or as @RHertel mentioned in the comments, we can combine both in a single step
as.matrix(read.csv('Test_Matrix.csv', row.names=1))
Upvotes: 7