【问题标题】:Writing matrix as table to csv file将矩阵作为表格写入 csv 文件
【发布时间】:2019-08-05 00:00:49
【问题描述】:

我有一个矩阵,它在打印中显示:

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

我想将其写入 csv 文件。

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

它这样写矩阵:

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

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

也许我应该更改矩阵中不是字符的数据。

我该怎么做?

谢谢

我想把数据写成向量而不是向量。应该是:

"1", "2", "3"

"1", "1", "2"

【问题讨论】:

  • 它看起来像矩阵的list。您需要 unlist 到单个 data.frame
  • 您能否在问题中包含dput(mat[1:3 ,])

标签: r


【解决方案1】:

vectors 中的list 通过包装matrix 直接转换为matrix 时,可能会发生这种情况。返回一个matrix,每个元素都是一个list。例如

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

如果我们这样做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

相反,它可以在包装到 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" 

或来自mat 本身

simplify2array(mat)

或者

sapply(mat, I)

现在,write.table 在“mat2”上,应该会给出预期的输出

【讨论】:

    【解决方案2】:

    data.tablefwrite 可以直接处理这个问题,因为它可以处理list 列,虽然看起来有点奇怪:

    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
    

    使用transpose 来“翻转”底层list 会更漂亮:

    fwrite(transpose(mat))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2023-03-17
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多