Reputation: 3336
A matrix Aexample is:
Aexample
[,1] [,2] [,3] [,4]
[1,] 0.6 0.4 0.00 0.0
[2,] 0.4 0.4 0.00 0.0
[3,] 0.4 0.4 0.00 0.0
[4,] 0.0 0.0 0.40 0.4
[5,] 0.0 0.0 0.34 0.4
[6,] 0.0 0.0 0.40 0.4
It is saved to a file by
write.table (Aexample, file = "~/R/Aexample", row.names = FALSE, col.names = FALSE);
And then I read by
Aexample = as.matrix(read.table(file="~/R/Aexample"));
But the result always has V1 V2 V3 V4 column names that I don't want:
V1 V2 V3 V4
[1,] 0.6 0.4 0.00 0.0
[2,] 0.4 0.4 0.00 0.0
[3,] 0.4 0.4 0.00 0.0
[4,] 0.0 0.0 0.40 0.4
[5,] 0.0 0.0 0.34 0.4
[6,] 0.0 0.0 0.40 0.4
What's the simplest way to save the Aexample matrix and then read from file and get the original Aexample matrix? (Not to read to aa = as.matrix(read.table(file="~/R/Aexample")) and then create Aexample from aa with many lines of code)
Upvotes: 1
Views: 2073
Reputation: 24074
You can do that in two steps :
1.
Aexample = as.matrix(read.table(file="~/R/Aexample"))
2.
colnames(Aexample) <- NULL
Example
aaa<-matrix(1:24,nrow=6)
> aaa
[,1] [,2] [,3] [,4]
[1,] 1 7 13 19
[2,] 2 8 14 20
[3,] 3 9 15 21
[4,] 4 10 16 22
[5,] 5 11 17 23
[6,] 6 12 18 24
write.table(aaa,"aaa.txt",col.names=F,row.names=F)
bbb<-as.matrix(read.table(file="aaa.txt",header=T))
colnames(bbb)<-NULL
> bbb
[,1] [,2] [,3] [,4]
[1,] 1 7 13 19
[2,] 2 8 14 20
[3,] 3 9 15 21
[4,] 4 10 16 22
[5,] 5 11 17 23
[6,] 6 12 18 24
Upvotes: 2