Tal
Tal

Reputation: 31

Writing matrix as table to csv file

I have a matrix, it looks in print:

  [,1]        
[1,] Character,17
[2,] Character,17
[3,] Character,17
[4,] Character,17
[5,] Character,17
[6,] Character,17
[7,] Character,17

I want to write it to csv file.

write.table(mat, file = "...", ..)

It writes the matrix like this:

c("1", "2", "3")

c("1", "1", "2")

Maybe I should change the data in the matrix that will not be a character.

How can I do this?

Thanks

I want to write the data not as a vector. It should be:

"1", "2", "3"

"1", "1", "2"

Upvotes: 3

Views: 124

Answers (2)

MichaelChirico
MichaelChirico

Reputation: 34703

data.table's fwrite could handle this directly, since it can handle list columns, though it looks a bit odd:

library(data.table)
mat = structure(lapply(1:7, function(i) letters[1:17]), dim = c(7, 1))

# need to set sep to something besides ,
fwrite(mat, sep2 = c('', ',', ''), sep = '\t')
# V1
# a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
# a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
# a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
# a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
# a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
# a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
# a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q

Much more beautiful would be to use transpose to "flip" the underlying list:

fwrite(transpose(mat))

Upvotes: 2

akrun
akrun

Reputation: 886938

This could happen when a list of vectors are converted directly to matrix by wrapping matrix on it. Returns a matrix with each element a list. e.g.

set.seed(24)
lst1 <- lapply(1:7, function(x) sample(letters[1:10], 17, replace = TRUE))

If we do matrix

mat <- matrix(lst1)
str(mat)
#List of 7
# $ : chr [1:17] "g" "c" "h" "g" ...
# $ : chr [1:17] "h" "a" "e" "e" ...
# $ : chr [1:17] "c" "a" "c" "h" ...
# $ : chr [1:17] "e" "j" "i" "a" ...
# $ : chr [1:17] "d" "j" "g" "h" ...
# $ : chr [1:17] "b" "b" "a" "h" ...
# $ : chr [1:17] "e" "c" "c" "d" ...
# - attr(*, "dim")= int [1:2] 7 1

mat
#     [,1]        
#[1,] Character,17
#[2,] Character,17
#[3,] Character,17
#[4,] Character,17
#[5,] Character,17
#[6,] Character,17
#[7,] Character,17

Instead, it can be either corrected before wrapping to matrix

mat2 <- simplify2array(lst1)
#or # mat2 <- do.call(cbind, lst1)
mat2
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,] "g"  "h"  "c"  "e"  "d"  "b"  "e" 
# [2,] "c"  "a"  "a"  "j"  "j"  "b"  "c" 
# [3,] "h"  "e"  "c"  "i"  "g"  "a"  "c" 
# [4,] "g"  "e"  "h"  "a"  "h"  "h"  "d" 
# [5,] "b"  "e"  "f"  "a"  "c"  "g"  "b" 
# [6,] "c"  "h"  "g"  "a"  "g"  "d"  "b" 
# [7,] "j"  "b"  "a"  "d"  "j"  "e"  "h" 
# [8,] "h"  "a"  "f"  "g"  "j"  "b"  "h" 
# [9,] "f"  "e"  "g"  "c"  "j"  "b"  "g" 
#[10,] "j"  "b"  "a"  "e"  "c"  "b"  "i" 
#[11,] "i"  "a"  "b"  "g"  "h"  "i"  "e" 
#[12,] "d"  "e"  "d"  "e"  "h"  "f"  "e" 
#[13,] "i"  "d"  "c"  "g"  "e"  "d"  "g" 
#[14,] "h"  "j"  "f"  "j"  "j"  "e"  "f" 
#[15,] "g"  "f"  "c"  "h"  "c"  "i"  "j" 
#[16,] "h"  "f"  "d"  "j"  "c"  "i"  "i" 
#[17,] "a"  "j"  "f"  "d"  "a"  "c"  "a" 

Or from the mat itself

simplify2array(mat)

Or

sapply(mat, I)

Now, with write.table on 'mat2', should give the expected output

Upvotes: 2

Related Questions