Reading a CSV file into a Matrix and order is not retained

I am trying to solve a liner programming model and have several matrices that feed into it from CSV files. I will use one as an example to explain the problem. It is a logic table showing demand for a particular location where 1 means the location has a demand, 0 means no demand.

This is a test case I put together to see what is happening to my file once read in vs having the data typed in.
I have a csv file that is 7 rows by 13 columns (see below) - just drop that into any txt/csv file.

1,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0,0,0,0

I am using:

DemandFile = read.csv("C:/Dev/R_ProjectFiles/xxx/DataTables/tblDemand.csv", header=FALSE)
Demand_DF <- as.matrix(DemandFile)

that creates a matrix that looks like the matrix on top in the image below

If I type that same thing into the matrix itself:

DemandLoc <- matrix(c(
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0),
              ncol=13)

it comes out like this (image on bottom):
Matrix Example

I understand that when I read into a matrix it goes across to fill the column (and thus my ones are not all in the first column like he CSV file) so how can I get a CSV file to act like a Matrix that is "manually" typed in. I need to two to act the same.

I have gone around in circles on this but cannot find a good explanation. Like I said the above is an example, what I need is to get the CSV file to act exactly like the manually typed in version.

BTW, if you cannot tell, this is my first post so pardon if I did not do things correctly. Thanks!

The matrix function has a byrow argument you can supply to indicate you're supplying the data by rows, instead of columns which it assumes by default.

> DemandLoc <- matrix(c(
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0),
              ncol=13,
              byrow=TRUE)
> DemandLoc
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,]    1    0    0    0    0    0    0    0    0     0     0     0     0
[2,]    1    0    0    0    0    0    0    0    0     0     0     0     0
[3,]    1    0    0    0    0    0    0    0    0     0     0     0     0
[4,]    1    0    0    0    0    0    0    0    0     0     0     0     0
[5,]    1    0    0    0    0    0    0    0    0     0     0     0     0
[6,]    1    0    0    0    0    0    0    0    0     0     0     0     0
[7,]    1    0    0    0    0    0    0    0    0     0     0     0     0

I only know this because I recently learned this the hard way while trying to hard-code some matrices for unit-testing.

UGH!!,

Thanks I cannot believe I spent so much time on this. I even had byrow = true in a different version of my code. Sometimes we sure try to make things harder than they are.

Appreciate the quick response.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.