C.Nijman
C.Nijman

Reputation: 11

exporting matrix from r to csv file with only matrix entries

This is matrix A in r:

          [,1]         [,2]         [,3]            [,4]    
[1,]   0.69036059      0.02021414   0.0006167409     0.0000000000   
[2,]   0.23268734      0.67640555   0.0202016684     0.0006167409    
[3,]   0.06068696      0.22798377   0.6759883802     0.0202016684    
[4,]   0.01307434      0.05946023   0.2278431604     0.6759883802

The code used to export this matrix: write.csv(A1,"ProbMatrix.csv")

The result in csv file:

        V1               V2          V3               V4
1   0.69036059      0.02021414   0.0006167409     0.0000000000
2   0.23268734      0.67640555   0.0202016684     0.0006167409
3   0.06068696      0.22798377   0.6759883802     0.0202016684
4   0.01307434      0.05946023   0.2278431604     0.6759883802

That is not what I want, I just want the numbers

0.69036059   0.02021414   0.0006167409   0.0000000000
0.23268734   0.67640555   0.0202016684   0.0006167409
0.06068696   0.22798377   0.6759883802   0.0202016684
0.01307434   0.05946023   0.2278431604   0.6759883802

I tried to delete the rows, and columns before exporting, I tried to include the row.names=False and col.names=FALSE argument in the write.csv code, but none give me the results I want.

How can I solve this problem?

Upvotes: 1

Views: 7278

Answers (2)

Nar
Nar

Reputation: 658

In write.table function there is an option to instruct column names and/ or names are not to be exported: row.names = F and/or col.names = F correspondingly. Default values are TRUE(meaning exporting names) unless you specify it as FALSE in your command :

write.table(A, file="file.csv", row.names = F, col.names = F)

Upvotes: 1

Suhas Hegde
Suhas Hegde

Reputation: 416

The issue here is how R handles csv files while reading them back into the R environment. If you use write.table(...row.names = FALSE, col.names = FALSE) to save the matrix into a csv file it will write the matrix without the column names added to them. But the problem arises when you are reading it back into the R environment using read.table() or read.csv() because these functions read the file back into a data.frame and not a matrix type object.

Let me illustrate it here.

data_set <- c(0.69036059, 0.02021414, 0.0006167409, 0.0000000000, 0.23268734, 
              0.67640555, 0.0202016684, 0.0006167409, 0.06068696, 0.22798377,
              0.6759883802, 0.0202016684, 0.01307434, 0.05946023, 0.2278431604 , 
              0.6759883802)

test_matrix <- matrix(data = data_set, nrow = 4, ncol = 4)

# Look at the type of the object
str(test_matrix)
num [1:4, 1:4] 0.690361 0.020214 0.000617 0 0.232687 ...

write.table(x = test_matrix,file = "test.csv", sep = ',', 
                              row.names = FALSE, col.names = FALSE)

csv file has no column names(headers) added to it

test_object <- read.table(file = "test.csv", header = FALSE, sep = ',')

str(test_object)
'data.frame':   4 obs. of  4 variables:
 $ V1: num  0.690361 0.020214 0.000617 0
 $ V2: num  0.232687 0.676406 0.020202 0.000617
 $ V3: num  0.0607 0.228 0.676 0.0202
 $ V4: num  0.0131 0.0595 0.2278 0.676

So while reading it back to R it reads the data from csv as a data.frame object. If there are no column names specified to dataframes they are auto created. I think that is the issue here.

Even if we use read_csv function from readr package the same issue persists.

library(readr)

test_object <- read_csv("test.csv", col_names = FALSE)

str(test_object)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   4 obs. of  4 variables:
 $ X1: num  0.690361 0.020214 0.000617 0
 $ X2: num  0.232687 0.676406 0.020202 0.000617
 $ X3: num  0.0607 0.228 0.676 0.0202
 $ X4: num  0.0131 0.0595 0.2278 0.676
 - attr(*, "spec")=List of 2
  ..$ cols   :List of 4
  .. ..$ X1: list()
  .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
  .. ..$ X2: list()
  .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
  .. ..$ X3: list()
  .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
  .. ..$ X4: list()
  .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
  ..$ default: list()
  .. ..- attr(*, "class")= chr  "collector_guess" "collector"
  ..- attr(*, "class")= chr "col_spec"

I think one possible solution is to save/write the object in .RData file. You can find more in this question - R save Matrix to csv, and load as Matrix

Upvotes: 0

Related Questions