将文本文件读取为矩阵并将其另存为json文件

时间:2017-04-02 21:01:59

标签: json r file matrix

我没有标题的文件是一个在RStudio中有多个向量的矩阵,它看起来像是:

Y 1 1.53 -0.06 0.58 0.52 0.42 0.16 0.79 NA -0.60 -0.30 -0.78 NA -0.39

B 1 -0.01 -0.30 0.23 0.01 NA NA NA -0.15 0.45 -0.04 0.14 -1.16 -0.14

我使用Json文件来识别我的matrice和NA值,但它不能识别我的matrice上的所有向量这是我的r脚本:

 my_data <- read.table(filename ,sep = "\t" , quote= "", 
          stringsAsFactors = FALSE , header = TRUE)
    my_matrix <- as.matrix(my_data[  ,c(4:176)])
    rownames (my_matrix) <- my_data$UID
    exportJson <- toJSON(my_matrix)
    write(exportJson, "matrix_ID.json")

我只得到一个列表,它不识别向量。 我的matrix_ID.json文件:

[1, 1.53, -0.06, 0.58, 0.52 ,0.42, 0.16, 0.79, u'NA', -0.60, -0.30 ,-0.78, u'NA',  -0.39, 1, -0.01, -0.30 ,0.23, 0.01, u'NA',u'NA',u'NA', -0.15 , 0.45, -0.04, 0.14 ,-1.16, -0.14]

我想用向量解析文件为矩阵并在我的python脚本上识别NA值:

Y : 1, 1.53, -0.06, 0.58, 0.52 ,0.42, 0.16, 0.79, 'NA', -0.60, -0.30 ,-0.78, 'NA',  -0.39
B :  1, -0.01, -0.30 ,0.23, 0.01, 'NA','NA','NA', -0.15 , 0.45, -0.04, 0.14 ,-1.16, -0.14

我该怎么办?谢谢你的帮助

0 个答案:

没有答案