Error creating matrix in r
I am trying to create a 256*256 matrix in R. Simple task I thought... If I create the data such that
aa=1:65536
z = matrix(bb,nrow=256,ncol=256,byrow=T)
I have the matrix I want e.g.
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
and so on. However, I am not creating the "aa" data but reading it instead such that
aa = read.table("myfile.txt",header=F)
> aa[c(1:10),]
[1] 1513.708 1513.971 1514.067 1513.971 1513.875 1513.622 1513.524 1513.578 1513.577 1513.481
When I read aa, the data looks fine but when I try and turn it into a matrix, the matrix reads as
[,1] [,2] [,3] [,4] [,5]
[1,] Numeric,65536 Numeric,65536 Numeric,65536 Numeric,65536 Numeric,65536
and so on. Any idea why this开发者_开发问答 is happening?
Thank you very much for your help!
Note this paragraph in the 'Memory usage' section of ?read.table
:
‘read.table’ is not the right tool for reading large matrices,
especially those with many columns: it is designed to read _data
frames_ which may have columns of very different classes. Use
‘scan’ instead for matrices.
Granted, your 256x265 matrix isn't large but scan
still seems more appropriate.
aa <- matrix(scan("myfile.txt"), nrow=256, ncol=256, byrow=TRUE)
Try the sep=""
parameter to read.table()
. With the sep-parameter you can define the separators for your file, which separators you can define and how you find in ?read.file
. Comment if there are any additional problems.
EDIT: can you copy-paste us a little of the file you are reading?
精彩评论