Reputation: 21971
I would like to read in the following matrix in R, but then discard the row and column names:
A B C D
A 0 4562 50714 882
B 5718 2302 47 26
C 72055 82 5889 215
D 1930 41 85 396
I do this:
matrix1 <-read.table("matrix_min.csv", header=T, sep=",")
m <- as.matrix(matrix1)
However, the resulting 'm' still has the row and column names. How do I get rid of them?
For some reason, on reading in the file using
matrix1 <-read.table("matrix_min.csv", header=T, sep=",")
m <- as.matrix(matrix1,row.names=0,col.names=0)
rownames(m) <- colnames(m) <- NULL
print(m)
produces:
[,1] [,2] [,3] [,4] [,5]
[1,] "A" " 0" "4562" "50714" "882"
[2,] "B" " 5718" "2302" " 47" " 26"
[3,] "C" "72055" " 82" " 5889" "215"
[4,] "D" " 1930" " 41" " 85" "396"
Upvotes: 1
Views: 20058
Reputation: 89057
You need to use header = TRUE
and row.names = 1
to read in the first row and first column as dimension names.
dat <- data.matrix(read.csv("matrix_min.csv", header = TRUE, row.names = 1,
sep = ","))
Then, as suggested in the comments, use dimnames<-
to remove the dimension names:
dimnames(dat) <- NULL
Upvotes: 9